MCPcopy
hub / github.com/coder/mux / forEachWithConcurrencyLimit

Function forEachWithConcurrencyLimit

src/node/services/workspaceService.ts:1480–1501  ·  view source on GitHub ↗
(
  items: readonly T[],
  limit: number,
  fn: (item: T) => Promise<void>
)

Source from the content-addressed store, hash-verified

1478}
1479
1480async function forEachWithConcurrencyLimit<T>(
1481 items: readonly T[],
1482 limit: number,
1483 fn: (item: T) => Promise<void>
1484): Promise<void> {
1485 assert(Number.isInteger(limit) && limit > 0, "Concurrency limit must be a positive integer");
1486
1487 let nextIndex = 0;
1488 const workerCount = Math.min(limit, items.length);
1489
1490 const workers = Array.from({ length: workerCount }, async () => {
1491 while (true) {
1492 const index = nextIndex++;
1493 if (index >= items.length) {
1494 return;
1495 }
1496 await fn(items[index]);
1497 }
1498 });
1499
1500 await Promise.all(workers);
1501}
1502
1503export interface WorkspaceServiceEvents {
1504 chat: (event: { workspaceId: string; message: WorkspaceChatMessage }) => void;

Callers 1

Calls 1

assertFunction · 0.50

Tested by

no test coverage detected