| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | /**
|
|---|
| 9 | * Walks a dynamically expanding async work tree with bounded concurrency.
|
|---|
| 10 | * Each processed item may enqueue more items through `push`, allowing callers
|
|---|
| 11 | * to model breadth-first or depth-first discovery without managing the queue
|
|---|
| 12 | * themselves.
|
|---|
| 13 | * @template T
|
|---|
| 14 | * @template {Error} E
|
|---|
| 15 | * @param {Iterable<T>} items initial items
|
|---|
| 16 | * @param {number} concurrency number of items running in parallel
|
|---|
| 17 | * @param {(item: T, push: (item: T) => void, callback: (err?: E) => void) => void} processor worker which pushes more items
|
|---|
| 18 | * @param {(err?: E) => void} callback all items processed
|
|---|
| 19 | * @returns {void}
|
|---|
| 20 | */
|
|---|
| 21 | const processAsyncTree = (items, concurrency, processor, callback) => {
|
|---|
| 22 | const queue = [...items];
|
|---|
| 23 | if (queue.length === 0) return callback();
|
|---|
| 24 | let processing = 0;
|
|---|
| 25 | let finished = false;
|
|---|
| 26 | let processScheduled = true;
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Enqueues a newly discovered item and schedules queue processing when the
|
|---|
| 30 | * current concurrency budget allows more work to start.
|
|---|
| 31 | * @param {T} item item
|
|---|
| 32 | */
|
|---|
| 33 | const push = (item) => {
|
|---|
| 34 | queue.push(item);
|
|---|
| 35 | if (!processScheduled && processing < concurrency) {
|
|---|
| 36 | processScheduled = true;
|
|---|
| 37 | process.nextTick(processQueue);
|
|---|
| 38 | }
|
|---|
| 39 | };
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Handles completion of a single processor call, propagating the first
|
|---|
| 43 | * error and scheduling more queued work when possible.
|
|---|
| 44 | * @param {E | null | undefined} err error
|
|---|
| 45 | */
|
|---|
| 46 | const processorCallback = (err) => {
|
|---|
| 47 | processing--;
|
|---|
| 48 | if (err && !finished) {
|
|---|
| 49 | finished = true;
|
|---|
| 50 | callback(err);
|
|---|
| 51 | return;
|
|---|
| 52 | }
|
|---|
| 53 | if (!processScheduled) {
|
|---|
| 54 | processScheduled = true;
|
|---|
| 55 | process.nextTick(processQueue);
|
|---|
| 56 | }
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | const processQueue = () => {
|
|---|
| 60 | if (finished) return;
|
|---|
| 61 | while (processing < concurrency && queue.length > 0) {
|
|---|
| 62 | processing++;
|
|---|
| 63 | const item = /** @type {T} */ (queue.pop());
|
|---|
| 64 | processor(item, push, processorCallback);
|
|---|
| 65 | }
|
|---|
| 66 | processScheduled = false;
|
|---|
| 67 | if (queue.length === 0 && processing === 0 && !finished) {
|
|---|
| 68 | finished = true;
|
|---|
| 69 | callback();
|
|---|
| 70 | }
|
|---|
| 71 | };
|
|---|
| 72 |
|
|---|
| 73 | processQueue();
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | module.exports = processAsyncTree;
|
|---|