Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gatsby): convert component-data-dependencies to typescript #24028

Merged
@@ -1,8 +1,10 @@
const reducer = require(`../component-data-dependencies`)
import { componentDataDependenciesReducer as reducer } from "../component-data-dependencies"

import { ICreatePageDependencyAction } from "../../types"

describe(`add page data dependency`, () => {
it(`lets you add a node dependency`, () => {
const action = {
const action: ICreatePageDependencyAction = {
type: `CREATE_COMPONENT_DEPENDENCY`,
payload: {
path: `/hi/`,
Expand All @@ -16,21 +18,21 @@ describe(`add page data dependency`, () => {
})
})
it(`lets you add a node dependency to multiple paths`, () => {
const action = {
const action: ICreatePageDependencyAction = {
type: `CREATE_COMPONENT_DEPENDENCY`,
payload: {
path: `/hi/`,
nodeId: `1.2.3`,
},
}
const action2 = {
const action2: ICreatePageDependencyAction = {
type: `CREATE_COMPONENT_DEPENDENCY`,
payload: {
path: `/hi2/`,
nodeId: `1.2.3`,
},
}
const action3 = {
const action3: ICreatePageDependencyAction = {
type: `CREATE_COMPONENT_DEPENDENCY`,
payload: {
path: `/blog/`,
Expand All @@ -48,14 +50,14 @@ describe(`add page data dependency`, () => {
})
})
it(`lets you add a connection dependency`, () => {
const action = {
const action: ICreatePageDependencyAction = {
type: `CREATE_COMPONENT_DEPENDENCY`,
payload: {
path: `/hi/`,
connection: `Markdown.Remark`,
},
}
const action2 = {
const action2: ICreatePageDependencyAction = {
type: `CREATE_COMPONENT_DEPENDENCY`,
payload: {
path: `/hi2/`,
Expand All @@ -72,15 +74,15 @@ describe(`add page data dependency`, () => {
})
})
it(`removes duplicate paths`, () => {
const action = {
const action: ICreatePageDependencyAction = {
type: `CREATE_COMPONENT_DEPENDENCY`,
payload: {
path: `/hi/`,
nodeId: 1,
connection: `MarkdownRemark`,
},
}
const action2 = {
const action2: ICreatePageDependencyAction = {
type: `CREATE_COMPONENT_DEPENDENCY`,
payload: {
path: `/hi2/`,
Expand All @@ -95,11 +97,13 @@ describe(`add page data dependency`, () => {
// Add different action
state = reducer(state, action2)

expect(state.connections.get(`MarkdownRemark`).size).toEqual(2)
expect(state.nodes.get(1).size).toEqual(2)
expect(
(state.connections.get(`MarkdownRemark`) as Set<string>).size
pieh marked this conversation as resolved.
Show resolved Hide resolved
).toEqual(2)
expect((state.nodes.get(1) as Set<number | string>).size).toEqual(2)
})
it(`lets you add both a node and connection in one action`, () => {
const action = {
const action: ICreatePageDependencyAction = {
type: `CREATE_COMPONENT_DEPENDENCY`,
payload: {
path: `/hi/`,
Expand All @@ -108,7 +112,7 @@ describe(`add page data dependency`, () => {
},
}

let state = reducer(undefined, action)
const state = reducer(undefined, action)

expect(state).toMatchSnapshot()
})
Expand Down
@@ -1,7 +1,12 @@
module.exports = (
state = { nodes: new Map(), connections: new Map() },
action
) => {
import { IGatsbyState, ActionsUnion } from "../types"

export const componentDataDependenciesReducer = (
state: IGatsbyState["componentDataDependencies"] = {
nodes: new Map(),
connections: new Map(),
},
action: ActionsUnion
): IGatsbyState["componentDataDependencies"] => {
switch (action.type) {
case `DELETE_CACHE`:
return { nodes: new Map(), connections: new Map() }
Expand All @@ -12,36 +17,38 @@ module.exports = (

// If this nodeId not set yet.
if (action.payload.nodeId) {
let existingPaths = new Set()
let existingPaths: Set<string> = new Set()
if (state.nodes.has(action.payload.nodeId)) {
existingPaths = state.nodes.get(action.payload.nodeId)
existingPaths = state.nodes.get(action.payload.nodeId) as Set<string>
}
if (!existingPaths.has(action.payload.path || action.payload.id)) {
existingPaths.add(action.payload.path || action.payload.id)
if (!existingPaths.has(action.payload.path)) {
existingPaths.add(action.payload.path)
}
state.nodes.set(action.payload.nodeId, existingPaths)
}

// If this connection not set yet.
if (action.payload.connection) {
let existingPaths = new Set()
let existingPaths: Set<string> = new Set()
if (state.connections.has(action.payload.connection)) {
existingPaths = state.connections.get(action.payload.connection)
existingPaths = state.connections.get(
action.payload.connection
) as Set<string>
}
if (!existingPaths.has(action.payload.path || action.payload.id)) {
existingPaths.add(action.payload.path || action.payload.id)
if (!existingPaths.has(action.payload.path)) {
existingPaths.add(action.payload.path)
}
state.connections.set(action.payload.connection, existingPaths)
}

return state
case `DELETE_COMPONENTS_DEPENDENCIES`:
state.nodes.forEach((val, _key) => {
state.nodes.forEach(val => {
for (const path of action.payload.paths) {
val.delete(path)
}
})
state.connections.forEach((val, _key) => {
state.connections.forEach(val => {
for (const path of action.payload.paths) {
val.delete(path)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/src/redux/reducers/index.js
Expand Up @@ -12,6 +12,7 @@ import { webpackCompilationHashReducer } from "./webpack-compilation-hash"
import { reducer as logReducer } from "gatsby-cli/lib/reporter/redux/reducer"
import { lastAction } from "./last-action"
import { jobsV2Reducer } from "./jobsv2"
import { componentDataDependenciesReducer } from "./component-data-dependencies"

/**
* @property exports.nodesTouched Set<string>
Expand All @@ -28,7 +29,7 @@ module.exports = {
schema: schemaReducer,
pages: pagesReducer,
status: statusReducer,
componentDataDependencies: require(`./component-data-dependencies`),
componentDataDependencies: componentDataDependenciesReducer,
components: require(`./components`),
staticQueryComponents: staticQueryComponentsReducer,
jobs: require(`./jobs`),
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby/src/redux/types.ts
Expand Up @@ -149,7 +149,7 @@ export interface IGatsbyState {
PLUGINS_HASH: Identifier
}
componentDataDependencies: {
nodes: Map<string, Set<string>>
nodes: Map<string | number, Set<string>>
pieh marked this conversation as resolved.
Show resolved Hide resolved
connections: Map<string, Set<string>>
}
components: Map<
Expand Down Expand Up @@ -283,10 +283,10 @@ export interface IRemoveStaleJobV2Action {

export interface ICreatePageDependencyAction {
type: `CREATE_COMPONENT_DEPENDENCY`
plugin: string
plugin?: string
payload: {
path: string
nodeId?: string
nodeId?: string | number
connection?: string
}
}
Expand Down