taskon
Version:
A simple JavaScript/TypeScript tasks queue that supports dynamic concurrency control
85 lines (84 loc) • 2.85 kB
TypeScript
import { ILogger, LoggerMethodNames } from './logger';
import { PromiseQueue, WaitedTask, TaskStatusUpdateHandler, TaskId, Task } from './type';
export interface TaskQueueCoreProps {
/**
* The logger instance.
* @default The default logger
*/
logger?: ILogger;
/**
* Toggle to log the task queue execution details.
* @default false
*/
verbose?: boolean;
/**
* Maximum concurrency running tasks.
* @default 1
*/
concurrency?: number;
/**
* Toggle to return the error during task execution (resolve to the promise).
* @default false
*/
returnError?: boolean;
/**
* Listener for task status updates.
* Please note, only one listener is allowed.
*/
onTaskStatusUpdate?: TaskStatusUpdateHandler;
}
/**
* Core abstract class for task queue, which is only responsible for serialized
* tasks execution and concurrency dynamic adjustment.
*/
export declare abstract class TaskQueueCore {
/** @internal */
private currentQueueId;
/** @internal */
protected logger: ILogger;
/** @internal */
private verbose;
/** @internal */
protected concurrency: number;
/** @internal */
private returnError;
/** @internal */
protected promiseQueues: Array<PromiseQueue>;
/** @internal */
protected onTaskStatusUpdate: TaskStatusUpdateHandler | undefined;
constructor({ logger, verbose, concurrency, returnError, onTaskStatusUpdate, }?: TaskQueueCoreProps);
/** @internal */
protected abstract _getAvailablePromiseQueue(): PromiseQueue | undefined | null;
/** @internal */
protected abstract _pushTaskToWaitingQueue(task: Task): void;
/** @internal */
protected abstract _getNextTask(previousTaskHasRun: boolean): Task | undefined | null;
/** @internal */
protected abstract _shouldStop(task?: Task): boolean;
/** @internal */
private _initPromiseQueues;
/**
* Adjust the concurrency.
* @param newConcurrency The new concurrency
* @param taskBalancingStrategy The existing running task balancing strategy
*/
adjustConcurrency(newConcurrency: number, taskBalancingStrategy?: 'round-robin'): void;
/**
* Get the current concurrency of the queue
*/
getConcurrency(): number;
/** @internal */
protected _log(config: {
level: LoggerMethodNames;
taskId?: TaskId;
}, ...messages: Array<any>): void;
/** @internal */
private _updateTaskStatus;
/** @internal */
private _runTask;
/** @internal */
protected _cleanTask<TaskType extends Task | WaitedTask>(task: TaskType): TaskType;
/** @internal */
protected _addTask<ReturnType>(task: Task<ReturnType> | WaitedTask<ReturnType>, withTaskClean?: boolean): Promise<ReturnType>;
}
export default TaskQueueCore;