hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | /** |
| 2 | * `Promise.all(items.map(fn))` with at most `limit` calls in flight. For the |
| 3 | * per-row fan-outs the backend has no bulk read for (marks, balances, member |
| 4 | * counts): an unbounded burst of a few hundred GETs trips the backend's rate |
| 5 | * limit and stalls every other request the page makes behind it. |
| 6 | * |
| 7 | * Results keep the input order. A rejection rejects the whole call, like |
| 8 | * `Promise.all` — callers that tolerate a failed row catch inside `fn`. |
| 9 | */ |
| 10 | export async function mapConcurrent<T, R>( |
| 11 | items: readonly T[], |
| 12 | limit: number, |
| 13 | fn: (item: T, index: number) => Promise<R>, |
| 14 | ): Promise<R[]> { |
| 15 | const results = new Array<R>(items.length); |
| 16 | let next = 0; |
| 17 | const worker = async () => { |
| 18 | while (next < items.length) { |
| 19 | const index = next++; |
| 20 | results[index] = await fn(items[index], index); |
| 21 | } |
| 22 | }; |
| 23 | await Promise.all(Array.from({ length: Math.min(Math.max(1, limit), items.length) }, worker)); |
| 24 | return results; |
| 25 | } |
| 26 | |
| 27 | /** In-flight cap shared by the page-level fan-outs. */ |
| 28 | export const FAN_OUT_LIMIT = 6; |