/**
 * 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<T>(): {
    promise: Promise<T>;
    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<T>(promises: ((() => Promise<T>) | Promise<T>)[]): Promise<T>;
/**
 * 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<T>(timeout: Duration, promise: Promise<T>, cancel?: AbortController | (() => void)): Promise<T>;
/**
 * 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 = void> = T | PromiseLike<T>;
/**
 * Promise-like version of above.
 */
export type MaybePromiseLike<T = void> = T | PromiseLike<T>;
export declare const MaybePromise: {
    /**
     * Determine whether a {@link MaybePromiseLike} is a {@link Promise}.
     */
    is<T>(value: MaybePromise<T>): value is PromiseLike<T>;
    /**
     * 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<I, O1 = never, O2 = never>(producer: MaybePromise<I> | (() => MaybePromise<I>), resolve?: ((input: I) => MaybePromise<O1>) | null, reject?: ((error: any) => MaybePromise<O2>) | null): MaybePromise<O1 | O2>;
    /**
     * Equivalent of {@link Promise.catch}.
     */
    catch<T, TResult = never>(producer: MaybePromise<T> | (() => MaybePromise<T>), onrejected?: ((reason: any) => MaybePromise<TResult>) | null): MaybePromise<TResult>;
    /**
     * Equivalent of {@link Promise.finally}.
     */
    finally<T>(producer: MaybePromise<T> | (() => MaybePromise<T>), onfinally?: (() => MaybePromise<void>) | null): MaybePromise<T>;
    [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<T = void> implements Promise<T> {
    #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<TResult1 = T, TResult2 = never>(resolve?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null, reject?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
    catch<TResult = never>(reject?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<T | TResult>;
    finally(then?: (() => void) | null): Promise<T>;
    [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<T>(values: Iterable<T>): Promise<Awaited<T>>;
}
//# sourceMappingURL=Promises.d.ts.map