import { Thunkable, ExceptionHandlerOptions, TaskObserver, TrackOptions, Some, Awaitable } from '@xh/hoist/core'; /** * Enhancements to the Global Promise object. * Merged in to definition here and implemented on the prototype below. */ declare global { interface Promise { /** * Version of `then()` that wraps the callback in a MobX action, for use in a Promise chain * that modifies MobX observables. */ thenAction(onFulfilled: (value: T) => Awaitable): Promise; /** * Version of `catch()` that will only catch certain exceptions. * * @param selector - closure that takes an exception and returns a boolean. May also be * specified as an exception name or list of names. Only exceptions passing this * selector will be handled by this method. * @param fn - catch handler */ catchWhen(selector: ((e: unknown) => boolean) | Some, fn?: (reason: unknown) => Awaitable): Promise; /** * Version of `catch()` that passes the error onto Hoist's default exception handler for * convention-driven logging and alerting. Typically called last in a Promise chain. */ catchDefault(options?: ExceptionHandlerOptions): Promise; /** * Version of `catchDefault()` that will only catch certain exceptions. */ catchDefaultWhen(selector: ((e: unknown) => boolean) | Some, options: ExceptionHandlerOptions): Promise; /** * Wait on a potentially async function before passing on the original value. * Useful when we want to block and do something on the promise chain, but do not want to * manipulate the values being passed through. * * @param onFulfillment - function to receive the pass-through value when ready. */ tap(onFulfillment: (value: T) => any): Promise; /** * Return a promise that will resolve a specified delay after this promise resolves. * * @param interval - delay in milliseconds. */ wait(interval: number): Promise; /** * Return a promise that will reject if this promise has not been settled after the * specified interval has passed. */ timeout(spec: PromiseTimeoutSpec): Promise; /** * Link this promise to an instance of a {@link TaskObserver}. See that class for details * on what it provides and how it can be used to coordinate masking and progress messages * on one or more async operations. */ linkTo(spec: PromiseLinkSpec): Promise; /** * Track a Promise (with timing) via Hoist activity tracking. * @param options - TrackOptions, or simply a message string. */ track(options: TrackOptions | string): Promise; } } /** * Timeout interval in ms, or an object specifying the interval and an optional message to be used * for any exception thrown on timeout. */ export type PromiseTimeoutSpec = number | { interval: number; message?: string; }; /** * TaskObserver to track execution of a Promise, or an object specifying one with an optional * message to show while pending and/or optional flag to skip (e.g. for conditional masking). */ export type PromiseLinkSpec = TaskObserver | { observer: TaskObserver; message?: string; omit?: Thunkable; }; /** * Return a promise that will resolve after the specified amount of time. * * This method serves as a lightweight way to start a promise chain for any code. * * @param interval - milliseconds to delay (default 0). Note that the actual delay will be subject * to the minimum delay for `setTimeout()` in the browser. */ export declare function wait(interval?: number): Promise; /** * Return a promise that will resolve after a condition has been met, or reject if timed out. * @param condition - function returning true when expected condition is met. * @param interval - milliseconds to wait between checks (default 50). Note that the actual time * will be subject to the minimum delay for `setTimeout()` in the browser. * @param timeout - milliseconds after which the Promise should be rejected (default 5000). */ export declare function waitFor(condition: () => boolean, { interval, timeout }?: { interval?: number; timeout?: number; }): Promise; /** * Wrap a promise-returning function with trailing-edge debounce semantics. Calls made within * `wait` ms of each other share a single pending Promise; when the quiet period elapses, the * underlying function is invoked once with the args from the most recent call, and the shared * Promise resolves (or rejects) with that result. * * Useful for search-as-you-type inputs and similar flows where only the latest call's result * matters and intermediate calls can be coalesced. * * Adapted from the (unmaintained) `debounce-promise` package by Bjorn Tipling, * https://github.com/bjoerge/debounce-promise - MIT licensed. */ export declare function debouncePromise(fn: (...args: A) => R | Promise, wait: number): (...args: A) => Promise; /** * Return a promise that resolves immediately. * @param value - the value to be returned by the resulting Promise. */ export declare function resolve(value?: T): Promise; /** * Return a promise that never resolves. */ export declare function never(): Promise;