Files
Joel Alan 325c1bd92e chore: eval 模块 TS 化 (#71)
* chore: eval 模块 TS 化

* chore: 代码优化

* chore: 修改 bm25 引入路径
2026-04-23 11:14:02 +08:00

38 lines
1.0 KiB
TypeScript

/**
* Parallel Executor — p-limit wrapper preserving parallelMap() interface.
*/
import pLimit from 'p-limit';
export interface ParallelMapOptions<T> {
concurrency?: number;
onProgress?: (info: { done: number; total: number; result: T | null }) => void;
onError?: (err: Error, item: unknown, index: number) => void;
}
export async function parallelMap<T, R>(
items: T[],
processor: (item: T, index: number) => Promise<R>,
opts: ParallelMapOptions<R> = {}
): Promise<(R | null)[]> {
const { concurrency = 5, onProgress, onError } = opts;
const limit = pLimit(concurrency);
let done = 0;
const tasks = items.map((item, i) =>
limit(async () => {
try {
const result = await processor(item, i);
onProgress?.({ done: ++done, total: items.length, result });
return result;
} catch (err) {
onError?.(err as Error, item, i);
onProgress?.({ done: ++done, total: items.length, result: null });
return null;
}
})
);
return Promise.all(tasks);
}