UNPKG

@typescript-package/queue

Version:

A lightweight TypeScript library for managing various queue and stack structures.

96 lines (95 loc) 4.29 kB
import { Ability as Processable } from "@typescript-package/state"; import { Processing } from "./processing.class"; import { ErrorCallback, ProcessCallback } from "../type"; /** * @description A class designed to manage and execute a collection of asynchronous tasks with concurrently control or synchronous tasks. * @export * @class Tasks * @template Type * @extends {Processable} */ export declare class Tasks<Type = any, Concurrency extends number = number> extends Processable { #private; /** * @description The maximum number of elements that can be processed concurrently. * @public * @readonly * @type {Concurrency} */ get concurrency(): Concurrency; /** * @description Returns the processed elements. * @public * @readonly * @type {Set<Type>} */ get processed(): Set<Type>; /** * @description Returns the `Processing` object that contains active tasks. * @public * @readonly * @type {Processing<Type, Concurrency>} */ get processing(): Processing; /** * Creates an instance of `Tasks`. * @constructor * @param {Concurrency} concurrency */ /** * Creates an instance of `Tasks`. * @constructor * @param {boolean} enabled Enable initially `Tasks` functionality. * @param {Concurrency} concurrency The maximum number of elements that can be processed concurrently. */ constructor(enabled: boolean, concurrency: Concurrency); /** * @description Set the `Tasks` to debug state. * @public */ debug(): this; /** * @description Runs asynchronous single processing on the `element`. * @public * @async * @param {Type} element The element to process. * @param {ProcessCallback<Type>} callbackFn The callback function to process the element. * @param {ErrorCallback<Type>} [onError] An optional error handler. * @returns {Promise<void>} */ asyncProcess(element: Type, callbackFn: ProcessCallback<Type>, onError?: ErrorCallback<Type>, onProcessed?: ProcessCallback<Type>): Promise<void>; /** * @description Starts asynchronous processing elements with concurrency control. * @public * @async * @param {Iterable<Type>} elements The elements to process. * @param {ProcessCallback<Type>} callbackFn The function to process each element. * @param {?ErrorCallback<Type>} [onError] An optional error handler. * @param {('default' | 'race')} [method='default'] * @returns {Promise<void>} */ asyncRun(elements: Iterable<Type>, callbackFn: ProcessCallback<Type>, onError?: ErrorCallback<Type>, onProcessed?: ProcessCallback<Type>, method?: 'all' | 'default' | 'race'): Promise<Set<Type>>; /** * @description Runs a synchronous processing on the provided `element` using the `callbackFn`. * If an `onError` callback is provided, it will handle any errors encountered during processing. * @param {(Type | undefined)} element The element to be processed. * @param {ProcessCallback<Type>} callbackFn A function that processes the element synchronously. * @param {?ErrorCallback<Type>} [onError] An optional callback function to handle errors during processing. * @returns {this} The current instance for method chaining. */ process(element: Type | undefined, callbackFn: ProcessCallback<Type>, onError?: ErrorCallback<Type>, onProcessed?: ProcessCallback<Type>): this; /** * @description Runs the provided `callbackFn` synchronously on each element in the `elements` iterable. * If an `onError` callback is provided, it will handle errors encountered during processing. * @public * @param {Iterable<Type>} elements An iterable collection of elements to be processed. * @param {ProcessCallback<Type>} callbackFn A function that will process each element synchronously. * @param {?ErrorCallback<Type>} [onError] Optional callback for handling errors that occur during processing. */ run(elements: Iterable<Type>, callbackFn: ProcessCallback<Type>, onError?: ErrorCallback<Type>, onProcessed?: ProcessCallback<Type>): void; /** * @description Unset the `Tasks` from debug state. * @public */ unDebug(): this; }