website
git clone https://git.pyrossh.dev/website
木 Personal website of pyrossh. Built with astrojs, shiki, vite.
src/_helpers/pool.js
| ada6e53 | 1 | /** |
| ada6e53 | 2 | * Run `mapper` over `items` with at most `limit` concurrent in-flight |
| ada6e53 | 3 | * calls, preserving input order in the result. The git subprocess calls |
| ada6e53 | 4 | * this feeds are I/O-bound (fork+exec, not CPU), so running them |
| ada6e53 | 5 | * concurrently instead of one-at-a-time cuts wall-clock time roughly |
| ada6e53 | 6 | * proportional to `limit` on a multi-core machine. |
| ada6e53 | 7 | */ |
| ada6e53 | 8 | export const mapPool = async (items, limit, mapper) => { |
| ada6e53 | 9 | const results = new Array(items.length); |
| ada6e53 | 10 | let next = 0; |
| ada6e53 | 11 | const workers = Array.from({ length: Math.min(limit, items.length) }, async () => { |
| ada6e53 | 12 | while (next < items.length) { |
| ada6e53 | 13 | const i = next++; |
| ada6e53 | 14 | results[i] = await mapper(items[i], i); |
| ada6e53 | 15 | } |
| ada6e53 | 16 | }); |
| ada6e53 | 17 | await Promise.all(workers); |
| ada6e53 | 18 | return results; |
| ada6e53 | 19 | }; |