MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / processInBatches

Function processInBatches

src/utils.ts:653–679  ·  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

651 * @returns Array of results
652 */
653export async function processInBatches<T, R>(
654 items: T[],
655 batchSize: number,
656 processor: (item: T, index: number) => Promise<R>,
657 onBatchComplete?: (completed: number, total: number) => void
658): Promise<R[]> {
659 const results: R[] = [];
660
661 for (let i = 0; i < items.length; i += batchSize) {
662 const batch = items.slice(i, Math.min(i + batchSize, items.length));
663 const batchResults = await Promise.all(
664 batch.map((item, idx) => processor(item, i + idx))
665 );
666 results.push(...batchResults);
667
668 if (onBatchComplete) {
669 onBatchComplete(Math.min(i + batchSize, items.length), items.length);
670 }
671
672 // Allow GC between batches
673 if (global.gc) {
674 global.gc();
675 }
676 }
677
678 return results;
679}
680
681/**
682 * Simple mutex lock for preventing concurrent operations

Callers

nothing calls this directly

Calls 3

mapMethod · 0.80
pushMethod · 0.80
allMethod · 0.65

Tested by

no test coverage detected