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 85c73ce
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/event-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ 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));

if (noItemsRunning) {
this.off('queue:delete', callback);
this.off('delete', callback);
resolve();
}
};

this.on('queue:delete', callback);
this.on('delete', callback);
callback();
});
}
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
63 changes: 63 additions & 0 deletions test/src/event-queue.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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', (item) => {
expect(item).toEqual('foo');
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) => {
expect(item).toEqual('foo');
done();
});

instance.delete('foo');
});
});

describe('#waitFor', () => {
it('resolves when the queue does not contain 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 85c73ce

Please sign in to comment.