taskon
Version:
A simple JavaScript/TypeScript tasks queue that supports dynamic concurrency control
105 lines (104 loc) • 3.98 kB
TypeScript
import TaskQueueCore, { TaskQueueCoreProps } from './core';
import { Task, TaskId, TaskPrioritizationMode, TaskPriority, TaskStatus, TaskStatusUpdateHandler, WaitedTask } from './type';
export interface TaskQueueBaseProps extends TaskQueueCoreProps {
/**
* Toggle to make getTask() and getAllTasks() available to retrieve task
* details.
* @default false
*/
memorizeTasks?: boolean;
/**
* Toggle to stop the queue when error happens during task execution.
* @default true
*/
stopOnError?: boolean;
/**
* Toggle to use default incremental task id.
* @default true
*/
defaultIncrementalTaskId?: boolean;
/**
* Pending task prioritization mode. It affects how the task queue
* picks the next task to be executed.
*
* @augments head - Pick the first task in the waiting queue
*
* @augments tail - Pick the last task in the waiting queue
*
* @augments head-with-truncation - Pick the first task in the waiting queue
* and clear the corresponding waiting queue. If the picked task is in the
* normal waiting queue, then only the normal queue will be cleared; same
* thing happens to the prioritized waiting queue
*
* @augments tail-with-truncation - Pick the last task in the waiting queue
* and clear the corresponding waiting queue. If the picked task is in the
* normal waiting queue, then only the normal queue will be cleared; same
* thing happens to the prioritized waiting queue
*/
taskPrioritizationMode?: TaskPrioritizationMode;
}
/**
* Base task queue with abstract methods implementation and base public methods.
*/
export declare class TaskQueueBase extends TaskQueueCore {
/** @internal */
protected memorizeTasks: boolean;
/** @internal */
protected stopOnError: boolean;
/** @internal */
protected defaultIncrementalTaskId: boolean;
/** @internal */
protected retrying: boolean;
/** @internal */
protected stopped: boolean;
/** @internal */
protected promiseQueueCapacity: number;
/** @internal */
protected taskPrioritizationMode: TaskPrioritizationMode;
/** @internal */
protected tasksWaitingQueue: Array<WaitedTask>;
/** @internal */
protected prioritizedTasksWaitingQueue: Array<WaitedTask>;
/** @internal */
protected failedRetryableTaskQueue: Array<Task>;
/** @internal */
protected taskLookup: Record<TaskId, Task>;
constructor({ memorizeTasks, stopOnError, defaultIncrementalTaskId, taskPrioritizationMode, ...rest }?: TaskQueueBaseProps);
/** @internal */
protected _getAvailablePromiseQueue(): import("./type").PromiseQueue | null;
/** @internal */
protected _pushTaskToWaitingQueue(task: WaitedTask): void;
/** @internal */
protected _getNextTask(previousTaskHasRun: boolean): Task<any> | null | undefined;
/** @internal */
protected _shouldStop(task?: Task): boolean;
/** @internal */
protected _createTask<ReturnType>(callback: () => ReturnType | Promise<ReturnType>, taskId?: TaskId, onStatusUpdate?: TaskStatusUpdateHandler<ReturnType>, priority?: TaskPriority): {
taskId: TaskId;
callback: () => ReturnType | Promise<ReturnType>;
createdAt: number;
status: TaskStatus;
onStatusUpdate: TaskStatusUpdateHandler<ReturnType> | undefined;
priority: TaskPriority;
};
/**
* Start the queue execution.
*/
start(): void;
/**
* Stop the queue execution.
* Please note, the current ongoing task will not be stopped immediately.
*/
stop(): void;
/**
* Retry running the queue with failed tasks.
* Please note, this method will be effective only when marking "stopOnError"
* as "true" for the queue.
*/
retry(): void;
/**
* Check if the queue is manually stopped.
*/
isManuallyStopped(): boolean;
}
export default TaskQueueBase;