Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
Add a spec for evented queue module
Browse files Browse the repository at this point in the history
 🐿 v2.10.0
  • Loading branch information
i-like-robots committed Dec 7, 2018
1 parent cda9d8d commit f74bb55
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
20 changes: 10 additions & 10 deletions src/event-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/run-parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
61 changes: 61 additions & 0 deletions test/src/event-queue.spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});

0 comments on commit f74bb55

Please sign in to comment.