Skip to content

Commit

Permalink
chore(gatsby): Migrate reducers/nodes-by-type to TypeScript (#24419)
Browse files Browse the repository at this point in the history
* Change nodes-by-type file extension from JS to TS

* Update reducer index import

* Update IDeleteNodeAction

* Update packages/gatsby/src/redux/reducers/nodes-by-type.ts

* properly handle undefined map.get usage

Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
Co-authored-by: Blaine Kasten <blainekasten@gmail.com>
  • Loading branch information
4 people authored May 26, 2020
1 parent 0050837 commit 1c5580a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/gatsby/src/redux/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { nodeReducer } from "./nodes"
const nodesByType = require(`./nodes-by-type`)
import { nodesByTypeReducer } from "./nodes-by-type"
import { pagesReducer } from "./pages"
import { redirectsReducer } from "./redirects"
import { schemaReducer } from "./schema"
Expand All @@ -25,7 +25,7 @@ import { jobsReducer } from "./jobs"
module.exports = {
program: require(`./program`),
nodes: nodeReducer,
nodesByType: nodesByType,
nodesByType: nodesByTypeReducer,
resolvedNodesCache: require(`./resolved-nodes`),
nodesTouched: nodesTouchedReducer,
lastAction: lastAction,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
const getNodesOfType = (node, state) => {
import { IGatsbyNode, IGatsbyState, ActionsUnion } from "../types"

const getNodesOfType = (
node: IGatsbyNode,
state: IGatsbyState["nodesByType"]
): Map<string, IGatsbyNode> => {
const { type } = node.internal
if (!state.has(type)) {
state.set(type, new Map())
}
return state.get(type)
const nodeByType = state.get(type)

if (!nodeByType) {
throw new Error(
`An error occurred finding a node by it's type. This is likely a bug in gatsby. If you experience this error please open an issue.`
)
}

return nodeByType
}

module.exports = (state = new Map(), action) => {
export const nodesByTypeReducer = (
state: IGatsbyState["nodesByType"] = new Map(),
action: ActionsUnion
): IGatsbyState["nodesByType"] => {
switch (action.type) {
case `DELETE_CACHE`:
return new Map()
Expand Down
4 changes: 1 addition & 3 deletions packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,7 @@ export interface IAddChildNodeToParentNodeAction {

export interface IDeleteNodeAction {
type: `DELETE_NODE`
payload: {
id: Identifier
}
payload: IGatsbyNode
}

export interface IDeleteNodesAction {
Expand Down

0 comments on commit 1c5580a

Please sign in to comment.