Skip to content

Commit

Permalink
Queue processing add events that arrive before chokidar fires ready
Browse files Browse the repository at this point in the history
  • Loading branch information
brentvatne committed Apr 27, 2017
1 parent ec011f4 commit 7366396
Showing 1 changed file with 48 additions and 8 deletions.
56 changes: 48 additions & 8 deletions packages/gatsby-source-filesystem/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ exports.sourceNodes = (
touchNode,
updateSourcePluginStatus,
} = boundActionCreators

let ready = false

updateSourcePluginStatus({
plugin: `source-filesystem --- ${pluginOptions.name}`,
ready: false,
ready,
})

const watcher = chokidar.watch(pluginOptions.path, {
ignored: [
`**/*.un~`,
Expand All @@ -78,26 +82,62 @@ exports.sourceNodes = (
],
})

watcher.on(`add`, path => {
// console.log("Added file at", path)
readFile(path, pluginOptions, (err, file) => {
createNode(file)
// For every path that is reported before the 'ready' event, we throw them
// into a queue and then flush the queue when 'ready' event arrives.
// After 'ready', we handle the 'add' event without putting it into a queue.
let pathQueue = []
const flushPathQueue = onComplete => {
let queue = pathQueue
pathQueue = []

let numPathsProcessed = 0
let numPaths = queue.length

queue.forEach(path => {
readFile(path, pluginOptions, (err, file) => {
createNode(file)

numPathsProcessed++
if (numPathsProcessed === numPaths) {
onComplete()
}
})
})
}

watcher.on(`add`, path => {
if (ready) {
console.log("added file at", path)
readFile(path, pluginOptions, (err, file) => {
createNode(file)
})
} else {
pathQueue.push(path)
}
})
watcher.on(`change`, path => {
console.log("changed file at", path)
readFile(path, pluginOptions, (err, file) => {
// TODO: this should update rather than create
createNode(file)
})
})
watcher.on(`unlink`, path => {
console.log("file deleted at", path)
// TODO: deleteNode is not a function and this throws an error
deleteNode(createId(path))
})
watcher.on(`ready`, () => {
updateSourcePluginStatus({
plugin: `source-filesystem --- ${pluginOptions.name}`,
ready: true,
if (ready) {
return
}

ready = true
flushPathQueue(() => {
updateSourcePluginStatus({
plugin: `source-filesystem --- ${pluginOptions.name}`,
ready,
})
})
})

Expand Down

0 comments on commit 7366396

Please sign in to comment.