From 2822210a69bfc7a072fd3df11f04c0c6a1ba48d4 Mon Sep 17 00:00:00 2001 From: Matt Hinchliffe Date: Wed, 13 Feb 2019 15:05:58 +0000 Subject: [PATCH 1/3] =?UTF-8?q?Refactor=20run=20parallel=20execution=20mod?= =?UTF-8?q?ule=20to=20use=20async/await=20=20=F0=9F=90=BF=20v2.10.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/run-parallel.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/run-parallel.js b/src/run-parallel.js index 5e698de..a7cf0ff 100644 --- a/src/run-parallel.js +++ b/src/run-parallel.js @@ -9,22 +9,21 @@ module.exports = (tasks = [], concurrency = 1, preserveOrder = false) => { logger.info(`Executing up to ${concurrency} tasks at a time`); return Promise.all( - tasks.map(({ pkg, apply }) => { + tasks.map(async ({ pkg, apply }) => { queue.add(pkg.name); - return semaphore - .acquire() - .then(() => { - // wait for any dependencies still in the queue to finish - return preserveOrder ? queue.waitFor(pkg.allDependencies) : null; - }) - .then(() => { - return apply(); - }) - .then(() => { - queue.delete(pkg.name); - return semaphore.release(); - }); + await semaphore.acquire(); + + // wait for any dependencies still in the queue to finish + if (preserveOrder) { + await queue.waitFor(pkg.allDependencies); + } + + await apply(); + + queue.delete(pkg.name); + + semaphore.release(); }) ); }; From 3fccd983fb00ff1ae34af0c3d396a05d2daeb5e6 Mon Sep 17 00:00:00 2001 From: Matt Hinchliffe Date: Wed, 13 Feb 2019 15:08:06 +0000 Subject: [PATCH 2/3] =?UTF-8?q?Rename=20evented=20queue=20"queue"=20to=20"?= =?UTF-8?q?waiting"=20to=20avoid=20typing=20"queue.queue"=20=20?= =?UTF-8?q?=F0=9F=90=BF=20v2.10.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/evented-queue.js | 10 +++++----- test/src/evented-queue.spec.js | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/evented-queue.js b/src/evented-queue.js index 1c832c6..04c7a04 100644 --- a/src/evented-queue.js +++ b/src/evented-queue.js @@ -3,17 +3,17 @@ const EventEmitter = require('events'); class EventedQueue extends EventEmitter { constructor() { super(); - this.queue = new Set(); + this.waiting = new Set(); } add(item) { - this.queue.add(item); + this.waiting.add(item); this.emit('add', item); return this; } delete(item) { - this.queue.delete(item); + this.waiting.delete(item); this.emit('delete', item); return this; } @@ -21,9 +21,9 @@ class EventedQueue extends EventEmitter { waitFor(items = []) { return new Promise((resolve) => { const callback = () => { - const itemsRunning = items.some((item) => this.queue.has(item)); + const itemsWaiting = items.some((item) => this.waiting.has(item)); - if (!itemsRunning) { + if (!itemsWaiting) { this.removeListener('delete', callback); resolve(); } diff --git a/test/src/evented-queue.spec.js b/test/src/evented-queue.spec.js index 0a4feb0..5752fa6 100644 --- a/test/src/evented-queue.spec.js +++ b/test/src/evented-queue.spec.js @@ -10,7 +10,7 @@ describe('src/evented-queue', () => { describe('#add', () => { it('adds the given item to the queue', () => { instance.add('foo'); - expect(instance.queue.size).toEqual(1); + expect(instance.waiting.size).toEqual(1); }); it('emits an event when items are added to the queue', (done) => { @@ -25,10 +25,10 @@ describe('src/evented-queue', () => { describe('#delete', () => { it('removes the given item to the queue', () => { instance.add('foo'); - expect(instance.queue.size).toEqual(1); + expect(instance.waiting.size).toEqual(1); instance.delete('foo'); - expect(instance.queue.size).toEqual(0); + expect(instance.waiting.size).toEqual(0); }); it('emits an event when items are removed from the queue', (done) => { @@ -50,7 +50,7 @@ describe('src/evented-queue', () => { .add('baz'); instance.waitFor(['foo', 'bar', 'baz']).then(() => { - expect(instance.queue.size).toEqual(0); + expect(instance.waiting.size).toEqual(0); done(); }); From 05dcd0101d87cf02fd06e7aa4865c833ad185ecd Mon Sep 17 00:00:00 2001 From: Matt Hinchliffe Date: Wed, 13 Feb 2019 15:14:16 +0000 Subject: [PATCH 3/3] =?UTF-8?q?Rename=20evented=20queue=20"waitFor"=20meth?= =?UTF-8?q?od=20"waitBehind"=20to=20maintain=20queue=20metaphor=20=20?= =?UTF-8?q?=F0=9F=90=BF=20v2.10.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/evented-queue.js | 2 +- src/run-parallel.js | 2 +- test/src/evented-queue.spec.js | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/evented-queue.js b/src/evented-queue.js index 04c7a04..47628b8 100644 --- a/src/evented-queue.js +++ b/src/evented-queue.js @@ -18,7 +18,7 @@ class EventedQueue extends EventEmitter { return this; } - waitFor(items = []) { + waitBehind(items = []) { return new Promise((resolve) => { const callback = () => { const itemsWaiting = items.some((item) => this.waiting.has(item)); diff --git a/src/run-parallel.js b/src/run-parallel.js index a7cf0ff..6897ca1 100644 --- a/src/run-parallel.js +++ b/src/run-parallel.js @@ -16,7 +16,7 @@ module.exports = (tasks = [], concurrency = 1, preserveOrder = false) => { // wait for any dependencies still in the queue to finish if (preserveOrder) { - await queue.waitFor(pkg.allDependencies); + await queue.waitBehind(pkg.allDependencies); } await apply(); diff --git a/test/src/evented-queue.spec.js b/test/src/evented-queue.spec.js index 5752fa6..4277483 100644 --- a/test/src/evented-queue.spec.js +++ b/test/src/evented-queue.spec.js @@ -42,14 +42,14 @@ describe('src/evented-queue', () => { }); }); - describe('#waitFor', () => { + describe('#waitBehind', () => { it('resolves when the queue no longer contains any of the given items', (done) => { instance .add('foo') .add('bar') .add('baz'); - instance.waitFor(['foo', 'bar', 'baz']).then(() => { + instance.waitBehind(['foo', 'bar', 'baz']).then(() => { expect(instance.waiting.size).toEqual(0); done(); });