UNPKG

@andreasnicolaou/reactive-event-source

Version:

A lightweight reactive wrapper around EventSource using RxJS, providing automatic reconnection and buffering.

95 lines (90 loc) 3.47 kB
import { Observable } from 'rxjs'; declare class EventSourceError extends Error { attempt: number; constructor(message: string, attempt?: number); } type EventSourceEventType = 'open' | 'message' | 'error' | (string & Record<never, never>); type EventSourceOptions = { maxRetries: number; initialDelay: number; maxDelay: number; connectionTimeout?: number; withCredentials?: boolean; }; declare class ReactiveEventSource { private readonly destroy$; private readonly eventListenerCleanup; private readonly eventSource$; private readonly eventSubjects; private lastEventSource; private readonly options; private readonly readyStateSubject$; private readonly subscriptions; private readonly url; /** * Creates an instance of reactive event source. * @param url - The URL or URL object for the SSE connection. * @param options - Optional configuration for retry behavior and credentials. */ constructor(url: string | URL, options?: Partial<EventSourceOptions>); /** * Gets the current connection state of the EventSource * @returns {0 | 1 | 2} The ready state: * - 0 (CONNECTING): The connection is being established * - 1 (OPEN): The connection is active and receiving events * - 2 (CLOSED): The connection is closed or failed * @author Andreas Nicolaou * @memberof ReactiveEventSource */ get readyState(): number; /** * Observable that emits the current connection state * @returns Observable<number> Stream of connection state changes * @author Andreas Nicolaou * @memberof ReactiveEventSource */ get readyState$(): Observable<number>; /** * Determines if credentials (cookies, HTTP auth) are sent with requests * @returns {boolean} True if credentials are being sent, false otherwise * @author Andreas Nicolaou * @memberof ReactiveEventSource */ get withCredentials(): boolean; /** * Gets the resolved URL string used for the EventSource connection * @returns {string} The fully qualified URL as a string * @author Andreas Nicolaou * @memberof ReactiveEventSource */ get URL(): string; /** * Closes the SSE connection and completes all internal subjects and observables. * @author Andreas Nicolaou * @memberof ReactiveEventSource */ close(): void; /** * Subscribes to a specific SSE event type and returns it as an Observable. * @param eventType - The name of the event to listen for (e.g., "message", "error", "open"). * @returns An Observable that emits events of the given type. * @author Andreas Nicolaou * @memberof ReactiveEventSource */ on(eventType?: EventSourceEventType): Observable<MessageEvent>; /** * Creates an observable EventSource instance with optional credentials and automatic reconnection. * @returns An observable that manages the EventSource lifecycle and reconnection strategy. * @author Andreas Nicolaou * @memberof ReactiveEventSource */ private createEventSource; /** * Sets up an event listener for a specific event type * @param eventType - The event type to listen for * @param subject - The subject to emit events to */ private setupEventListener; } export { EventSourceError, ReactiveEventSource }; export type { EventSourceEventType, EventSourceOptions };