diff options
author | Alex Early <alexander.early@gmail.com> | 2018-07-09 11:24:06 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-09 11:24:06 -0700 |
commit | 1f3925eb5b97bad4da8d225eb61ce75c728d6760 (patch) | |
tree | 229be5f072db9918cdd5eb8e3ba20f373a0eb45e | |
parent | 17471a579b0650e54b322120b1e6ca85de0846c5 (diff) | |
download | async-1f3925eb5b97bad4da8d225eb61ce75c728d6760.tar.gz |
feat: Iterable queues (#1556)
* feat: iterable queues
* docs
-rw-r--r-- | lib/internal/DoublyLinkedList.js | 16 | ||||
-rw-r--r-- | lib/internal/queue.js | 3 | ||||
-rw-r--r-- | lib/queue.js | 14 | ||||
-rw-r--r-- | test/queue.js | 19 |
4 files changed, 44 insertions, 8 deletions
diff --git a/lib/internal/DoublyLinkedList.js b/lib/internal/DoublyLinkedList.js index 4444bcb..39f88ac 100644 --- a/lib/internal/DoublyLinkedList.js +++ b/lib/internal/DoublyLinkedList.js @@ -60,14 +60,16 @@ export default class DLL { return this.tail && this.removeLink(this.tail); } - toArray () { - var arr = Array(this.length); - var curr = this.head; - for(var idx = 0; idx < this.length; idx++) { - arr[idx] = curr.data; - curr = curr.next; + toArray() { + return [...this] + } + + *[Symbol.iterator] () { + var cur = this.head + while (cur) { + yield cur.data + cur = cur.next } - return arr; } remove (testFn) { diff --git a/lib/internal/queue.js b/lib/internal/queue.js index a811080..4c1f5cc 100644 --- a/lib/internal/queue.js +++ b/lib/internal/queue.js @@ -87,6 +87,9 @@ export default function queue(worker, concurrency, payload) { var isProcessing = false; var q = { _tasks: new DLL(), + *[Symbol.iterator] () { + yield* q._tasks[Symbol.iterator]() + }, concurrency, payload, saturated: noop, diff --git a/lib/queue.js b/lib/queue.js index 00b90ce..0fa7b96 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -3,7 +3,7 @@ import wrapAsync from './internal/wrapAsync'; /** * A queue of tasks for the worker function to complete. - * @typedef {Object} QueueObject + * @typedef {Iterable} QueueObject * @memberOf module:ControlFlow * @property {Function} length - a function returning the number of items * waiting to be processed. Invoke with `queue.length()`. @@ -53,6 +53,18 @@ import wrapAsync from './internal/wrapAsync'; * @property {Function} kill - a function that removes the `drain` callback and * empties remaining tasks from the queue forcing it to go idle. No more tasks * should be pushed to the queue after calling this function. Invoke with `queue.kill()`. + * + * @example + * const q = aync.queue(worker, 2) + * q.push(item1) + * q.push(item2) + * q.push(item3) + * // queues are iterable, spread into an array to inspect + * const items = [...q] // [item1, item2, item3] + * // or use for of + * for (let item of q) { + * console.log(item) + * } */ /** diff --git a/test/queue.js b/test/queue.js index 3eeeac8..2ac5077 100644 --- a/test/queue.js +++ b/test/queue.js @@ -799,4 +799,23 @@ describe('queue', function(){ done(); } }); + + it('should be iterable', (done) => { + var q = async.queue((data, cb) => { + if (data === 3) { + q.push(6) + expect([...q]).to.eql([4, 5, 6]); + } + async.setImmediate(cb); + }); + + q.push([1, 2, 3, 4, 5]); + + expect([...q]).to.eql([1, 2, 3, 4, 5]); + + q.drain = function () { + expect([...q]).to.eql([]); + done(); + } + }) }); |