MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / parallelLimit

Method parallelLimit

packages/db/src/buffers/base-buffer.ts:215–247  ·  view source on GitHub ↗

* Run `fn` over `items` with a bounded concurrency. Preserves order of * `results`. If any worker throws, the helper propagates the first error * (consistent with `Promise.all`); other in-flight promises run to * completion but their resolved values/errors are discarded.

(
    items: T[],
    fn: (item: T, index: number) => Promise<R>,
    concurrency: number = this.chInsertConcurrency
  )

Source from the content-addressed store, hash-verified

213 * completion but their resolved values/errors are discarded.
214 */
215 protected async parallelLimit<T, R>(
216 items: T[],
217 fn: (item: T, index: number) => Promise<R>,
218 concurrency: number = this.chInsertConcurrency
219 ): Promise<R[]> {
220 if (items.length === 0) {
221 return [];
222 }
223 if (concurrency <= 1 || items.length === 1) {
224 const out: R[] = [];
225 for (let i = 0; i < items.length; i++) {
226 out.push(await fn(items[i] as T, i));
227 }
228 return out;
229 }
230 const results = new Array<R>(items.length);
231 let nextIndex = 0;
232 const worker = async (): Promise<void> => {
233 while (true) {
234 const idx = nextIndex++;
235 if (idx >= items.length) {
236 return;
237 }
238 results[idx] = await fn(items[idx] as T, idx);
239 }
240 };
241 const workers = Array.from(
242 { length: Math.min(concurrency, items.length) },
243 () => worker()
244 );
245 await Promise.all(workers);
246 return results;
247 }
248
249 /**
250 * Subclasses call this from within onFlush to record what they actually

Callers 4

processBufferMethod · 0.80
processBufferMethod · 0.80
processBufferMethod · 0.80
processBufferMethod · 0.80

Calls 3

fnFunction · 0.85
pushMethod · 0.45
fromMethod · 0.45

Tested by

no test coverage detected