1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-19 13:29:41 +02:00
portainer/app/react/common/processItemsInBatches.ts
cmeng 3924d0f081
Some checks failed
Test / test-server (map[arch:amd64 platform:linux]) (push) Has been cancelled
ci / build_images (map[arch:amd64 platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:amd64 platform:windows version:1809]) (push) Has been cancelled
ci / build_images (map[arch:amd64 platform:windows version:ltsc2022]) (push) Has been cancelled
ci / build_images (map[arch:arm platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:arm64 platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:ppc64le platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:s390x platform:linux version:]) (push) Has been cancelled
/ triage (push) Has been cancelled
Lint / Run linters (push) Has been cancelled
Test / test-client (push) Has been cancelled
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Has been cancelled
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Has been cancelled
Test / test-server (map[arch:arm64 platform:linux]) (push) Has been cancelled
ci / build_manifests (push) Has been cancelled
fix(deletion): delete objects batch by batch EE-7084 (#11833)
2024-05-16 14:34:50 +12:00

30 lines
1.1 KiB
TypeScript

/**
* Type definition for the callback function used in processItemsInBatches.
* It should accept an item from the array as its first argument
* and additional arguments (if any) as its second argument, and should return a Promise.
*/
type ProcessItemsCallback<T, Args extends never[]> = (
item: T,
...args: Args
) => Promise<void>;
/**
* Asynchronously processes an array of items in batches.
* @param items An array of items to be processed.
* @param processor A callback function of type ProcessItemsCallback that will be called for each item in the array.
* @param batchSize The maximum number of items to process in each batch. Defaults to 100 if not provided.
* @param args Additional arguments to be passed to the callback function for each item.
*/
export async function processItemsInBatches<T, Args extends never[]>(
items: T[],
processor: ProcessItemsCallback<T, Args>,
batchSize = 100,
...args: Args
): Promise<void> {
while (items.length) {
const batch = items.splice(0, batchSize);
const batchPromises = batch.map((item) => processor(item, ...args));
await Promise.all(batchPromises);
}
}