/** * Utils for promises. * * @license * Copyright 2022-2026 Matter.js Authors * SPDX-License-Identifier: Apache-2.0 */ import { Duration } from "#time/Duration.js"; import { TimeoutError } from "../MatterError.js"; /** * Obtain a promise with functions to resolve and reject. */ export declare function createPromise(): { promise: Promise; resolver: (value: T) => void; rejecter: (reason?: any) => void; }; /** * Use all promises or promise returning methods and return the first resolved promise or reject when all promises * rejected */ export declare function anyPromise(promises: ((() => Promise) | Promise)[]): Promise; /** * Thrown when a timed promise times out. */ export declare class PromiseTimeoutError extends TimeoutError { } /** * Create a promise with a timeout. * * By default, rejects with {@link PromiseTimeoutError} on timeout but you can override by supplying {@link cancel}. * * @param timeout the timeout in milliseconds * @param promise a promise that resolves or rejects when the timed task completes * @param cancel invoked on timeout (default implementation throws {@link PromiseTimeoutError}) */ export declare function withTimeout(timeout: Duration, promise: Promise, cancel?: AbortController | (() => void)): Promise; /** * Return type for functions that are optionally asynchronous. * * TODO - as currently defined MaybePromise of a Promise incorrectly wraps as a Promise of a Promise */ export type MaybePromise = T | PromiseLike; /** * Promise-like version of above. */ export type MaybePromiseLike = T | PromiseLike; export declare const MaybePromise: { /** * Determine whether a {@link MaybePromiseLike} is a {@link Promise}. */ is(value: MaybePromise): value is PromiseLike; /** * Chained MaybePromise. Invokes the resolve function immediately if the {@link MaybePromise} is not a * {@link Promise}, otherwise the same as a normal {@link Promise.then}. */ then(producer: MaybePromise | (() => MaybePromise), resolve?: ((input: I) => MaybePromise) | null, reject?: ((error: any) => MaybePromise) | null): MaybePromise; /** * Equivalent of {@link Promise.catch}. */ catch(producer: MaybePromise | (() => MaybePromise), onrejected?: ((reason: any) => MaybePromise) | null): MaybePromise; /** * Equivalent of {@link Promise.finally}. */ finally(producer: MaybePromise | (() => MaybePromise), onfinally?: (() => MaybePromise) | null): MaybePromise; [Symbol.toStringTag]: string; }; /** * A recurring promise. * * A gate is duck-compatible with {@link Promise} and includes resolution methods on its public API. * * As an extension to normal promise semantics, a gate may be "opened" or "closed". {@link open} provides multi- * resolution so that future awaits return a new value. {@link close} returns the promise to an unsettled state so that * future awaits block. * * This is useful for basic synchronization of two tightly coupled ongoing tasks. */ export declare class Gate implements Promise { #private; constructor(); /** * Move to resolved state with the specified value. */ open(value: T): void; /** * Move to unresolved state. */ close(): void; /** * Perform normal promise resolution. Ignored if promise is already settled. */ resolve(value: T): void; /** * Perform normal promise rejection. Ignored if promise is already settled. */ reject(cause: any): void; then(resolve?: ((value: T) => TResult1 | PromiseLike) | null, reject?: ((reason: any) => TResult2 | PromiseLike) | null): Promise; catch(reject?: ((reason: any) => TResult | PromiseLike) | null): Promise; finally(then?: (() => void) | null): Promise; [Symbol.toStringTag]: string; } /** * Replacements for standard promise functionality that avoid spec pitfalls. */ export declare namespace SafePromise { /** * A version of {@link Promise.race} that won't leak memory with long-lived promises. * * See: * * https://github.com/nodejs/node/issues/17469#issuecomment-685216777 * * ...although this isn't an issue specific to Node. * * We specialize support for {@link Observable} and {@link ObservableValue}. Those contracts are awaitable like a * {@link Promise} but we instead register listeners directly so we can unregister using {@link Observable#off}. */ function race(values: Iterable): Promise>; } //# sourceMappingURL=Promises.d.ts.map