/** * A robust polling implementation that handles asynchronous operations with controlled timing. * * Unlike traditional interval-based polling, this class implements a delay-after-completion approach: * After each polling operation completes, it waits for a specified delay before initiating the next operation. * This prevents operation stacking when individual operations take longer than the delay period. * * Key features: * - Guaranteed operation spacing using delay-after-completion * - Safe stopping mechanism that works even during operation execution * - Optional timeout support * - Promise-based API with type safety * * @template T The type of the value that will be resolved when polling completes successfully * * @example * // Basic polling example - check for task completion * const polling = new Polling( * async () => { * const result = await checkLongRunningProcess(); * return result.completed ? { stop: true, value: result } : { stop: false }; * }, * 1000 // Poll every 1 second * ); * * try { * const result = await polling.start(); * console.log('Task completed:', result); * } catch (error) { * if (isTimeoutError(error)) { * console.log('Polling timed out'); * } else if (isInterruptedError(error)) { * console.log('Polling was interrupted'); * } else { * console.error('Unexpected error', error); * } * } * * @example * // Polling with timeout * const pollingWithTimeout = new Polling( * async () => { * const result = await checkLongRunningProcess(); * return result.completed ? { stop: true, value: result } : { stop: false }; * }, * 500, // Poll every 500ms * 10000 // Timeout after 10 seconds * ); * * pollingWithTimeout.start() * .then(result => console.log('Task completed:', result);) * .catch(error => console.error('Failed:', error.message)); */ export declare class Polling { #private; /** * Creates a new Polling instance. * * @param asyncCallback * The async function to be called on each polling tick. * Should return an object with: * - stop: true to end polling (optionally with a value) * - stop: false to continue polling * @param delay * The delay in milliseconds to wait after each polling operation completes * before starting the next one * @param timeout * Optional timeout in milliseconds after which polling will stop * and reject with PollingTimeoutError */ constructor(asyncCallback: () => Promise<{ stop: true; value?: T; } | { stop: false; }>, delay: number, timeout?: number); /** * Indicates whether the polling process is currently active. * * @returns true if polling is running, false otherwise */ get running(): boolean; /** * Starts the polling process. * If polling is already running, it will be forcefully stopped before starting anew. * * @returns Resolves with the final value when polling completes successfully * @throws {PollingInterruptedError} If polling is manually stopped * @throws {PollingTimeoutError} If the specified timeout is reached */ start(): Promise; /** * Stops the polling process immediately. * This will reject the promise returned by start() with a PollingInterruptedError. */ stop(): void; } /** * Error thrown when polling is manually stopped using the stop() method. */ export declare class PollingInterruptedError extends Error { constructor(); } /** * Error thrown when polling exceeds its specified timeout duration. */ export declare class PollingTimeoutError extends Error { constructor(); } /** * Utility function to check if an error is a PollingTimeoutError. * * @param error The error to check * @returns true if the error is a PollingTimeoutError, false otherwise */ export declare function isTimeoutError(error: unknown): boolean; /** * Utility function to check if an error is a PollingInterruptedError. * * @param error The error to check * @returns true if the error is a PollingInterruptedError, false otherwise */ export declare function isInterruptedError(error: unknown): boolean; //# sourceMappingURL=polling.d.ts.map