/** * Task status. */ export type TaskStatus = 'pending' | 'resolved' | 'rejected'; /** * Task function signature. */ export type TaskFunc = (...args: A) => Promise; /** * Task options. */ export interface TaskOptions { state?: TaskStatus; error?: unknown; result?: Awaited; args?: A; swallow?: boolean; } /** * Object type used for pattern matching. */ export interface TaskMatchProps { pending?: (...args: A) => T1; rejected?: (error: unknown) => T2; resolved?: (result: Awaited) => T3; } /** * Task state. */ export interface TaskState { /** * The status (resolved, rejected, pending) */ readonly state: TaskStatus; /** * Convenience getter for `state === 'pending'`. */ readonly pending: boolean; /** * Convenience getter for `state === 'resolved'`. */ readonly resolved: boolean; /** * Convenience getter for `state === 'rejected'`. */ readonly rejected: boolean; /** * The last arguments passed to the task. */ readonly args: A; /** * The result of the last invocation. */ readonly result?: Awaited; /** * The error of the last failed invocation. */ readonly error?: unknown; } /** * Task methods. */ export interface TaskMethods { /** * Pattern-matches on the task status. * @param props */ match(props: TaskMatchProps): PT | ET | RT; /** * Wraps the task by invoking `func` with the inner task function, which returns the wrapped function * and converts that to a task. * * @param func */ wrap(func: (inner: (...args: A) => R) => (...args: NA) => NR): Task; /** * Sets the state. */ setState(props: TaskOptions): void; /** * Resets the state. */ reset(): void; } /** * Task function, state and methods. */ export type Task = TaskFunc> & TaskState & TaskMethods; /** * Actual task factory. */ export interface TaskCreator> extends MethodDecorator, PropertyDecorator { /** * Calls the actual task function. */ (func: (...args: A) => R, options?: Pick, K>): Task; (options: Pick, K>): PropertyDecorator; (options: Pick, K>): MethodDecorator; } export interface TaskFactory extends TaskCreator> { /** * Creates a task in the `resolved` state. */ resolved: TaskCreator, 'state'>>; /** * Creates a task in the `rejected` state. */ rejected: TaskCreator, 'state'>>; } /** * Creates a task in the `pending` state. */ declare const task: TaskFactory; export { task };