import { type MaybePromise, type Func, type AsyncFunc } from '../index.ts';
type BatchFunction = (AsyncFunc | Func);
export type OnChunkParam = {
index: number;
total: number;
items: T[];
processedCount: number;
remainingCount: number;
completionPercent: number;
};
export type BatchOptions = {
items: T[];
concurrency?: number;
failureMode?: 'abort' | 'continue';
onError?: (error: Error, item: T, itemIndex: number) => MaybePromise;
onStart?: (total: number) => MaybePromise;
onEnd?: (results: BatchResult[]) => MaybePromise;
onChunkStart?: (params: OnChunkParam) => MaybePromise;
onChunkEnd?: (params: OnChunkParam) => MaybePromise;
};
export type BatchResult = {
result: R | null;
error: Error | null;
item: T;
index: number;
itemIndex: number;
};
/**
* Processes items in batches with configurable concurrency and error handling.
*
* Provides progress tracking through lifecycle callbacks and supports
* different failure modes for robust batch processing.
*
* @example
*
* const items = [1, 2, 3, 4, 5]; // Array of arguments
*
* const handleOneItem = async (item: number) => {
* console.log(item);
* };
*
* await batch(handleOneItem, {
* concurrency: 10,
* items,
* failureMode: 'continue',
* onError: (error, item) => {
* console.error(error, item);
* },
* onStart: (total) => {
* console.log(`Starting batch with ${total} chunks`);
* },
* onEnd: (results) => {
* console.log(`Batch completed with ${results.length} results`);
* },
* onChunkStart: ({ index, total, items }) => {
* console.log(`Starting chunk ${index + 1}/${total}`);
* },
* onChunkEnd: ({ index, total, items }) => {
* console.log(`Finished chunk ${index + 1}/${total}`);
* }
* });
*/
export declare const batch: (fn: BatchFunction<[T], R>, options: BatchOptions) => Promise[]>;
export {};