MCPcopy
hub / github.com/colbymchenry/codegraph / processInBatches

Function processInBatches

src/utils.ts:344–370  ·  view source on GitHub ↗
(
  items: T[],
  batchSize: number,
  processor: (item: T, index: number) => Promise<R>,
  onBatchComplete?: (completed: number, total: number) => void
)

Source from the content-addressed store, hash-verified

342 * @returns Array of results
343 */
344export async function processInBatches<T, R>(
345 items: T[],
346 batchSize: number,
347 processor: (item: T, index: number) => Promise<R>,
348 onBatchComplete?: (completed: number, total: number) => void
349): Promise<R[]> {
350 const results: R[] = [];
351
352 for (let i = 0; i < items.length; i += batchSize) {
353 const batch = items.slice(i, Math.min(i + batchSize, items.length));
354 const batchResults = await Promise.all(
355 batch.map((item, idx) => processor(item, i + idx))
356 );
357 results.push(...batchResults);
358
359 if (onBatchComplete) {
360 onBatchComplete(Math.min(i + batchSize, items.length), items.length);
361 }
362
363 // Allow GC between batches
364 if (global.gc) {
365 global.gc();
366 }
367 }
368
369 return results;
370}
371
372/**
373 * Simple mutex lock for preventing concurrent operations

Callers

nothing calls this directly

Calls 1

allMethod · 0.65

Tested by

no test coverage detected