(items, concurrency, processor, callback)
| 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; |
no test coverage detected