Skip to content

Commit dd616ed

Browse files
committed
Don't await batches
1 parent 4ceeda0 commit dd616ed

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

src/handler/utils.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import { ManyQueryResponse, ManyResponse, StatusExecution } from './types';
55
* @ignore
66
*/
77
export const chunkArray = (list, size) => {
8+
const clonedList = [...list];
89
const results: any = [];
9-
while (list.length) {
10-
results.push(list.splice(0, size));
10+
while (clonedList.length) {
11+
results.push(clonedList.splice(0, size));
1112
}
1213
return results;
1314
};
1415

1516
/**
1617
* @ignore
1718
*/
18-
function* processBatch(items, fn, metadata, extra, options): IterableIterator<StatusExecution> {
19+
function* processBatch(items, fn, metadata, extra, options): IterableIterator<Promise<StatusExecution>> {
1920
const clonedItems = [...items];
2021
for (const items of clonedItems) {
2122
yield fn(items, metadata, extra, options)
@@ -31,14 +32,18 @@ export const batchProcessQueue =
3132
<T = any>(metadata: ModelMetadata) =>
3233
async (items: unknown[], fn: unknown, extra: Record<string, unknown> = {}, options: any = {}, throttle = 100) => {
3334
const chunks = chunkArray([...items], throttle);
34-
const chunkPromises = chunks.map((data) => Promise.resolve(data));
3535
const result: ManyResponse<T> = { success: 0, match_number: items.length, errors: [], data: [] };
36-
for await (const chunk of chunkPromises) {
37-
for await (const r of processBatch(chunk, fn, metadata, extra, options)) {
36+
for (const chunk of chunks) {
37+
const promises: Promise<StatusExecution>[] = [];
38+
for (const promise of processBatch(chunk, fn, metadata, extra, options)) {
39+
promises.push(promise);
40+
}
41+
const batchResults = await Promise.all(promises);
42+
for (const r of batchResults) {
3843
if (r.status === 'FAILURE') {
3944
result.errors.push(r);
4045
} else {
41-
result.success = result.success + 1;
46+
result.success += 1;
4247
if (r.payload) {
4348
result.data?.push(r.payload as T);
4449
}

0 commit comments

Comments
 (0)