* Run `fn` over `items` with a bounded concurrency. Preserves order of * `results`. If any worker throws, the helper propagates the first error * (consistent with `Promise.all`); other in-flight promises run to * completion but their resolved values/errors are discarded.
(
items: T[],
fn: (item: T, index: number) => Promise<R>,
concurrency: number = this.chInsertConcurrency
)
| 213 | * completion but their resolved values/errors are discarded. |
| 214 | */ |
| 215 | protected async parallelLimit<T, R>( |
| 216 | items: T[], |
| 217 | fn: (item: T, index: number) => Promise<R>, |
| 218 | concurrency: number = this.chInsertConcurrency |
| 219 | ): Promise<R[]> { |
| 220 | if (items.length === 0) { |
| 221 | return []; |
| 222 | } |
| 223 | if (concurrency <= 1 || items.length === 1) { |
| 224 | const out: R[] = []; |
| 225 | for (let i = 0; i < items.length; i++) { |
| 226 | out.push(await fn(items[i] as T, i)); |
| 227 | } |
| 228 | return out; |
| 229 | } |
| 230 | const results = new Array<R>(items.length); |
| 231 | let nextIndex = 0; |
| 232 | const worker = async (): Promise<void> => { |
| 233 | while (true) { |
| 234 | const idx = nextIndex++; |
| 235 | if (idx >= items.length) { |
| 236 | return; |
| 237 | } |
| 238 | results[idx] = await fn(items[idx] as T, idx); |
| 239 | } |
| 240 | }; |
| 241 | const workers = Array.from( |
| 242 | { length: Math.min(concurrency, items.length) }, |
| 243 | () => worker() |
| 244 | ); |
| 245 | await Promise.all(workers); |
| 246 | return results; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Subclasses call this from within onFlush to record what they actually |
no test coverage detected