UNPKG

@shutterstock/chunker

Version:

Calls a callback before size would be exceeded or when count limit is reached

109 lines 3.71 kB
/** * Function that returns the user-defined size of an item * * @param item Item to have size computed * @returns User-defined size of item * @template T Type of item to have size computed */ export type SizerFunc<T> = (item: T) => Promise<number> | number; /** * Function that writes items to a destination * * @param items Items to be written * @returns Result of writing items * @template T Type of items to be written */ export type WriterFunc<T> = (items: T[]) => Promise<void>; type Errors = (string | { [key: string]: any; } | Error)[]; /** * Options for the Chunker constructor */ export interface IChunkerOptions<T> { /** * User-defined size before which `writer` should be called */ sizeLimit: number; /** * Number of pending items requiring that `writer` be called */ countLimit: number; /** * Function that returns user-defined size of an item */ sizer: SizerFunc<T>; /** * Function that writes the pending items when `sizeLimit` or `countLimit` would be exceeded. * This is not a `mapper` as it does not return a result at all. * If the results need to be passed along, add them to an `IterableQueueMapper` for example. */ writer: WriterFunc<T>; } /** * Collects items up to `countLimit`, calling `writer` before `sizeLimit` would be exceeded. * * @remarks * * Always call {@link onIdle} when done to ensure that the last `writer` call is made. * * @template T Type of items to be chunked */ export declare class Chunker<T> { private readonly _writer; private readonly _iterableQueue; private readonly _errors; private _pendingSize; private _pendingItems; private readonly _backgroundWriter; /** * Collects items up to `countLimit`, calling `writer` before `sizeLimit` would be exceeded. * * @remarks * * Always call {@link onIdle} when done to ensure that the last `writer` call is made. * * @template T Type of items to be chunked * @param options Chunker options * @param options.sizeLimit User-defined size before (not to exceed) which `writer` should be called * @param options.countLimit User-defined size at which `writer` should be called * @param options.sizer Function that returns user-defined size of an item * @param options.writer Function that writes the pending items when `sizeLimit` or `countLimit` would be exceeded. * This is not a `mapper` as it does not return a result at all. * For example, this may write the items to a file or send them to a service. * If the results need to be passed along, add them to an `IterableQueueMapper` for example. */ constructor(options: IChunkerOptions<T>); /** * Sum of the user-defined size of all pending items */ get pendingSize(): number; /** * Number of items pending */ get pendingCount(): number; /** * Accumulated errors from background flushes */ get errors(): Errors; /** * Wait for all background writes to finish. * MUST be called before exit to ensure no lost writes. */ onIdle(): Promise<void>; /** * Calls `writer` for any pending items and clears pending items queue. * @returns Result from `writer` function or `undefined` if no items pending */ private flush; /** * Adds an item to the pending queue, flushing the queue before * adding the item if the new item would cause the item limit * or size limit to be exceeded. * * @param item Item to be added to the queue */ enqueue(item: T): Promise<void>; } export {}; //# sourceMappingURL=chunker.d.ts.map