import type { Task, Tasks } from "./Task"; export type Readonly = { readonly [K in keyof T]: T[K]; }; export type Maybe = T | undefined; export type TaskID = number | string; export type Done = (result?: T) => void; export type TaskReturn = T | void; export type TaskWithDone = ( done: Done, id: TaskID ) => TaskReturn | Promise>; export type TasksWithDone = TaskWithDone[]; export type TasksList = { readonly running: Tasks; readonly pending: Tasks; readonly completed: Tasks; }; export type TasksCount = { total: number; completed: number; pending: number; running: number; }; export enum TaskStatus { PENDING = "pending", RUNNING = "running", CANCELLED = "cancelled", DONE = "done", } export enum RemovalMethods { ALL = "all", BY_INDEX = "by-index", RANGE = "range", FIRST = "first", LAST = "last", } export enum AdditionMethods { FIRST = "first", LAST = "last", AT_INDEX = "at-index", MULTIPLE_FIRST = "multiple-first", MULTIPLE_LAST = "multiple-range", } export type RunnerDuration = { start: number; end: number; total: number; }; export type HookDefaults = { tasks: TasksList; count: TasksCount; duration: RunnerDuration }; type HookFn< T = any, Data extends Record = Record > = ( args: Data extends Record ? Readonly> : Readonly & Data> ) => void; export type OnStart = HookFn; export type OnPause = HookFn; export type onDestroy = HookFn; export type OnAdd = HookFn< T, { method: AdditionMethods; } >; export type OnRemove = HookFn< T, { method: RemovalMethods; removedTasks: Tasks } >; export type OnRun = HookFn< T, { task: Task; } >; export type OnDone = HookFn< T, { task: Task; result?: T; } >; export type OnEnd = HookFn; export enum RunnerEvents { START = "onStart", PAUSE = "onPause", DESTROY = "onDestroy", ADD = "onAdd", REMOVE = "onRemove", RUN = "onRun", DONE = "onDone", END = "onEnd", } export type RunnerHooks = { [RunnerEvents.START]: OnStart; [RunnerEvents.PAUSE]: OnPause; [RunnerEvents.DESTROY]: onDestroy; [RunnerEvents.ADD]: OnAdd; [RunnerEvents.REMOVE]: OnRemove; [RunnerEvents.RUN]: OnRun; [RunnerEvents.DONE]: OnDone; [RunnerEvents.END]: OnEnd; }; export type RunnerDefaultOptions = RunnerHooks & { concurrency: number; name: string | (() => string); }; export type RunnerOptions = { [K in keyof RunnerDefaultOptions]: RunnerDefaultOptions[K]; };