@@ -21,6 +21,16 @@ import debug from '../Debug'
2121import Socket from '../Socket/index.js'
2222import JsonEncoder from '../JsonEncoder/index.js'
2323
24+ /**
25+ * Connection class is used to make a TCP/Socket connection
26+ * with the server. It relies on Native Websocket browser
27+ * support.
28+ *
29+ * @class Connection
30+ *
31+ * @param {String } url
32+ * @param {Object } options
33+ */
2434export default class Connection extends Emitter {
2535 constructor ( url , options ) {
2636 super ( )
@@ -55,20 +65,6 @@ export default class Connection extends Emitter {
5565 */
5666 this . _reconnectionAttempts = 0
5767
58- /**
59- * Base URL for the websocket connection
60- *
61- * @type {String }
62- */
63- this . url = `${ url . replace ( / \/ $ / , '' ) } /${ this . options . path } `
64-
65- /**
66- * Subscriptions for a single connection
67- *
68- * @type {Object }
69- */
70- this . subscriptions = { }
71-
7268 /**
7369 * All packets are sent in sequence to the server. So we need to
7470 * maintain a queue and process one at a time
@@ -100,6 +96,20 @@ export default class Connection extends Emitter {
10096 */
10197 this . _extendedQuery = { }
10298
99+ /**
100+ * Base URL for the websocket connection
101+ *
102+ * @type {String }
103+ */
104+ this . _url = `${ url . replace ( / \/ $ / , '' ) } /${ this . options . path } `
105+
106+ /**
107+ * Subscriptions for a single connection
108+ *
109+ * @type {Object }
110+ */
111+ this . subscriptions = { }
112+
103113 /**
104114 * Handler called when `close` is emitted from the
105115 * subscription
@@ -122,6 +132,21 @@ export default class Connection extends Emitter {
122132 this . options . reconnectionAttempts > this . _reconnectionAttempts
123133 }
124134
135+ /**
136+ * Clean references
137+ *
138+ * @method _cleanup
139+ *
140+ * @return {void }
141+ *
142+ * @private
143+ */
144+ _cleanup ( ) {
145+ clearInterval ( this . _pingTimer )
146+ this . ws = null
147+ this . _pingTimer = null
148+ }
149+
125150 /**
126151 * Calls a callback passing subscription to it
127152 *
@@ -134,9 +159,7 @@ export default class Connection extends Emitter {
134159 * @private
135160 */
136161 _subscriptionsIterator ( callback ) {
137- Object . keys ( this . subscriptions ) . forEach ( ( sub ) => {
138- callback ( this . subscriptions [ sub ] , sub )
139- } )
162+ Object . keys ( this . subscriptions ) . forEach ( ( sub ) => callback ( this . subscriptions [ sub ] , sub ) )
140163 }
141164
142165 /**
@@ -156,7 +179,7 @@ export default class Connection extends Emitter {
156179 const socket = this . getSubscription ( packet . d . topic )
157180
158181 if ( ! socket ) {
159- debug ( 'cannot acknowledge join since topic has no subscription %j' , packet )
182+ debug ( 'cannot consume packet since %s topic has no active subscription %j' , packet . d . topic , packet )
160183 return
161184 }
162185
@@ -228,6 +251,7 @@ export default class Connection extends Emitter {
228251 * @private
229252 */
230253 _onError ( event ) {
254+ debug ( 'error %O' , event )
231255 this . _subscriptionsIterator ( ( subscription ) => ( subscription . serverError ( ) ) )
232256 this . emit ( 'error' , event )
233257 }
@@ -245,8 +269,9 @@ export default class Connection extends Emitter {
245269 _reconnect ( ) {
246270 this . _reconnectionAttempts ++
247271
248- const timer = setTimeout ( ( ) => {
249- clearTimeout ( timer )
272+ this . emit ( 'reconnect' , this . _reconnectionAttempts )
273+
274+ setTimeout ( ( ) => {
250275 this . _connectionState = 'reconnect'
251276 this . connect ( )
252277 } , this . options . reconnectionDelay * this . _reconnectionAttempts )
@@ -264,11 +289,11 @@ export default class Connection extends Emitter {
264289 * @private
265290 */
266291 _onClose ( event ) {
267- clearInterval ( this . _pingTimer )
268-
269- this . ws = null
270- this . _pingTimer = null
292+ this . _cleanup ( )
271293
294+ /**
295+ * Force subscriptions to terminate
296+ */
272297 this . _subscriptionsIterator ( ( subscription ) => subscription . terminate ( ) )
273298
274299 this
@@ -391,6 +416,7 @@ export default class Connection extends Emitter {
391416 /**
392417 * Sending packets to make pending subscriptions
393418 */
419+ debug ( 'processing pre connection subscriptions' )
394420 this . _subscriptionsIterator ( ( subscription ) => {
395421 this . sendPacket ( wsp . joinPacket ( subscription . topic ) )
396422 } )
@@ -495,7 +521,7 @@ export default class Connection extends Emitter {
495521 */
496522 connect ( ) {
497523 const query = stringify ( extend ( { } , this . options . query , this . _extendedQuery ) )
498- const url = query ? `${ this . url } ?${ query } ` : this . url
524+ const url = query ? `${ this . _url } ?${ query } ` : this . _url
499525
500526 debug ( 'creating socket connection on %s url' , url )
501527
0 commit comments