export type Status = "idle" | "processing" | "success" | "failed"; export interface AbstractTaskState { status: S; } export type TaskFailureReason = "timeout" | "aborted" | "error"; export interface AbstractFailedTask extends AbstractTaskState<"failed"> { reason: R; } export interface TaskFailedByError extends AbstractFailedTask<"error"> { error: E; } export type FailedTask = TaskFailedByError | AbstractFailedTask<"timeout"> | AbstractFailedTask<"aborted">; export interface ProcessingTask extends AbstractTaskState<"processing"> { delayed: boolean; args: T; promise: Promise; abortController: AbortController; } export type TaskState = AbstractTaskState<"idle"> | ProcessingTask | AbstractTaskState<"success"> | FailedTask; export type TasksCombinatorDecision = boolean | "abort" | "untrack"; export type TasksCombinator = (state: TaskState) => TasksCombinatorDecision; export interface TaskOptions, R, E> { execute: (signal: AbortSignal, ...args: T) => Promise; onSuccess?: (result: R, ...args: T) => void; onFailure?: (failure: FailedTask, ...args: T) => void; /** * The `combinator` runtime error is interpreted as `false`. * @default waitPrevious */ combinator?: TasksCombinator; /** * @default 500 */ delayedMs?: number; /** * @default 8000 */ timeoutMs?: number; } /** * Forget previous task */ export declare const forgetPrevious: TasksCombinator; /** * Abort previous task */ export declare const abortPrevious: TasksCombinator; /** * Ignore new tasks until the previous task is completed */ export declare const waitPrevious: TasksCombinator; export declare function throttle(combinator: TasksCombinator, delayedMs: number): TasksCombinator; export declare class InitializationError { readonly state: TaskState; constructor(state: TaskState); } export declare class CompletionError { readonly state: FailedTask; constructor(state: FailedTask); } export interface Task, R, E> { readonly state: Readonly>; readonly status: Status; readonly isSuccess: boolean; readonly isFailed: boolean; readonly isProcessed: boolean; readonly isDelayed: boolean; matches(status: S): this is Task & { status: S; state: Readonly, AbstractTaskState>>; }; /** * Initiates the task without waiting for its result. * Any side effects or failures are handled internally. */ run(...args: T): void; /** * Initiates the task and returns a promise that resolves when the task completes. * Use this method when you need to handle the result or catch errors. * @throws {InitializationError} if combinator returns `false`. * @throws {CompletionError} if task were aborted or timeouted. */ runAsync(...args: T): Promise; /** * Aborts the ongoing task if it is currently processing. * The task will fail with an "aborted" reason and trigger any associated failure callbacks. */ abort(): void; } export declare function createTask, R = unknown, E = unknown>(options: TaskOptions): Task;