From f74bb551c7ac5596ad75c0bb7388ff0420af791d Mon Sep 17 00:00:00 2001 From: Matt Hinchliffe Date: Fri, 7 Dec 2018 14:31:22 +0000 Subject: [PATCH] Add a spec for evented queue module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐿 v2.10.0 --- src/event-queue.js | 20 ++++++------ src/run-parallel.js | 2 +- test/src/event-queue.spec.js | 61 ++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 test/src/event-queue.spec.js diff --git a/src/event-queue.js b/src/event-queue.js index fe40e62..5f775dc 100644 --- a/src/event-queue.js +++ b/src/event-queue.js @@ -8,28 +8,28 @@ class EventQueue extends EventEmitter { add(item) { this.queue.add(item); - this.emit('queue:add', item); + this.emit('add', item); } delete(item) { this.queue.delete(item); - this.emit('queue:delete', item); + this.emit('delete', item); } - // Resolves when the queue contains none of the given items - done(items = []) { + waitFor(items = []) { return new Promise((resolve) => { - const callback = () => { - const noItemsRunning = items.every((item) => !this.queue.has(item)); + const callback = (item) => { + const itemsRunning = items.some((item) => this.queue.has(item)); - if (noItemsRunning) { - this.off('queue:delete', callback); + if (!itemsRunning) { + this.off('delete', callback); resolve(); } }; - this.on('queue:delete', callback); - callback(); + this.on('delete', callback); + + callback(null); }); } } diff --git a/src/run-parallel.js b/src/run-parallel.js index bd95d15..eb27a38 100644 --- a/src/run-parallel.js +++ b/src/run-parallel.js @@ -16,7 +16,7 @@ module.exports = (tasks = [], concurrency = 1) => { // Queue the package now to maintain running order... queue.add(pkg.name); // ...but wait for any dependencies in the queue to finish - return queue.done(pkg.allDependencies); + return queue.waitFor(pkg.allDependencies); }) .then(() => { return apply(); diff --git a/test/src/event-queue.spec.js b/test/src/event-queue.spec.js new file mode 100644 index 0000000..d15a37c --- /dev/null +++ b/test/src/event-queue.spec.js @@ -0,0 +1,61 @@ +const Subject = require('../../src/event-queue'); + +describe('src/event-queue', () => { + let instance; + + beforeEach(() => { + instance = new Subject(); + }); + + describe('#add', () => { + it('adds the given item to the queue', () => { + instance.add('foo'); + expect(instance.queue.size).toEqual(1); + }); + + it('emits an event when items are added to the queue', (done) => { + instance.on('add', () => { + done(); + }); + + instance.add('foo'); + }); + }); + + describe('#delete', () => { + it('removes the given item to the queue', () => { + instance.add('foo'); + expect(instance.queue.size).toEqual(1); + + instance.delete('foo'); + expect(instance.queue.size).toEqual(0); + }); + + it('emits an event when items are removed from the queue', (done) => { + instance.add('foo'); + + instance.on('delete', (item) => { + done(); + }); + + instance.delete('foo'); + }); + }); + + describe('#waitFor', () => { + it('resolves when the queue no longer contains any of the given items', (done) => { + instance.add('foo'); + instance.add('bar'); + instance.add('baz'); + + instance.waitFor(['foo', 'bar', 'baz']).then(() => { + expect(instance.queue.size).toEqual(0); + done(); + }); + + instance.delete('foo'); + instance.delete('bar'); + instance.delete('baz'); + }); + }); +});