(concurrency)
| 8 | |
| 9 | /** @param {number} concurrency */ |
| 10 | export function queue(concurrency) { |
| 11 | /** @type {Task[]} */ |
| 12 | const tasks = []; |
| 13 | |
| 14 | let current = 0; |
| 15 | |
| 16 | // TODO: Whenever Node >21 is minimum supported version, we can use `Promise.withResolvers` to avoid this ceremony |
| 17 | /** @type {(value?: any) => void} */ |
| 18 | let fulfil; |
| 19 | |
| 20 | /** @type {(error: Error) => void} */ |
| 21 | let reject; |
| 22 | |
| 23 | let closed = false; |
| 24 | |
| 25 | const done = new Promise((f, r) => { |
| 26 | fulfil = f; |
| 27 | reject = r; |
| 28 | }); |
| 29 | |
| 30 | done.catch(() => { |
| 31 | // this is necessary in case a catch handler is never added |
| 32 | // to the done promise by the user |
| 33 | }); |
| 34 | |
| 35 | function dequeue() { |
| 36 | if (current < concurrency) { |
| 37 | const task = tasks.shift(); |
| 38 | |
| 39 | if (task) { |
| 40 | current += 1; |
| 41 | const promise = Promise.resolve(task.fn()); |
| 42 | |
| 43 | void promise |
| 44 | .then(task.fulfil, (err) => { |
| 45 | task.reject(err); |
| 46 | reject(err); |
| 47 | }) |
| 48 | .then(() => { |
| 49 | current -= 1; |
| 50 | dequeue(); |
| 51 | }); |
| 52 | } else if (current === 0) { |
| 53 | closed = true; |
| 54 | fulfil(); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return { |
| 60 | /** @param {() => any} fn */ |
| 61 | add: (fn) => { |
| 62 | if (closed) throw new Error('Cannot add tasks to a queue that has ended'); |
| 63 | |
| 64 | const promise = new Promise((fulfil, reject) => { |
| 65 | tasks.push({ fn, fulfil, reject }); |
| 66 | }); |
| 67 |
no test coverage detected