Skip to content

Commit e374818

Browse files
authored
[json-loader] Only log file events if we're past bootstrap (#4826)
* Don't emit new file node until previous is finished processing This is an experiment to use [xstate](http://davidkpiano.github.io/xstate/docs/#/) to setup state machines to better handle complex state changes as we sometimes have. Ideally this happens in core and then gatsby-source-filesystem just has a simple queue and emits a new file node every time the system returns to idle. In a future refactor we'll do that plus refactor other parts of core that should be handled in a state machine e.g. pages-query-runner.js This PR also reinforced the need for us to implement [tracing](https://github.com/jaegertracing/jaeger) in core / plugins as that'd make it far far easier to understand what's happening and when. * Document state machine and remove extraneous Chokidar states * Remove console.log * Only log file events if we're past bootstrap
1 parent abcb58c commit e374818

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

packages/gatsby-source-filesystem/src/gatsby-node.js

+32-5
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ const fsMachine = Machine({
2626
CHOKIDAR_NOT_READY: {
2727
on: {
2828
CHOKIDAR_READY: "CHOKIDAR_WATCHING",
29+
BOOTSTRAP_FINISHED: "CHOKIDAR_WATCHING_BOOTSTRAP_FINISHED",
2930
},
3031
},
3132
CHOKIDAR_WATCHING: {
3233
on: {
34+
BOOTSTRAP_FINISHED: "CHOKIDAR_WATCHING_BOOTSTRAP_FINISHED",
3335
CHOKIDAR_READY: "CHOKIDAR_WATCHING",
3436
},
3537
},
38+
CHOKIDAR_WATCHING_BOOTSTRAP_FINISHED: {
39+
on: {
40+
CHOKIDAR_READY: "CHOKIDAR_WATCHING_BOOTSTRAP_FINISHED",
41+
},
42+
},
3643
},
3744
},
3845
PROCESSING: {
@@ -152,20 +159,32 @@ See docs here - https://www.gatsbyjs.org/packages/gatsby-source-filesystem/
152159

153160
watcher.on(`add`, path => {
154161
if (currentState.value.CHOKIDAR !== `CHOKIDAR_NOT_READY`) {
155-
reporter.info(`added file at ${path}`)
162+
if (
163+
currentState.value.CHOKIDAR === `CHOKIDAR_WATCHING_BOOTSTRAP_FINISHED`
164+
) {
165+
reporter.info(`added file at ${path}`)
166+
}
156167
createAndProcessNode(path).catch(err => reporter.error(err))
157168
} else {
158169
pathQueue.push(path)
159170
}
160171
})
161172

162173
watcher.on(`change`, path => {
163-
reporter.info(`changed file at ${path}`)
174+
if (
175+
currentState.value.CHOKIDAR === `CHOKIDAR_WATCHING_BOOTSTRAP_FINISHED`
176+
) {
177+
reporter.info(`changed file at ${path}`)
178+
}
164179
createAndProcessNode(path).catch(err => reporter.error(err))
165180
})
166181

167182
watcher.on(`unlink`, path => {
168-
reporter.info(`file deleted at ${path}`)
183+
if (
184+
currentState.value.CHOKIDAR === `CHOKIDAR_WATCHING_BOOTSTRAP_FINISHED`
185+
) {
186+
reporter.info(`file deleted at ${path}`)
187+
}
169188
const node = getNode(createNodeId(path))
170189
// It's possible the file node was never created as sometimes tools will
171190
// write and then immediately delete temporary files to the file system.
@@ -177,15 +196,23 @@ See docs here - https://www.gatsbyjs.org/packages/gatsby-source-filesystem/
177196

178197
watcher.on(`addDir`, path => {
179198
if (currentState.value.CHOKIDAR !== `CHOKIDAR_NOT_READY`) {
180-
reporter.info(`added directory at ${path}`)
199+
if (
200+
currentState.value.CHOKIDAR === `CHOKIDAR_WATCHING_BOOTSTRAP_FINISHED`
201+
) {
202+
reporter.info(`added directory at ${path}`)
203+
}
181204
createAndProcessNode(path).catch(err => reporter.error(err))
182205
} else {
183206
pathQueue.push(path)
184207
}
185208
})
186209

187210
watcher.on(`unlinkDir`, path => {
188-
reporter.info(`directory deleted at ${path}`)
211+
if (
212+
currentState.value.CHOKIDAR === `CHOKIDAR_WATCHING_BOOTSTRAP_FINISHED`
213+
) {
214+
reporter.info(`directory deleted at ${path}`)
215+
}
189216
const node = getNode(createNodeId(path))
190217
deleteNode(node.id, node)
191218
})

0 commit comments

Comments
 (0)