Skip to content

Commit

Permalink
chore(gatsby): Deregister all events to the queue after batch (#20798)
Browse files Browse the repository at this point in the history
The queue was retained for develop mode which led to the same event handlers being registered over and over again, which led to strange artifacts in the output.

With this change the events are aggressively deregistered from the queue when it finishes or fails.

Fixes #20787
  • Loading branch information
pvdz authored and GatsbyJS Bot committed Jan 23, 2020
1 parent 3880394 commit 3e6ebad
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/gatsby/src/query/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const createDevelopQueue = getRunner => {
* Returns a promise that pushes jobs onto queue and resolves onces
* they're all finished processing (or rejects if one or more jobs
* fail)
* Note: queue is reused in develop so make sure to thoroughly cleanup hooks
*/
const processBatch = async (queue, jobs, activity) => {
let numJobs = jobs.length
Expand All @@ -97,11 +98,25 @@ const processBatch = async (queue, jobs, activity) => {
queue.on(`task_finish`, () => activity.tick())
}

const gc = () => {
queue.removeAllListeners(`task_failed`)
queue.removeAllListeners(`drain`)
queue.removeAllListeners(`task_finish`)
queue = null
}

queue
// Note: the first arg is the path, the second the error
.once(`task_failed`, (...err) => reject(err))
// Note: `drain` fires when all tasks _finish_, `empty` fires when queue is empty (but tasks are still running)
.once(`drain`, resolve)
.on(`task_failed`, (...err) => {
gc()
reject(err)
})
// Note: `drain` fires when all tasks _finish_
// `empty` fires when queue is empty (but tasks are still running)
.on(`drain`, () => {
gc()
resolve()
})

jobs.forEach(job => queue.push(job))
})
Expand Down

0 comments on commit 3e6ebad

Please sign in to comment.