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 | * @template T
|
---|
10 | * @template {Error} E
|
---|
11 | * @param {Iterable<T>} items initial items
|
---|
12 | * @param {number} concurrency number of items running in parallel
|
---|
13 | * @param {function(T, function(T): void, function(E=): void): void} processor worker which pushes more items
|
---|
14 | * @param {function(E=): void} callback all items processed
|
---|
15 | * @returns {void}
|
---|
16 | */
|
---|
17 | const processAsyncTree = (items, concurrency, processor, callback) => {
|
---|
18 | const queue = Array.from(items);
|
---|
19 | if (queue.length === 0) return callback();
|
---|
20 | let processing = 0;
|
---|
21 | let finished = false;
|
---|
22 | let processScheduled = true;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * @param {T} item item
|
---|
26 | */
|
---|
27 | const push = item => {
|
---|
28 | queue.push(item);
|
---|
29 | if (!processScheduled && processing < concurrency) {
|
---|
30 | processScheduled = true;
|
---|
31 | process.nextTick(processQueue);
|
---|
32 | }
|
---|
33 | };
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * @param {E | null | undefined} err error
|
---|
37 | */
|
---|
38 | const processorCallback = err => {
|
---|
39 | processing--;
|
---|
40 | if (err && !finished) {
|
---|
41 | finished = true;
|
---|
42 | callback(err);
|
---|
43 | return;
|
---|
44 | }
|
---|
45 | if (!processScheduled) {
|
---|
46 | processScheduled = true;
|
---|
47 | process.nextTick(processQueue);
|
---|
48 | }
|
---|
49 | };
|
---|
50 |
|
---|
51 | const processQueue = () => {
|
---|
52 | if (finished) return;
|
---|
53 | while (processing < concurrency && queue.length > 0) {
|
---|
54 | processing++;
|
---|
55 | const item = /** @type {T} */ (queue.pop());
|
---|
56 | processor(item, push, processorCallback);
|
---|
57 | }
|
---|
58 | processScheduled = false;
|
---|
59 | if (queue.length === 0 && processing === 0 && !finished) {
|
---|
60 | finished = true;
|
---|
61 | callback();
|
---|
62 | }
|
---|
63 | };
|
---|
64 |
|
---|
65 | processQueue();
|
---|
66 | };
|
---|
67 |
|
---|
68 | module.exports = processAsyncTree;
|
---|