UNPKG

seq-prom

Version:

A small library to allow for sequential operations on an array using Promises

76 lines (75 loc) 3.2 kB
/** * Created by adrianbrowning */ type ItemError<T> = { item: T; reason: string | Error; }; type ItemSuccess<T, RT> = { item: T; result: RT; }; export type Func_CB<T, RT> = (item: T, extra: { resolve: (value: (RT | PromiseLike<RT>)) => void; reject: Rejecter; self: SeqPromiseClass<RT, NoInfer<T>>; }) => Promise<RT> | void | RT; export type Func_ERR<T> = (item: T, reason: ItemError<void>["reason"]) => void; type SeqPromOptionsSchema<RT, T> = { list: ReadonlyArray<T>; cb: Func_CB<T, RT>; finalCB?: (errors: Array<ItemError<T>>, items: ReadonlyArray<ItemSuccess<T, RT>>) => any; errorCB?: Func_ERR<T>; useBatch?: boolean; autoStart?: boolean; size?: number; context?: any; }; type Resolver<T = unknown, RT = unknown> = (value?: (SeqPromResult<T, RT> | PromiseLike<SeqPromResult<T, RT>>)) => void; type Rejecter = (reason?: string | Error) => void; type SeqPromResult<T, RT> = [Array<ItemError<T>>, Array<ItemSuccess<T, RT>>]; declare class SeqPromiseClass<ReturnType, ItemType, TOptions extends SeqPromOptionsSchema<ReturnType, ItemType> = SeqPromOptionsSchema<ReturnType, ItemType>> { list: Array<ItemType>; cb: TOptions["cb"]; finalCB: TOptions["finalCB"]; errorCB: TOptions["errorCB"]; useBatch: boolean; size: number; promise: Promise<SeqPromResult<ItemType, ReturnType>>; _globalPromiseResolver: Resolver<ItemType, ReturnType>; _stopped: boolean; _errors: Array<ItemError<ItemType>>; _responses: Array<ItemSuccess<ItemType, ReturnType>>; /** * SeqPromise class processes items sequentially using promises * @template ItemType - Type of items in the list to process * @template ReturnType - Type returned by the callback * * @param options - The options for setting the chain * @param options.list - The list of items to iterate through asynchronously * @param options.cb - Function called for each item, returning a result or promise * @param options.size - Either - The size of "simulated" thread pool (default: 1)\n - size of the batch (default: 1) * @param options.autoStart - Will start processing immediately if true (default: false) * @param options.useBatch - Switches from Stream mode to batch (default: false) * @param options.context - Context to run functions in (this binding) * @param options.finalCB - Function called when all processing is complete * @param options.errorCB - Function called when an error occurs processing an item */ constructor(options: SeqPromOptionsSchema<ReturnType, ItemType>); start(): this; stop(): void; } /** * Factory function to create a SeqPromise instance * @template T - Type of items in the list to process * @template RT - Type returned by the callback (default: any) * * @param options - Configuration options for sequential processing * @returns A configured SeqPromiseClass instance that can be started */ declare function SeqPromise<RT, T>(options: SeqPromOptionsSchema<RT, T>): SeqPromiseClass<RT, T>; declare namespace SeqPromise { var prototype: SeqPromiseClass<any, any, any>; } export default SeqPromise; export { SeqPromise };