Spaces:
Running
Running
Download src/read-batches.ts from Shoraky/Sporalize-api: direct link, hf CLI and curl.
- Browser
- Download file 857 Bytes
-
https://huggingface.co/spaces/Shoraky/Sporalize-api/resolve/main/src/read-batches.ts
- Command line
-
hf download hf://spaces/Shoraky/Sporalize-api/src/read-batches.ts
-
curl -L -o read-batches.ts https://huggingface.co/spaces/Shoraky/Sporalize-api/resolve/main/src/read-batches.ts
857 Bytes
| /** Bound parallel DB reads so large rosters do not create unbounded fan-out. */ | |
| export async function readBatches<T, R>( | |
| items: readonly T[], | |
| load: (batch: T[]) => PromiseLike<readonly R[]>, | |
| batchSize = 100, | |
| concurrency = 4, | |
| ): Promise<R[]> { | |
| if (!Number.isInteger(batchSize) || batchSize < 1 || | |
| !Number.isInteger(concurrency) || concurrency < 1) throw new Error("Invalid batch configuration"); | |
| const output: R[] = []; | |
| for (let offset = 0; offset < items.length; offset += batchSize * concurrency) { | |
| const group: PromiseLike<readonly R[]>[] = []; | |
| for (let cursor = offset; cursor < Math.min(items.length, offset + batchSize * concurrency); cursor += batchSize) { | |
| group.push(load(items.slice(cursor, cursor + batchSize))); | |
| } | |
| for (const rows of await Promise.all(group)) output.push(...rows); | |
| } | |
| return output; | |
| } | |