thorish
Version:
This is a library of useful JS concepts and data structures for Node and the browser. It it, unashamedly, a dumping ground for code needed by [@samthor](https://twitter.com/samthor)'s projects.
50 lines (49 loc) • 1.68 kB
TypeScript
export type TaskType<T> = {
/**
* Resolves when the passed {@link AbortSignal} is aborted, or rejects if the task runner throws.
*/
done: Promise<void>;
/**
* Helper to queue items into the {@link workTask}.
*/
queue: (arg: T, ...rest: T[]) => void;
};
export type TaskOptions = {
signal: AbortSignal;
/**
* Whether only to pass unique items (as per {@link Set} equality) to the task runner.
*
* @default false
*/
unique: boolean;
/**
* The minimum time to wait before running a task.
*
* @default 0
*/
min: number;
/**
* The time to wait for items to run the task on, after the first is seen.
*
* @default 0
*/
delay: number;
};
/**
* Runs a task forever (unless it crashes). This enables a "single-threaded" task to run over items
* pushed into it, possibly with some delaying/deduping. It aggregates the inputs and passes them
* into the task runner to be handled all at once.
*
* Errors throws inside the task runner will result in the returned {@link Promise} rejecting.
*
* Returns a function which triggers the task for new items.
*/
export declare function workTask<T = void>(task: (...args: T[]) => void | Promise<void>, options?: Partial<TaskOptions>): TaskType<T>;
/**
* Creates a simple serial runner for tasks. This allows callables to be pushed into it and run
* only after the prior task has failed or completed.
*
* Pass an optional argument to use as the `this` on all invoked tasks. This isn't typesafe, just
* assume it.
*/
export declare function runner<X = never>(thisArg?: X): <T>(cb: () => Promise<T> | T) => Promise<T>;