UNPKG

tiny-essentials

Version:

Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.

110 lines 4.55 kB
export default TinyPromiseQueue; export type QueuedTask = { /** * - The async task to execute. */ task: (...args: any[]) => Promise<any> | Promise<any>; /** * - The resolve function from the Promise. */ resolve: (value: any) => any; /** * - The reject function from the Promise. */ reject: (reason?: any) => any; /** * - Optional identifier for the task. */ id?: string | undefined; /** * - Optional marker for the task. */ marker?: string | null | undefined; /** * - Optional delay (in ms) before the task is executed. */ delay?: number | null | undefined; }; /** * @typedef {Object} QueuedTask * @property {(...args: any[]) => Promise<any>|Promise<any>} task - The async task to execute. * @property {(value: any) => any} resolve - The resolve function from the Promise. * @property {(reason?: any) => any} reject - The reject function from the Promise. * @property {string|undefined} [id] - Optional identifier for the task. * @property {string|null|undefined} [marker] - Optional marker for the task. * @property {number|null|undefined} [delay] - Optional delay (in ms) before the task is executed. */ /** * A queue system for managing and executing asynchronous tasks sequentially, one at a time. * * Tasks can be delayed, reordered, canceled, and processed in strict order. The queue ensures that each task * is executed after the previous one finishes, and any task can be skipped or canceled if needed. * * @class */ declare class TinyPromiseQueue { /** * Returns whether the queue is currently processing a task. * * @returns {boolean} */ isRunning(): boolean; /** * Returns the index of a task by its ID. * * @param {string} id The ID of the task to locate. * @returns {number} The index of the task in the queue, or -1 if not found. */ getIndexById(id: string): number; /** * Returns a list of IDs for all tasks currently in the queue. * * @returns {{ index: number, id: string }[]} An array of task IDs currently queued. */ getQueuedIds(): { index: number; id: string; }[]; /** * Reorders a task in the queue from one index to another. * * @param {number} fromIndex The current index of the task to move. * @param {number} toIndex The index where the task should be placed. */ reorderQueue(fromIndex: number, toIndex: number): void; /** * Inserts a point in the queue where subsequent tasks will be grouped and executed together in a Promise.all. * If the queue is currently empty, behaves like a regular promise. * * @param {(...args: any[]) => Promise<any>|Promise<any>} task A function that returns a Promise. * @param {string} [id] Optional ID to identify the task in the queue. * @returns {Promise<any>} A Promise that resolves or rejects with the result of the task once it's processed. * @throws {Error} Throws if param is invalid. */ enqueuePoint(task: (...args: any[]) => Promise<any> | Promise<any>, id?: string): Promise<any>; /** * Adds a new async task to the queue and ensures it runs in order after previous tasks. * Optionally, a delay can be added before the task is executed. * * If the task is canceled before execution, it will be rejected with the message: * "The function was canceled on TinyPromiseQueue." * * @param {(...args: any[]) => Promise<any>|Promise<any>} task A function that returns a Promise to be executed sequentially. * @param {number|null} [delay] Optional delay (in ms) before the task is executed. * @param {string} [id] Optional ID to identify the task in the queue. * @returns {Promise<any>} A Promise that resolves or rejects with the result of the task once it's processed. * @throws {Error} Throws if param is invalid. */ enqueue(task: (...args: any[]) => Promise<any> | Promise<any>, delay?: number | null, id?: string): Promise<any>; /** * Cancels a scheduled delay and removes the task from the queue. * Adds the ID to a blacklist so the task is skipped if already being processed. * * @param {string} id The ID of the task to cancel. * @returns {boolean} True if a delay was cancelled and the task was removed. * @throws {Error} Throws if `id` is not a string. */ cancelTask(id: string): boolean; #private; } //# sourceMappingURL=TinyPromiseQueue.d.mts.map