import { type Milliseconds } from '../date/date'; import { type Maybe } from '../value/maybe.type'; /** * Waits `ms`, waking EARLY (and never rejecting) when `signal` aborts. * * Resolves at once when `ms` is not positive (0, negative or NaN) or the signal is already aborted. Each path tears the * other down, so neither a fired timer nor an abort leaves a listener or timer behind. * * @param ms - How long to wait. * @param signal - Optional signal that ends the wait early. * @returns A promise that resolves after the wait or on abort, whichever comes first. * * @dbxUtil * @dbxUtilCategory promise * @dbxUtilTags wait, delay, sleep, abort, signal, cancel, timeout, backoff * @dbxUtilRelated wait-for-ms, wait-for-promise-unless-aborted * * @example * ```ts * const controller = new AbortController(); * await waitForMsUnlessAborted(1000, controller.signal); // resolves after 1s, or as soon as controller.abort() runs * ``` */ export declare function waitForMsUnlessAborted(ms: Milliseconds, signal?: Maybe): Promise; /** * Starts a promise and waits for it, ending early with `undefined` when `signal` aborts. * * - With no signal, it is `await start()`. * - With an already-aborted signal, it resolves `undefined` without calling `start`. * - Otherwise it races `start()` against the abort: an abort resolves `undefined`; a rejection from `start()` is rethrown * unless the signal has aborted by then, in which case it also resolves `undefined`. A late rejection is never left * unhandled, and the abort listener is always removed. * * @param start - Starts the work to wait for. * @param signal - Optional signal that ends the wait early. * @returns The work's value, or `undefined` when the signal aborted first. * @throws {unknown} The work's rejection when it rejects before the signal aborts. * * @dbxUtil * @dbxUtilCategory promise * @dbxUtilTags wait, abort, signal, cancel, race, promise * @dbxUtilRelated wait-for-ms-unless-aborted, merge-abort-signals * * @example * ```ts * const value = await waitForPromiseUnlessAborted(() => gate.waitUntilOpen(), signal); * // value is undefined when signal aborted before the gate opened * ``` */ export declare function waitForPromiseUnlessAborted(start: () => PromiseLike, signal?: Maybe): Promise>; /** * Combines optional abort signals into one that aborts when any input aborts, with that input's `reason`. * * - Nullish entries are dropped; no signals returns `undefined` and exactly one returns that SAME instance. * - Two or more use `AbortSignal.any` (in input order) when the runtime has it, else an `AbortController` fallback that * aborts with the first already-aborted input's reason, or on the first input to abort later. * * @param signals - The signals to combine. * @returns The combined signal, or `undefined` when there are none. * * @dbxUtil * @dbxUtilCategory promise * @dbxUtilTags abort, signal, merge, combine, any, cancel * @dbxUtilRelated make-timeout-abort-signal, wait-for-promise-unless-aborted * * @example * ```ts * const signal = mergeAbortSignals([runSignal, request.signal]); // aborts when either aborts * mergeAbortSignals([undefined, runSignal]) === runSignal; // true * ``` */ export declare function mergeAbortSignals(signals: ReadonlyArray>): Maybe; /** * A cancellation seam an options bag carries. */ export interface AbortSignalRef { /** * An external cancellation signal. */ readonly signal?: AbortSignal | undefined; } /** * Configuration for {@link makeTimeoutAbortSignal}. */ export interface MakeTimeoutAbortSignalConfig { /** * How long until the signal aborts with a `TimeoutError`. A timeout of 0 aborts on the next tick; it never means "no * timeout". */ readonly timeout: Milliseconds; /** * Optional caller signal that also aborts the result, with its own reason. */ readonly signal?: Maybe; } /** * Creates a signal that aborts with a `TimeoutError` `DOMException` after `timeout`, or earlier when the optional caller * signal aborts. * * Uses `AbortSignal.timeout` when the runtime has it, else a `setTimeout`-driven controller (its timer is `unref()`ed * where supported), then combines it with the caller signal through {@link mergeAbortSignals} (timeout first). Runtime * APIs are read at call time, and an error they throw propagates to the caller. * * @param config - The timeout and the optional caller signal. * @returns The signal to pass to the timed call. * * @dbxUtil * @dbxUtilCategory promise * @dbxUtilTags abort, signal, timeout, fetch, cancel, deadline * @dbxUtilRelated merge-abort-signals, wait-for-ms-unless-aborted * * @example * ```ts * const response = await fetch(url, { signal: makeTimeoutAbortSignal({ timeout: 15_000, signal: callerSignal }) }); * ``` */ export declare function makeTimeoutAbortSignal(config: MakeTimeoutAbortSignalConfig): AbortSignal;