/** * @module ServerSentEvents * SSE client that dispatches received events as DOM events. * Uses the browser's built-in EventSource reconnection. * * @example * const sse = new SSEClient('/api/events', { * eventTypes: ['user-updated', 'order-created'] * }); * sse.connect(); * * document.addEventListener('user-updated', (e: SSEDataEvent) => { * console.log('User updated:', e.data); * }); */ /** * Event dispatched when an SSE message is received. * The event name matches the SSE event type. */ export declare class SSEDataEvent extends Event { data: unknown; constructor(eventName: string, data: unknown, eventInit?: EventInit); } /** * Factory function for creating custom event instances. * * @example * const factory: SSEEventFactory = (eventName, data) => { * switch (eventName) { * case 'user-updated': * return new UserUpdatedEvent(data as User); * default: * return new SSEDataEvent(eventName, data); * } * }; */ export type SSEEventFactory = (eventName: string, data: unknown) => Event; /** * Configuration options for SSEClient. */ export interface SSEOptions { /** * Target element or CSS selector for event dispatching. * Defaults to document. */ target?: string | Element; /** * Whether to send credentials with the request (default: false). */ withCredentials?: boolean; /** * Specific SSE event types to listen for. * If not specified, listens to the default 'message' event. * * @example * eventTypes: ['user-updated', 'order-created'] */ eventTypes?: string[]; /** * Factory function for creating custom event instances. * If not provided, SSEDataEvent is used. * * @example * eventFactory: (name, data) => new MyCustomEvent(name, data) */ eventFactory?: SSEEventFactory; /** * Callback when connection is established. */ onConnect?: (client: SSEClient) => void; /** * Callback when an error occurs. * Note: EventSource automatically reconnects on errors. */ onError?: (client: SSEClient, error: Event) => void; } /** * Server-Sent Events client that dispatches received events as DOM events. * Uses the browser's built-in EventSource with automatic reconnection. * * @example * const sse = new SSEClient('/api/events', { * target: '#notifications', * eventTypes: ['notification', 'alert'] * }); * * sse.connect(); * * document.querySelector('#notifications') * .addEventListener('notification', (e: SSEDataEvent) => { * showNotification(e.data); * }); * * sse.disconnect(); */ export declare class SSEClient { private url; private options?; private eventSource?; private target; /** * Whether the client is currently connected. */ get connected(): boolean; constructor(url: string, options?: SSEOptions); /** * Establish connection to the SSE endpoint. */ connect(): void; /** * Close the connection. */ disconnect(): void; private resolveTarget; private dispatchEvent; }