mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-22 05:59:31 +08:00
* Shorten the CI critical path * Bound Effect runner memory and avoid repeated type checks
20 lines
845 B
JavaScript
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;
|
|
}
|