/** * @module @xmcl/task */ export declare class CancelledError extends Error { constructor(); } export declare enum TaskState { Idle = 0, Running = 1, Cancelled = 2, Paused = 3, Succeed = 4, Failed = 5 } export interface Task { readonly id: number; readonly name: string; readonly param: Record; readonly progress: number; readonly total: number; readonly from: string | undefined; readonly to: string | undefined; readonly path: string; readonly isCancelled: boolean; readonly isPaused: boolean; readonly isDone: boolean; readonly isRunning: boolean; readonly state: TaskState; readonly context: TaskContext | undefined; readonly parent: Task | undefined; pause(): Promise; resume(): Promise; cancel(timeout?: number): Promise; start(context?: TaskContext, parent?: Task): void; wait(): Promise; startAndWait(context?: TaskContext, parent?: Task): Promise; onChildUpdate(chunkSize: number): void; map(transform: Transform): Task ? R : N>; setName(name: string, param?: Record): this; } export interface Transform { (this: T, value: T): N; } export interface TaskContext { fork?(task: Task): number; onStart?(task: Task): void; onUpdate?(task: Task, chunkSize: number): void; onFailed?(task: Task, error: any): void; onSucceed?(task: Task, result: any): void; onPaused?(task: Task): void; onResumed?(task: Task): void; onCancelled?(task: Task): void; } export declare function createFork(): TaskContext['fork']; export declare abstract class BaseTask implements Task { protected _state: TaskState; protected _promise: Promise; protected resolve: (value: T) => void; protected reject: (err: any) => void; protected _from: string | undefined; protected _to: string | undefined; protected _progress: number; protected _total: number; protected _path: string; protected _id: number; parent: Task | undefined; context: TaskContext; name: string; param: object; protected resultOrError: T | any; constructor(); setName(name: string, param?: object): this; get(): T | void; get id(): number; get path(): string; get progress(): number; get total(): number; get to(): string | undefined; get from(): string | undefined; get state(): TaskState; get isCancelled(): boolean; get isPaused(): boolean; get isDone(): boolean; get isRunning(): boolean; pause(): Promise; resume(): Promise; cancel(timeout?: number): Promise; wait(): Promise; start(context?: TaskContext, parent?: Task): void; startAndWait(context?: TaskContext, parent?: Task): Promise; protected update(chunkSize: number): void; onChildUpdate(chunkSize: number): void; protected abstract runTask(): Promise; protected abstract cancelTask(): Promise; protected abstract pauseTask(): Promise; protected abstract resumeTask(): Promise; map(transform: Transform): Task ? R : N>; } export declare abstract class AbortableTask extends BaseTask { protected _pausing: Promise; protected _unpause: () => void; protected _onAborted: () => void; protected _onResume: () => void; protected abstract process(): Promise; protected abstract abort(isCancelled: boolean): void; protected abstract isAbortedError(e: any): boolean; protected cancelTask(): Promise; protected pauseTask(): Promise; protected resumeTask(): Promise; protected runTask(): Promise; } export declare abstract class TaskGroup extends BaseTask { protected children: Task[]; onChildUpdate(chunkSize: number): void; protected cancelTask(): Promise; protected pauseTask(): Promise; protected resumeTask(): Promise; all>(tasks: Iterable, { throwErrorImmediately, getErrorMessage }?: { throwErrorImmediately?: boolean; getErrorMessage?: (errors: any[]) => string; }): Promise<(T extends Task ? R : never)[]>; } export type TaskExecutor = (this: TaskRoutine) => Promise | T; export declare class TaskRoutine extends TaskGroup { readonly executor: TaskExecutor; constructor(name: string, executor: TaskExecutor, param?: object); concat(task: TaskRoutine): Promise; /** * Yield a new child task to this routine */ yield(task: Task): Promise; protected runTask(): Promise; } export declare function task(name: string, executor: TaskExecutor, param?: object): TaskRoutine; //# sourceMappingURL=index.d.ts.map