Skip to content

Commit

Permalink
[1.0] Factor out node sourcing code into its own module (#1083)
Browse files Browse the repository at this point in the history
* Factor out sourcing nodes into its own module

* Add explicit mapping for highlight.js cause jest can't find it for whatever reason
  • Loading branch information
KyleAMathews authored Jun 2, 2017
1 parent e0f2b89 commit a64ddc0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 53 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ module.exports = {
"^graphql-skip-limit$": `<rootDir>/packages/graphql-skip-limit/src/index.js`,
"^gatsby-plugin-sharp$": `<rootDir>/packages/gatsby-plugin-sharp/src/index.js`,
"^gatsby/dist/redux/actions$": `<rootDir>/packages/gatsby/src/redux/actions.js`,
"^highlight.js$": `<rootDir>/node_modules/highlight.js/lib/index.js`,
},
}
7 changes: 6 additions & 1 deletion packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = async (program: any) => {
console.log(
`lib/bootstrap/index.js time since started:`,
process.uptime(),
"sec"
`sec`
)

// Fix program directory path for windows env
Expand Down Expand Up @@ -221,6 +221,11 @@ data

console.timeEnd(`copy gatsby files`)

// Source nodes
console.time(`initial sourcing and transforming nodes`)
await require(`../utils/source-nodes`)()
console.timeEnd(`initial sourcing and transforming nodes`)

// Create Schema.
await require(`../schema`)()

Expand Down
54 changes: 2 additions & 52 deletions packages/gatsby/src/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@
const _ = require(`lodash`)
const { GraphQLSchema, GraphQLObjectType } = require(`graphql`)

const apiRunner = require(`../utils/api-runner-node`)
const buildNodeTypes = require(`./build-node-types`)
const buildNodeConnections = require(`./build-node-connections`)
const { store, emitter, getNode } = require(`../redux`)
const { boundActionCreators } = require(`../redux/actions`)
const { deleteNodes } = boundActionCreators
const { store } = require(`../redux`)

async function buildSchema() {
console.time(`building schema`)
module.exports = async () => {
const typesGQL = await buildNodeTypes()
const connections = buildNodeConnections(_.values(typesGQL))

Expand All @@ -35,49 +31,3 @@ async function buildSchema() {

return
}

module.exports = () =>
new Promise(resolve => {
console.time(`initial sourcing and transforming nodes`)
apiRunner(`sourceNodes`, {
traceId: `initial-sourceNodes`,
waitForCascadingActions: true,
}).then(() => {
console.timeEnd(`initial sourcing and transforming nodes`)
const state = store.getState()

// Garbage collect stale data nodes before creating the
// schema.
const touchedNodes = Object.keys(state.nodesTouched)
const staleNodes = _.values(state.nodes).filter(node => {
// Find the root node.
let rootNode = node
let whileCount = 0
while (
rootNode.parent &&
getNode(rootNode.parent) !== undefined &&
whileCount < 101
) {
rootNode = getNode(rootNode.parent)
whileCount += 1
if (whileCount > 100) {
console.log(
`It looks like you have a node that's set its parent as itself`,
rootNode
)
}
}

return !_.includes(touchedNodes, rootNode.id)
})

if (staleNodes.length > 0) {
deleteNodes(staleNodes.map(n => n.id))
}

// Resolve promise once the schema is built.
buildSchema().then(() => {
resolve()
})
})
})
43 changes: 43 additions & 0 deletions packages/gatsby/src/utils/source-nodes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const _ = require(`lodash`)

const apiRunner = require(`./api-runner-node`)
const { store, getNode } = require(`../redux`)
const { boundActionCreators } = require(`../redux/actions`)
const { deleteNodes } = boundActionCreators

module.exports = async () => {
await apiRunner(`sourceNodes`, {
traceId: `initial-sourceNodes`,
waitForCascadingActions: true,
})

const state = store.getState()

// Garbage collect stale data nodes
const touchedNodes = Object.keys(state.nodesTouched)
const staleNodes = _.values(state.nodes).filter(node => {
// Find the root node.
let rootNode = node
let whileCount = 0
while (
rootNode.parent &&
getNode(rootNode.parent) !== undefined &&
whileCount < 101
) {
rootNode = getNode(rootNode.parent)
whileCount += 1
if (whileCount > 100) {
console.log(
`It looks like you have a node that's set its parent as itself`,
rootNode
)
}
}

return !_.includes(touchedNodes, rootNode.id)
})

if (staleNodes.length > 0) {
deleteNodes(staleNodes.map(n => n.id))
}
}

0 comments on commit a64ddc0

Please sign in to comment.