refactor(core): unify bounded concurrency runner

This commit is contained in:
Peter Steinberger
2026-02-18 16:47:03 +00:00
parent 2b8f1bade0
commit 82cb185881
4 changed files with 143 additions and 51 deletions

View File

@@ -2,6 +2,7 @@ import crypto from "node:crypto";
import fsSync from "node:fs";
import fs from "node:fs/promises";
import path from "node:path";
import { runTasksWithConcurrency } from "../utils/run-with-concurrency.js";
export type MemoryFileEntry = {
path: string;
@@ -301,35 +302,12 @@ export async function runWithConcurrency<T>(
tasks: Array<() => Promise<T>>,
limit: number,
): Promise<T[]> {
if (tasks.length === 0) {
return [];
}
const resolvedLimit = Math.max(1, Math.min(limit, tasks.length));
const results: T[] = Array.from({ length: tasks.length });
let next = 0;
let firstError: unknown = null;
const workers = Array.from({ length: resolvedLimit }, async () => {
while (true) {
if (firstError) {
return;
}
const index = next;
next += 1;
if (index >= tasks.length) {
return;
}
try {
results[index] = await tasks[index]();
} catch (err) {
firstError = err;
return;
}
}
const { results, firstError, hasError } = await runTasksWithConcurrency({
tasks,
limit,
errorMode: "stop",
});
await Promise.allSettled(workers);
if (firstError) {
if (hasError) {
throw firstError;
}
return results;