Skip to content

Commit

Permalink
chore(gatsby): convert component-data-dependencies to typescript (#24028
Browse files Browse the repository at this point in the history
)

* Convert component-data-dependencies

* Convert test file

* Convert import statement

* Update snapshot

* Refactor to use existing types

* Use provided union type

* Improve reducer name

* Remove nodeId number type

* Convert nodeId to string &  use optional chain

* Prefer non-null instead of casting

* missed one spot ;)

Co-authored-by: Michal Piechowiak <[email protected]>
  • Loading branch information
Kornil and pieh authored May 14, 2020
1 parent 8248392 commit 4c79511
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
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)!
}
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

0 comments on commit 4c79511

Please sign in to comment.