|
| 1 | +/** |
| 2 | + * Shared Stream Connection Manager |
| 3 | + * Handles reconnect, heartbeat, and event callbacks for SSE or WebSocket |
| 4 | + */ |
| 5 | +export class StreamConnection { |
| 6 | + /** |
| 7 | + * @param {() => EventSource | WebSocket} createFn - Function that creates a new EventSource or WebSocket. |
| 8 | + * @param {ng.StreamConnectionConfig} config - Configuration object with callbacks, retries, heartbeat, transformMessage. |
| 9 | + * @param {ng.LogService} log - Optional logger (default: console). |
| 10 | + */ |
| 11 | + constructor( |
| 12 | + createFn: () => EventSource | WebSocket, |
| 13 | + config?: ng.StreamConnectionConfig, |
| 14 | + log?: ng.LogService, |
| 15 | + ); |
| 16 | + createFn: () => EventSource | WebSocket; |
| 17 | + config: { |
| 18 | + onOpen?: (event: Event) => void; |
| 19 | + onMessage?: (data: any, event: Event | MessageEvent) => void; |
| 20 | + onError?: (err: any) => void; |
| 21 | + onReconnect?: (attempt: number) => void; |
| 22 | + retryDelay: number; |
| 23 | + maxRetries: number; |
| 24 | + heartbeatTimeout: number; |
| 25 | + transformMessage: (data: string) => any; |
| 26 | + }; |
| 27 | + $log: import("../log/interface.ts").LogService; |
| 28 | + retryCount: number; |
| 29 | + closed: boolean; |
| 30 | + heartbeatTimer: number; |
| 31 | + /** @type {EventSource | WebSocket | null} */ |
| 32 | + connection: EventSource | WebSocket | null; |
| 33 | + /** |
| 34 | + * Establishes a new connection using the provided createFn. |
| 35 | + * Closes any existing connection before creating a new one. |
| 36 | + */ |
| 37 | + connect(): void; |
| 38 | + /** |
| 39 | + * Binds event handlers to the underlying connection (EventSource or WebSocket) |
| 40 | + * for open, message, error, and close events. |
| 41 | + */ |
| 42 | + bindEvents(): void; |
| 43 | + /** |
| 44 | + * Handles the open event from the connection. |
| 45 | + * @param {Event} event - The open event. |
| 46 | + */ |
| 47 | + handleOpen(event: Event): void; |
| 48 | + /** |
| 49 | + * Handles incoming messages, applies the transformMessage function, |
| 50 | + * and calls the onMessage callback. |
| 51 | + * @param {any} data - Raw message data. |
| 52 | + * @param {Event} event - The message event. |
| 53 | + */ |
| 54 | + handleMessage(data: any, event: Event): void; |
| 55 | + /** |
| 56 | + * Handles errors emitted from the connection. |
| 57 | + * Calls onError callback and schedules a reconnect. |
| 58 | + * @param {any} err - Error object or message. |
| 59 | + */ |
| 60 | + handleError(err: any): void; |
| 61 | + /** |
| 62 | + * Handles close events for WebSocket connections. |
| 63 | + * Triggers reconnect logic. |
| 64 | + */ |
| 65 | + handleClose(): void; |
| 66 | + /** |
| 67 | + * Schedules a reconnect attempt based on retryCount and config.maxRetries. |
| 68 | + * Calls onReconnect callback if reconnecting. |
| 69 | + */ |
| 70 | + scheduleReconnect(): void; |
| 71 | + /** |
| 72 | + * Resets the heartbeat timer. If the timer expires, the connection is closed |
| 73 | + * and a reconnect is attempted. |
| 74 | + */ |
| 75 | + resetHeartbeat(): void; |
| 76 | + /** |
| 77 | + * Sends data over a WebSocket connection. |
| 78 | + * Logs a warning if called on a non-WebSocket connection. |
| 79 | + * @param {any} data - Data to send. |
| 80 | + */ |
| 81 | + send(data: any): void; |
| 82 | + /** |
| 83 | + * Closes the connection manually and clears the heartbeat timer. |
| 84 | + */ |
| 85 | + close(): void; |
| 86 | + _closeInternal(): void; |
| 87 | +} |
0 commit comments