import { sleep } from '@enbox/common';
export class Poller {
    /**
     * The interval in milliseconds to wait before retrying the delegate function.
     */
    static pollRetrySleep = 20;
    /**
     * The maximum time in milliseconds to wait before timing out the delegate function.
     */
    static pollTimeout = 2000;
    /**
     *  Polls the delegate function until it succeeds or the timeout is exceeded.
     *
     * @param delegate a function that returns a promise and may throw.
     * @param retrySleep the interval in milliseconds to wait before retrying the delegate function.
     * @param timeout the maximum time in milliseconds to wait before timing out the delegate function.
     *
     * @throws {Error} `Operation timed out` if the timeout is exceeded.
     */
    static async pollUntilSuccessOrTimeout(delegate, retrySleep = Poller.pollRetrySleep, timeout = Poller.pollTimeout) {
        const startTime = Date.now();
        while (true) {
            try {
                // Attempt to execute the delegate function
                return await delegate();
            }
            catch {
                // Check if the timeout has been exceeded
                if (Date.now() - startTime >= timeout) {
                    throw new Error('Operation timed out');
                }
                // Sleep for the retry interval before attempting again
                await sleep(retrySleep);
            }
        }
    }
}
//# sourceMappingURL=poller.js.map