import { AsyncDisposable, Disposable } from './disposable'; /** * Function that is called to start the iteration of a task. * @param {Task} task The task itself. */ export declare type TaskFunc = (task: Task) => void | Promise; /** * Function that is called when a task result is available. * @param {Task} value The result. */ export declare type TaskConsumer = (value: Promise) => void; export declare class Task { /** * Create a new task. * @param func The function that is called when the task is started or after it has been reset. */ constructor(func: TaskFunc); private readonly _func; private readonly _activity; private readonly _dependents; private readonly _resources; private readonly _persistentResources; private readonly _output; private readonly _consumers; private readonly _sources; private readonly _dependencies; private _started; private _resetting; /** * If set to a function, it will be called for every critical error that occurs. * If not set to a function, a unhandled promise rejection will occur for any critical error. */ static criticalErrorCallback: (error: any, task: Task) => void; /** * Emit a result. * @param value The result. */ return(value: T | Promise): void; /** * Emit an error as the result. * @param error The error. */ throw(error: any): void; /** * Register a resource to be disposed when this task is reset or stopped. * If this task is currently beeing reset or stopped, the resource is disposed immediately. * @param resource The resource. */ using(resource: AsyncDisposable): void; /** * Register a resource to be disposed when this task is stopped. * If this task is currently stopped, the resource is disposed immediately. * @param resource The resource. */ usingPersistent(resource: AsyncDisposable): void; private disposeResources; private disposePersistentResources; /** * Add and start a dependency if this task is currently started. * @param source The dependency. */ addDependency(source: Task): void; /** * Remove a dependency and stop it if it has no other dependents. * @param source The dependency. */ removeDependency(source: Task): void; private disposeDependencies; /** * Register activity. * The next iteration will not be started until all activity is done. * @param activity The activity. Rejections are ignored. */ activity(activity: Promise): Promise; private criticalActivity; /** * Wait until this task is inactive. */ inactive(): Promise; /** * Pipe the results of this task to the specified consumer. * Outdated results are discarded. * @param consumer The consumer. * @param latest True to asynchronously push the current latest result. Default is true. */ pipe(consumer: TaskConsumer, latest?: boolean): Disposable; /** * Add a dependency and get it's latest result. * When the dependency updates it's result, this task will be reset. * @param source The dependency. * @returns The latest result. If the dependency is stopped before emitting a result, the promise will be rejected as it is assumed, that the task will never emit a result. */ use(source: Task): Promise; /** * Get a single output of a task and start it if needed. * @param task The task. * @returns The latest result. */ static use(task: Task): Promise; /** * Start this task. * @returns A resource that represents the dependency between the caller and this task. When all resources are disposed, the task will be stopped. */ start(): Disposable; /** * Restart this task if it is currently running. */ reset(): void; /** * Create an extension of this task that emits different output. * @param transform The output transform. * @returns An extension of this task. */ transform

(transform: (value: T) => P): Task

; /** * Create an extension of this task that extracts a property of the output and emits it instead. * @param prop The property key to extract. * @returns An extension of this task. */ extract

(prop: P): Task; /** * Create a task that always returns the same value. * @param value The value. */ static value(value: T): Task; /** * Wrap a function into a task that uses the specified input task. * @param fn The function to wrap. * @param input The input task. * @returns a function that returns the result of the wrapped function. */ static wrap(fn: (arg: T) => R | Promise, input: Task): Task; /** * Wrap a function into a task that uses the specified input task. * @param fn The function to wrap. * @param input The input task. * @returns a function that returns the result of the wrapped function. */ static wrapN(fn: (...args: T) => R | Promise, input: Task): Task; }