@@ -72,7 +72,7 @@ export default class ReconnectingWebSocket {
72
72
private _connectTimeout : any ;
73
73
private _shouldReconnect = true ;
74
74
private _connectLock = false ;
75
- private _binaryType = 'blob' ;
75
+ private _binaryType : BinaryType = 'blob' ;
76
76
private _closeCalled = false ;
77
77
private _messageQueue : Message [ ] = [ ] ;
78
78
@@ -116,14 +116,13 @@ export default class ReconnectingWebSocket {
116
116
return ReconnectingWebSocket . CLOSED ;
117
117
}
118
118
119
- get binaryType ( ) : string {
119
+ get binaryType ( ) {
120
120
return this . _ws ? this . _ws . binaryType : this . _binaryType ;
121
121
}
122
122
123
- set binaryType ( value : string ) {
123
+ set binaryType ( value : BinaryType ) {
124
124
this . _binaryType = value ;
125
125
if ( this . _ws ) {
126
- // @ts -ignore
127
126
this . _ws . binaryType = value ;
128
127
}
129
128
}
@@ -194,23 +193,23 @@ export default class ReconnectingWebSocket {
194
193
/**
195
194
* An event listener to be called when the WebSocket connection's readyState changes to CLOSED
196
195
*/
197
- public onclose ? : ( event : Events . CloseEvent ) => void = undefined ;
196
+ public onclose : ( ( event : Events . CloseEvent ) => void ) | null = null ;
198
197
199
198
/**
200
199
* An event listener to be called when an error occurs
201
200
*/
202
- public onerror ? : ( event : Events . ErrorEvent ) => void = undefined ;
201
+ public onerror : ( ( event : Events . ErrorEvent ) => void ) | null = null ;
203
202
204
203
/**
205
204
* An event listener to be called when a message is received from the server
206
205
*/
207
- public onmessage ? : ( event : MessageEvent ) => void = undefined ;
206
+ public onmessage : ( ( event : MessageEvent ) => void ) | null = null ;
208
207
209
208
/**
210
209
* An event listener to be called when the WebSocket connection's readyState changes to OPEN;
211
210
* this indicates that the connection is ready to send and receive data
212
211
*/
213
- public onopen ? : ( event : Events . Event ) => void = undefined ;
212
+ public onopen : ( ( event : Event ) => void ) | null = null ;
214
213
215
214
/**
216
215
* Closes the WebSocket connection or connection attempt, if any. If the connection is already
@@ -276,6 +275,16 @@ export default class ReconnectingWebSocket {
276
275
}
277
276
}
278
277
278
+ public dispatchEvent ( event : Event ) {
279
+ const listeners = this . _listeners [ event . type as keyof Events . WebSocketEventListenerMap ] ;
280
+ if ( listeners ) {
281
+ for ( const listener of listeners ) {
282
+ this . _callEventListener ( event , listener ) ;
283
+ }
284
+ }
285
+ return true ;
286
+ }
287
+
279
288
/**
280
289
* Removes an event listener
281
290
*/
@@ -372,7 +381,6 @@ export default class ReconnectingWebSocket {
372
381
this . _ws = this . _protocols
373
382
? new WebSocket ( url , this . _protocols )
374
383
: new WebSocket ( url ) ;
375
- // @ts -ignore
376
384
this . _ws ! . binaryType = this . _binaryType ;
377
385
this . _connectLock = false ;
378
386
this . _addListeners ( ) ;
@@ -418,14 +426,13 @@ export default class ReconnectingWebSocket {
418
426
}
419
427
}
420
428
421
- private _handleOpen = ( event : Events . Event ) => {
429
+ private _handleOpen = ( event : Event ) => {
422
430
this . _debug ( 'open event' ) ;
423
431
const { minUptime = DEFAULT . minUptime } = this . _options ;
424
432
425
433
clearTimeout ( this . _connectTimeout ) ;
426
434
this . _uptimeTimeout = setTimeout ( ( ) => this . _acceptOpen ( ) , minUptime ) ;
427
435
428
- // @ts -ignore
429
436
this . _ws ! . binaryType = this . _binaryType ;
430
437
431
438
// send enqueued messages (messages sent before websocket open event)
0 commit comments