MCPcopy Create free account
hub / github.com/Noumena-Network/code / parallelWithLimit

Function parallelWithLimit

src/services/api/filesApi.ts:281–308  ·  view source on GitHub ↗

* Execute promises with limited concurrency * * @param items - Items to process * @param fn - Async function to apply to each item * @param concurrency - Maximum concurrent operations * @returns Results in the same order as input items

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

Source from the content-addressed store, hash-verified

279 * @returns Results in the same order as input items
280 */
281async function parallelWithLimit<T, R>(
282 items: T[],
283 fn: (item: T, index: number) => Promise<R>,
284 concurrency: number,
285): Promise<R[]> {
286 const results: R[] = new Array(items.length)
287 let currentIndex = 0
288
289 async function worker(): Promise<void> {
290 while (currentIndex < items.length) {
291 const index = currentIndex++
292 const item = items[index]
293 if (item !== undefined) {
294 results[index] = await fn(item, index)
295 }
296 }
297 }
298
299 // Start workers up to the concurrency limit
300 const workers: Promise<void>[] = []
301 const workerCount = Math.min(concurrency, items.length)
302 for (let i = 0; i < workerCount; i++) {
303 workers.push(worker())
304 }
305
306 await Promise.all(workers)
307 return results
308}
309
310/**
311 * Downloads all file attachments for a session in parallel

Callers 2

downloadSessionFilesFunction · 0.85
uploadSessionFilesFunction · 0.85

Calls 1

workerFunction · 0.85

Tested by

no test coverage detected