Files
scriptc/packages/runtime-pack-common/scripts/parallel-map.mjs
Chris Tate cc75597869 Shorten the CI critical path (#605)
* Shorten the CI critical path

* Bound Effect runner memory and avoid repeated type checks
2026-10-01 22:18:40 -05:00

20 lines
845 B
JavaScript

/** Bounded build work that preserves manifest order and drains in-flight
* compilers before propagating a failure to the staging-directory cleanup. */
export async function parallelMap(items, width, task) {
if (!Number.isInteger(width) || width < 1) throw new Error("invalid build concurrency");
const results = new Array(items.length);
let next = 0;
let failed = false;
const workers = Array.from({ length: Math.min(width, items.length) }, async () => {
while (!failed && next < items.length) {
const index = next++;
try { results[index] = await task(items[index]); }
catch (error) { failed = true; throw error; }
}
});
const settled = await Promise.allSettled(workers);
const failure = settled.find((result) => result.status === "rejected");
if (failure) throw failure.reason;
return results;
}