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
Original file line number Diff line number Diff line change
@@ -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,19 +74,19 @@ describe(`add page data dependency`, () => {
})
})
it(`removes duplicate paths`, () => {
const action = {
const action: ICreatePageDependencyAction = {
type: `CREATE_COMPONENT_DEPENDENCY`,
payload: {
path: `/hi/`,
nodeId: 1,
nodeId: `1`,
connection: `MarkdownRemark`,
},
}
const action2 = {
const action2: ICreatePageDependencyAction = {
type: `CREATE_COMPONENT_DEPENDENCY`,
payload: {
path: `/hi2/`,
nodeId: 1,
nodeId: `1`,
connection: `MarkdownRemark`,
},
}
Expand All @@ -95,11 +97,11 @@ 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`)?.size).toEqual(2)
expect(state.nodes.get(`1`)?.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 +110,7 @@ describe(`add page data dependency`, () => {
},
}

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

expect(state).toMatchSnapshot()
})
Expand Down
Original file line number Diff line number Diff line change
@@ -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,36 @@ 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)!
}
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)!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is so weird that TS is not smart enough to know that this for sure exists given the check above it, but 🤷‍♂️

}
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
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export interface IRemoveStaleJobV2Action {

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