UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

55 lines (54 loc) 2.26 kB
import type { NumberOfMilliseconds } from './types.js'; /** * Like AbortSignal, but it can "abort itself" via the `.abort()` method. * * Similar to how DeferredPromise is both a Promise and has `.resolve()` and `.reject()` methods. * * This is to simplify the AbortController/AbortSignal usage. * * Before this - you need to keep track of 2 things: AbortController and AbortSignal. * * After - you are good with only AbortableSignal, which can do both. * And it's compatible with AbortSignal (because it extends it). * * @experimental */ export interface AbortableSignal extends AbortSignal { abort: AbortController['abort']; } /** * Creates AbortableSignal, * which is like AbortSignal, but can "abort itself" with `.abort()` method. * * @experimental */ export declare function createAbortableSignal(): AbortableSignal; /** * Returns AbortSignal if ms is defined. * Otherwise returns undefined. */ export declare function abortSignalTimeoutOrUndefined(ms: NumberOfMilliseconds | undefined): AbortSignal | undefined; /** * Returns an AbortSignal that aborts after the given number of milliseconds. * Uses native `AbortSignal.timeout()` when available, falls back to a polyfill. * * The abort reason is a DOMException with name "TimeoutError". */ export declare function abortSignalTimeout(ms: NumberOfMilliseconds): AbortSignal; export declare function polyfilledAbortSignalTimeout(ms: NumberOfMilliseconds): AbortSignal; /** * Returns AbortSignal.any(signals) is the array (after filtering undefined inputs) is not empty, * otherwise undefined. */ export declare function abortSignalAnyOrUndefined(signals: (AbortSignal | undefined)[]): AbortSignal | undefined; /** * Returns an AbortSignal that aborts when any of the given signals abort. * Uses native `AbortSignal.any()` when available, falls back to a polyfill. * * The abort reason is taken from the first signal that aborts. * If any input signal is already aborted, the returned signal is immediately aborted. * * If only 1 signal is passed in the input array - that Signal is returned as-is. */ export declare function abortSignalAny(signals: AbortSignal[]): AbortSignal; export declare function polyfilledAbortSignalAny(signals: AbortSignal[]): AbortSignal;