@typescript-package/queue
Version:
A lightweight TypeScript library for managing various queue and stack structures.
75 lines (74 loc) • 2.72 kB
TypeScript
import { Queue } from "./queue.abstract";
import { Processing } from "./processing.class";
import { ErrorCallback, ProcessCallback } from "../type";
/**
* @description A task queue that processes elements concurrently with a specified concurrency limit.
* @export
* @class TaskQueue
* @template Type
* @template {number} [Concurrency=number]
* @template {number} [Size=number]
* @extends {Queue<Type, Size>}
*/
export declare class TaskQueue<Type, Concurrency extends number = number, Size extends number = number> extends Queue<Type, Size> {
#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 child class.
* @constructor
* @param {Concurrency} [concurrency=1 as Concurrency]
* @param {Size} [size=Infinity as Size]
* @param {...Type[]} elements
*/
constructor(concurrency?: Concurrency, size?: Size, ...elements: Type[]);
/**
* @description Checks whether the queue processing is completed.
* @public
* @returns {boolean}
*/
isCompleted(): boolean;
/**
* @description Waits for all elements in the queue to be processed and returns the set of processed elements.
* @public
* @async
* @returns {Promise<Set<Type>>}
*/
onCompleted(): Promise<Set<Type>>;
/**
* @description Starts asynchronous processing queue elements with concurrency control.
* @public
* @async
* @param {ProcessCallback<Type>} callbackFn The function to process each element.
* @param {?ErrorCallback<Type>} [onError] An optional error handler.
* @returns {Promise<Set<Type>>}
*/
asyncRun(callbackFn: ProcessCallback<Type>, onError?: ErrorCallback<Type>): Promise<Set<Type>>;
/**
* @description Starts processing elements in the queue using the provided callback function.
* @public
* @param {(element: Type) => void} callbackFn A function to process each element in the queue.
* @param {?(element: Type, error: unknown) => void} [onError] An optional function to handle the error.
* @returns {void) => void}
*/
run(callbackFn: (element: Type) => void, onError?: (element: Type, error: unknown) => void): void;
}