( items: readonly T[], limit: number, fn: (item: T) => Promise<void> )
| 1478 | } |
| 1479 | |
| 1480 | async 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 | |
| 1503 | export interface WorkspaceServiceEvents { |
| 1504 | chat: (event: { workspaceId: string; message: WorkspaceChatMessage }) => void; |
no test coverage detected