Skip to content

Commit

Permalink
Prevent the 'complete' event from firing more than once during non-se…
Browse files Browse the repository at this point in the history
…quential processing
  • Loading branch information
coreybutler committed Sep 21, 2020
1 parent 686d230 commit 05a9b6e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ngnjs/queue",
"version": "1.0.0-alpha.3",
"version": "1.0.0-alpha.4",
"description": "A lightweight NGN queue/taskrunner.",
"type": "module",
"main": "src/index.js",
Expand All @@ -23,7 +23,7 @@
"@ngnjs/plugin-debug": "^1.0.0-alpha"
},
"peerDependencies": {
"ngn": ">=2.0.0-alpha.5",
"ngn": "^2.0.0-alpha.5",
"@ngnjs/plugin": ">=1.0.0-alpha.8"
},
"dependencies": {},
Expand Down Expand Up @@ -69,6 +69,9 @@
"buildoption": {
"preserveEntrySignatures": true
},
"autoimport": [
"import ngn from 'ngn'"
],
"alias": {
"ngn": "/node_modules/ngn/index.js",
"@ngnjs/plugin": "/source/@ngnjs/plugin/index.js",
Expand Down
9 changes: 2 additions & 7 deletions src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,12 @@ export default class Queue extends EventEmitter {
this.#timer = setTimeout(() => this.abort(true, activeItem), this.#timeout)
}

this.afterOnce('task.done', this.size, () => {
this.emit('complete')
})

if (!sequential) {
this.afterOnce('task.done', this.size, 'complete')

// Run in parallel
// const TOKEN = Symbol('queue runner')
this.afterOnce('blah.blah', this.size, 'complete')
for (const task of this.#queue.items) {
// task.once('done', () => this.emit('blah.blah'))
// task.once('task.done', () => console.log('here'))
task.run()
}
} else {
Expand Down
11 changes: 10 additions & 1 deletion tests/01-sanity.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,18 @@ test('NGN Queue parallel execution', t => {
x.push('Task 3')
})

let ended = false
tasks.on('complete', function () {
if (ended) {
t.fail("'complete' event fired more than once.")
t.end()
return
}

ended = true
t.expect(3, x.length, 'All functions ran in parallel.')
t.end()

setTimeout(() => t.end(), 300)
})

tasks.run()
Expand Down

0 comments on commit 05a9b6e

Please sign in to comment.