@@ -2,25 +2,17 @@ import ws from 'ws'
22import EventEmitter from 'eventemitter3'
33
44/**
5- * This class represents a reconnecting WebSocket. It extends the EventEmitter.
5+ * This class represents a reconnecting WebSocket client . It extends the EventEmitter.
66 *
7- * The class exposes all websocket
7+ * The class exposes all WebSocket properties and methods
88 * WebSocket client https://github.com/websockets/ws
99 */
10-
11- // Names of properties which are not cloned from underlying WebSocket
12- const ownEventNames = [ 'connecting' , 'delay' , 'timeout' , 'newListener' , 'removeListener' , 'reconnected' ]
13-
10+ export class ForeverWebSocket extends EventEmitter {
11+ // Names of properties which are not cloned from underlying WebSocket
12+ #ownEventNames = [ 'connecting' , 'delay' , 'timeout' , 'newListener' , 'removeListener' , 'reconnected' ]
1413// Property names for `options`
15- const optionsPropertyNames = [ 'automaticOpen' , 'reconnect' , 'timeout' , 'ping' , 'newWebsocketFn' ]
16-
17- // Detects if the property name is a method of obj
18- function isMethod ( obj , propertyName ) {
19- const desc = Object . getOwnPropertyDescriptor ( obj , propertyName ) ;
20- return ! ! desc && typeof desc . value === 'function' ;
21- }
14+ #optionsPropertyNames = [ 'automaticOpen' , 'reconnect' , 'timeout' , 'ping' , 'newWebsocketFn' ]
2215
23- export class ReconnectingWebSocketClient extends EventEmitter {
2416 /**
2517 *
2618 * @param {string } address - The URL to which to connect
@@ -42,6 +34,12 @@ export class ReconnectingWebSocketClient extends EventEmitter {
4234 constructor ( address , protocol , options ) {
4335 super ( )
4436
37+ // Detects if the property name is a method of obj
38+ const isMethod = ( obj , propertyName ) => {
39+ const desc = Object . getOwnPropertyDescriptor ( obj , propertyName ) ;
40+ return ! ! desc && typeof desc . value === 'function' ;
41+ }
42+
4543 // Store parameters
4644 this . _address = address
4745 if ( Array . isArray ( protocol ) || typeof protocol === 'string' ) {
@@ -54,7 +52,7 @@ export class ReconnectingWebSocketClient extends EventEmitter {
5452
5553 this . _optionsWebSocket = { }
5654 Object . keys ( this . _options ) . forEach ( ( key ) => {
57- if ( ! optionsPropertyNames . includes ( key ) ) {
55+ if ( ! this . # optionsPropertyNames. includes ( key ) ) {
5856 this . _optionsWebSocket [ key ] = this . _options [ key ]
5957 }
6058 } )
@@ -262,7 +260,7 @@ export class ReconnectingWebSocketClient extends EventEmitter {
262260 * @param options
263261 */
264262 on ( eventName , listener , options ) {
265- if ( ownEventNames . includes ( eventName ) ) {
263+ if ( this . # ownEventNames. includes ( eventName ) ) {
266264 return super . on ( eventName , listener )
267265 }
268266
@@ -302,7 +300,7 @@ export class ReconnectingWebSocketClient extends EventEmitter {
302300 * Alias for `on`
303301 */
304302 addEventListener ( eventName , listener , options ) {
305- if ( ownEventNames . includes ( eventName ) ) {
303+ if ( this . # ownEventNames. includes ( eventName ) ) {
306304 return super . on ( eventName , listener )
307305 }
308306
@@ -327,9 +325,8 @@ export class ReconnectingWebSocketClient extends EventEmitter {
327325 return this
328326 }
329327
330-
331328 off ( eventName , listener ) {
332- if ( ownEventNames . includes ( eventName ) ) {
329+ if ( this . # ownEventNames. includes ( eventName ) ) {
333330 return super . removeListener ( eventName , listener )
334331 }
335332
@@ -350,7 +347,6 @@ export class ReconnectingWebSocketClient extends EventEmitter {
350347 this . addListener ( ...args )
351348 }
352349
353-
354350 /**
355351 * Returns the readyState of the underlying WebSocket or `undefined` if it does not exist.
356352 * When the underlying WebSocket object does not exist it returns `undefined`
@@ -373,7 +369,6 @@ export class ReconnectingWebSocketClient extends EventEmitter {
373369 this . ws = new ws ( this . _address , this . _protocol , this . _optionsWebSocket )
374370 }
375371
376-
377372 // When WebSocket connection is open, restart ping and timeout factories, and reset the reconnect factory
378373 this . ws . addEventListener ( 'open' , ( ) => {
379374 this . _pingFactory ?. start ( )
@@ -441,7 +436,7 @@ export class ReconnectingWebSocketClient extends EventEmitter {
441436 *
442437 * Sends data to the WebsocketServer.
443438 *
444- * The method extends `WebSocket.send()` method, so that and `Object` can be passed, in which case it is stringfied before sending.
439+ * The method extends `WebSocket.send()` method, so that and `Object` can be passed. In this case the object is stringfied before sending.
445440 *
446441 * It will throw an exception if you call send() when the connection is in the CONNECTING state or when underlying WebSocket object does not exist.
447442 *
@@ -491,5 +486,3 @@ export class ReconnectingWebSocketClient extends EventEmitter {
491486 }
492487 }
493488}
494-
495-
0 commit comments