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.
61 lines (60 loc) • 2.52 kB
TypeScript
/**
* Configures a function to run after this signal is aborted. This always run in its own microtask
* (either as an event handler, or immediately via {@link Promise.resolve}).
*
* Returns a method which can be used to remove this handler. Returns `true` if it had not yet been
* run.
*/
export declare function afterSignal(signal: AbortSignal, fn: () => any): () => boolean;
/**
* Returns a {@link Promise} for the abort of the passed {@link AbortSignal}. Resolves with void/
* `undefined` when done, rather than the `reason` property.
*/
export declare function promiseVoidForSignal(signal: AbortSignal): Promise<void>;
/**
* Returns a {@link Promise} for the abort of the passed {@link AbortSignal}. This may be already
* resolved/rejected if the signal is already aborted, rather than running in a microtask.
*
* By default, this rejects with the signal's `reason`. Pass a second argument (even `null` or
* `undefined`) to resolve with this value, instead (e.g., `Promise.reject(...)`).
*/
export declare function promiseForSignal<T = never>(signal: AbortSignal, resolveWith?: Promise<T> | T): Promise<T>;
/**
* Wraps {@link AbortSignal.timeout} to deal with Chrome's "TimeoutError" issue (before Chrome 123, it reported "AbortError").
*/
export declare function abortSignalTimeout(timeout: number): AbortSignal;
/**
* Returns a new {@link AbortSignal} which aborts on the next tick.
*
* Aborts with "TimeoutError", just like {@link AbortSignal.timeout}.
*/
export declare function tickAbortSignal(): AbortSignal;
/**
* Returns a new {@link AbortSignal} that can be individually aborted, but which is also tied to
* the lifetimes of the passed signals. If any passed signals are aborted, the derived symbol also
* aborts.
*
* If any passed signal is already aborted, returns one of them directly (not derived), with a no-op
* abort function.
*
* If no signals are passed, acts as a pure convenience over creating a proper
* {@link AbortController}, and the result can be destructured.
*/
export declare function derivedSignal(...raw: (AbortSignal | undefined)[]): {
signal: AbortSignal;
abort: (reason?: any) => void;
};
/**
* An already aborted signal.
*/
export declare const abortedSignal: AbortSignal;
/**
* A never aborted signal.
*/
export declare const neverAbortedSignal: AbortSignal;
/**
* A TODO signal, used as a placeholder until you can find a better one.
*
* This is the same as {@link neverAbortedSignal}.
*/
export declare const todoSignal: AbortSignal;