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

Fix tracking nodes with filtering/running sift #4024

Merged
merged 2 commits into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/gatsby/src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { joinPath } = require(`../utils/path`)
const {
getNode,
hasNodeChanged,
trackSubObjectsToRootNodeId,
trackInlineObjectsInRootNode,
} = require(`./index`)
const { store } = require(`./index`)
import * as joiSchemas from "../joi-schemas/joi"
Expand Down Expand Up @@ -484,7 +484,7 @@ actions.createNode = (node: any, plugin?: Plugin, traceId?: string) => {
)
}

trackSubObjectsToRootNodeId(node)
trackInlineObjectsInRootNode(node)

const oldNode = getNode(node.id)

Expand Down
36 changes: 28 additions & 8 deletions packages/gatsby/src/redux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,45 @@ const emitter = mitt()
// Reducers
const reducers = require(`./reducers`)

// Parent node tracking
// Root node tracking

/**
* Map containing links between inline objects or arrays
* and Node that contains them
* @type {Object.<(Object|Array),string>}
*/
const rootNodeMap = new WeakMap()
const addParentToSubObjects = (data, parentId) => {

/**
* Add link between passed data and Node. This function shouldn't be used
* directly. Use higher level `trackInlineObjectsInRootNode`
* @see trackInlineObjectsInRootNode
* @param {(Object|Array)} data Inline object or array
* @param {string} nodeId Id of node that contains data passed in first parameter
*/
const addRootNodeToInlineObject = (data, nodeId) => {
if (_.isPlainObject(data) || _.isArray(data)) {
_.each(data, o => addParentToSubObjects(o, parentId))
rootNodeMap.set(data, parentId)
_.each(data, o => addRootNodeToInlineObject(o, nodeId))
rootNodeMap.set(data, nodeId)
}
}

const trackSubObjectsToRootNodeId = node => {
/**
* Adds link between inline objects/arrays contained in Node object
* and that Node object.
* @param {Node} node Root Node
*/
const trackInlineObjectsInRootNode = node => {
_.each(node, (v, k) => {
// Ignore the node internal object.
if (k === `internal`) {
return
}
addParentToSubObjects(v, node.parent)
addRootNodeToInlineObject(v, node.id)
})
return node
}
exports.trackSubObjectsToRootNodeId = trackSubObjectsToRootNodeId
exports.trackInlineObjectsInRootNode = trackInlineObjectsInRootNode

// Read from cache the old node data.
let initialState = {}
Expand All @@ -40,7 +60,7 @@ try {
)

_.each(initialState.nodes, node => {
trackSubObjectsToRootNodeId(node)
trackInlineObjectsInRootNode(node)
})
} catch (e) {
// ignore errors.
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/schema/run-sift.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { connectionFromArray } = require(`graphql-skip-limit`)
const { createPageDependency } = require(`../redux/actions/add-page-dependency`)
const prepareRegex = require(`./prepare-regex`)
const Promise = require(`bluebird`)
const { trackInlineObjectsInRootNode } = require(`../redux`)

function awaitSiftField(fields, node, k) {
const field = fields[k]
Expand Down Expand Up @@ -109,6 +110,7 @@ module.exports = ({
return Promise.all(
nodes.map(node => resolveRecursive(node, fieldsToSift, type.getFields()))
).then(myNodes => {
myNodes = myNodes.map(trackInlineObjectsInRootNode)
if (!connection) {
const index = _.isEmpty(siftArgs)
? 0
Expand Down