44 */
55export class StreamConnection {
66 /**
7- * @param {() => EventSource | WebSocket } createFn - function that creates a new EventSource or WebSocket
8- * @param {Object } config - config object with callbacks, retries, heartbeat, transformMessage
9- * @param {ng.LogService } log - optional logger
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).
1010 */
1111 constructor ( createFn , config = { } , log = console ) {
1212 this . createFn = createFn ;
@@ -33,6 +33,10 @@ export class StreamConnection {
3333 this . connect ( ) ;
3434 }
3535
36+ /**
37+ * Establishes a new connection using the provided createFn.
38+ * Closes any existing connection before creating a new one.
39+ */
3640 connect ( ) {
3741 if ( this . closed ) return ;
3842
@@ -48,6 +52,10 @@ export class StreamConnection {
4852 this . bindEvents ( ) ;
4953 }
5054
55+ /**
56+ * Binds event handlers to the underlying connection (EventSource or WebSocket)
57+ * for open, message, error, and close events.
58+ */
5159 bindEvents ( ) {
5260 const conn = this . connection ;
5361
@@ -63,12 +71,22 @@ export class StreamConnection {
6371 }
6472 }
6573
74+ /**
75+ * Handles the open event from the connection.
76+ * @param {Event } event - The open event.
77+ */
6678 handleOpen ( event ) {
6779 this . retryCount = 0 ;
6880 this . config . onOpen ?. ( event ) ;
6981 this . resetHeartbeat ( ) ;
7082 }
7183
84+ /**
85+ * Handles incoming messages, applies the transformMessage function,
86+ * and calls the onMessage callback.
87+ * @param {any } data - Raw message data.
88+ * @param {Event } event - The message event.
89+ */
7290 handleMessage ( data , event ) {
7391 try {
7492 data = this . config . transformMessage ?. ( data ) ?? data ;
@@ -79,15 +97,28 @@ export class StreamConnection {
7997 this . resetHeartbeat ( ) ;
8098 }
8199
100+ /**
101+ * Handles errors emitted from the connection.
102+ * Calls onError callback and schedules a reconnect.
103+ * @param {any } err - Error object or message.
104+ */
82105 handleError ( err ) {
83106 this . config . onError ?. ( err ) ;
84107 this . scheduleReconnect ( ) ;
85108 }
86109
110+ /**
111+ * Handles close events for WebSocket connections.
112+ * Triggers reconnect logic.
113+ */
87114 handleClose ( ) {
88115 this . scheduleReconnect ( ) ;
89116 }
90117
118+ /**
119+ * Schedules a reconnect attempt based on retryCount and config.maxRetries.
120+ * Calls onReconnect callback if reconnecting.
121+ */
91122 scheduleReconnect ( ) {
92123 if ( this . closed ) return ;
93124
@@ -100,6 +131,10 @@ export class StreamConnection {
100131 }
101132 }
102133
134+ /**
135+ * Resets the heartbeat timer. If the timer expires, the connection is closed
136+ * and a reconnect is attempted.
137+ */
103138 resetHeartbeat ( ) {
104139 if ( ! this . config . heartbeatTimeout ) return ;
105140 clearTimeout ( this . heartbeatTimer ) ;
@@ -111,6 +146,11 @@ export class StreamConnection {
111146 } , this . config . heartbeatTimeout ) ;
112147 }
113148
149+ /**
150+ * Sends data over a WebSocket connection.
151+ * Logs a warning if called on a non-WebSocket connection.
152+ * @param {any } data - Data to send.
153+ */
114154 send ( data ) {
115155 if ( this . connection instanceof WebSocket ) {
116156 this . connection . send ( JSON . stringify ( data ) ) ;
@@ -119,16 +159,14 @@ export class StreamConnection {
119159 }
120160 }
121161
162+ /**
163+ * Closes the connection manually and clears the heartbeat timer.
164+ */
122165 close ( ) {
123166 this . closed = true ;
124167 clearTimeout ( this . heartbeatTimer ) ;
125- if ( this . connection ) {
126- if (
127- this . connection instanceof EventSource ||
128- this . connection instanceof WebSocket
129- ) {
130- this . connection . close ( ) ;
131- }
168+ if ( this . connection && this . connection . close ) {
169+ this . connection . close ( ) ;
132170 }
133171 }
134172}
0 commit comments