/** * Reconnection logic for FlashQ client. * * Implements exponential backoff with jitter for automatic reconnection * after connection loss. */ import { EventEmitter } from 'events'; import type { Logger } from '../utils/logger'; /** Connection state machine states */ export type ConnectionState = 'disconnected' | 'connecting' | 'connected' | 'reconnecting' | 'closed'; /** Configuration for reconnection behavior */ export interface ReconnectConfig { /** Whether auto-reconnect is enabled */ enabled: boolean; /** Maximum number of reconnection attempts (0 = unlimited) */ maxAttempts: number; /** Initial delay between reconnection attempts (ms) */ initialDelay: number; /** Maximum delay between reconnection attempts (ms) */ maxDelay: number; } /** Event data emitted during reconnection */ export interface ReconnectEventData { /** Current attempt number */ attempt: number; /** Delay before this attempt (ms) */ delay: number; } /** * Manages reconnection state and scheduling. * * Implements exponential backoff with jitter: * delay = min(initialDelay * 2^attempt, maxDelay) + random jitter * * @example * ```typescript * const manager = new ReconnectManager(config, logger, emitter); * * manager.schedule(async () => { * await connect(); * manager.reset(); * }); * ``` */ export declare class ReconnectManager { private readonly config; private readonly logger; private readonly emitter; /** Current reconnection attempt number */ private attempts; /** Timer handle for scheduled reconnection */ private timer; /** Whether reconnection was cancelled */ private cancelled; constructor(config: ReconnectConfig, logger: Logger, emitter: EventEmitter); /** * Calculates the delay for the next reconnection attempt. * * Uses exponential backoff with 30% jitter to prevent * thundering herd problems. * * @param attempt - Current attempt number (1-based) * @returns Delay in milliseconds */ calculateDelay(attempt: number): number; /** * Checks if more reconnection attempts are allowed. * * @returns true if another attempt can be made */ canRetry(): boolean; /** * Schedules a reconnection attempt. * * @param connectFn - Async function that performs the connection */ schedule(connectFn: () => Promise): void; /** * Resets the reconnection state after successful connection. */ reset(): void; /** * Cancels any pending reconnection attempt. */ cancel(): void; /** * Gets the current attempt count. */ getAttempts(): number; } /** * Waits for a reconnection to complete. * * @param emitter - Event emitter that fires reconnection events * @param timeout - Maximum time to wait (ms) * @returns Promise that resolves when reconnected * @throws ConnectionError if reconnection fails or times out */ export declare function waitForReconnection(emitter: EventEmitter, timeout: number): Promise; //# sourceMappingURL=reconnect.d.ts.map