/** * @file SSE Client * @description Low-level Server-Sent Events client */ /** * SSE connection state */ export type SSEState = 'connecting' | 'connected' | 'disconnected' | 'reconnecting'; /** * SSE message handler */ export type SSEMessageHandler = (data: unknown, event?: string) => void; /** * SSE error handler */ export type SSEErrorHandler = (error: Event) => void; /** * SSE state change handler */ export type SSEStateChangeHandler = (state: SSEState) => void; /** * SSE client configuration */ export interface SSEClientConfig { url: string; withCredentials?: boolean; reconnect?: boolean; reconnectInterval?: number; maxReconnectAttempts?: number; eventTypes?: string[]; } /** * SSE client class */ export declare class SSEClient { private eventSource; private config; private state; private reconnectAttempts; private reconnectTimeout; private messageHandlers; private errorHandlers; private stateChangeHandlers; /** * Store bound event handler references for proper cleanup * This fixes a memory leak where createEventHandler() created new functions * that couldn't be properly removed with removeEventListener */ private boundEventHandlers; constructor(config: SSEClientConfig); /** * Get current connection state */ getState(): SSEState; /** * Check if connected */ isConnected(): boolean; /** * Connect to SSE endpoint */ connect(): void; /** * Disconnect from SSE endpoint */ disconnect(): void; /** * Subscribe to messages of a specific event type * * Note: Uses stored bound handler references for proper cleanup. * This prevents memory leaks from orphaned event listeners. */ on(eventType: string, handler: SSEMessageHandler): () => void; /** * Subscribe to all 'message' events (default) */ onMessage(handler: SSEMessageHandler): () => void; /** * Subscribe to errors */ onError(handler: SSEErrorHandler): () => void; /** * Subscribe to state changes */ onStateChange(handler: SSEStateChangeHandler): () => void; /** * Setup EventSource event listeners */ private setupEventListeners; /** * Add listener for specific event type * * Note: Stores bound handler reference for proper cleanup later. */ private addEventTypeListener; /** * Create event handler for event type */ private createEventHandler; /** * Handle disconnection */ private handleDisconnect; /** * Schedule reconnection */ private scheduleReconnect; /** * Calculate exponential backoff delay */ private calculateBackoff; /** * Clear reconnect timeout */ private clearReconnectTimeout; /** * Update and broadcast state */ private setState; } /** * Create SSE client with default configuration */ export declare function createSSEClient(path?: string, config?: Partial): SSEClient;