arehs
Version:
The arehs ensures the best possible large batch processing, which is oriented towards event-driven chunk processing.
90 lines (89 loc) • 3.27 kB
TypeScript
export declare class Arehs<T, R> {
private readonly data;
private readonly results;
private concurrency;
private inFlightTasks;
private processedEntries;
private processor;
private eventEmitter;
private promiseExecution;
private timeout;
private error;
private allowStopOnFailure;
private retryLimitCount;
/**
* Constructor that initializes an instance of the class.
* Takes input data (data), a parallelism limit (concurrency), and a data processing function (processor),
* and a timeout (timeout) in milliseconds.
*
* @param data
* @param concurrency
* @param processor
* @param timeout
*/
constructor(data: T[], concurrency: number, processor: (data: T) => Promise<R>, timeout: number);
/**
* The purpose of the create method is to create an Arehs instance from a specific array of data.
*
* @param data
*/
static create<T>(data: T[]): Arehs<T, unknown>;
/**
* Methods that set the value for parallelism and return the current instance.
*
* @param concurrency
*/
withConcurrency(concurrency: number): this;
/**
* Set the timeout time.
* The default value is 0. If it's greater than 0, the option works, and an error is thrown if the operation takes longer than the timeout time(ms).
*
* @param ms
*/
timeoutLimit(ms?: number): this;
/**
* Set whether to stop on failure.
*
* @param stopOnFailure
*/
stopOnFailure(stopOnFailure: boolean): this;
/**
* Set a limit on the number of retries on failure.
*
* @param retryLimit
*/
retryLimit(retryLimit: number): this;
/**
* Calling the mapAsync function starts the process of asynchronously processing the input data and returning the results.
* If the stopOnFailure option is set to true, the function stops processing and emits appropriate events.
* This can be useful for handling transient errors or ensuring data processing resilience.
* Also, if the retryLimit option is greater than 0, you can set a limit on the number of retries on failure.
*
* @param processor The function responsible for processing each data item. If allowStopOnFailure is true, retry logic is applied.
* @returns A Promise that resolves to an array of results after processing all data items.
*/
mapAsync(processor: (data: T) => Promise<R>): Promise<R[]>;
/**
* Method that waits for the currently in-progress task to not exceed the concurrency limit.
* When the task is complete, raise the TASK_COMPLETED event.
*
* @private
*/
private _waitForTaskCompletion;
/**
* Runs an asynchronous task that processes each data item and stores the results in the results array.
* When the task is complete, raise the TASK_COMPLETED event.
*
* @param data
* @private
*/
private _executeTask;
/**
* A method that executes an asynchronous operation and collects the result.
* If a promiseExecution already exists, it returns that promise
* otherwise, it creates a new promise to start the asynchronous operation.
*
* @private
*/
private _executeProcess;
}