Skip to content

Commit 4bd1379

Browse files
committed
refactor(*): cleanup code and tests
1 parent 964c648 commit 4bd1379

File tree

10 files changed

+287
-288
lines changed

10 files changed

+287
-288
lines changed

package-lock.json

Lines changed: 35 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"karma-japa": "^1.0.2",
3535
"karma-rollup-preprocessor": "^5.1.1",
3636
"karma-sourcemap-loader": "^0.3.7",
37+
"pify": "^3.0.0",
3738
"puppeteer": "^1.2.0",
3839
"rollup": "^0.57.0",
3940
"rollup-plugin-babel": "^3.0.3",

src/Connection/index.js

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ import debug from '../Debug'
2121
import Socket from '../Socket/index.js'
2222
import 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+
*/
2434
export 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

src/Debug/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
'use strict'
2+
3+
/*
4+
* adonis-websocket-client
5+
*
6+
* (c) Harminder Virk <virk@adonisjs.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
112
import Debug from 'debug'
213
Debug.enable('adonis:*')
314
export default Debug('adonis:websocket')

src/JsonEncoder/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
* file that was distributed with this source code.
1010
*/
1111

12+
/**
13+
* The default encoder to encode packets.
14+
*/
1215
export default {
1316
name: 'json',
1417
/**

src/Socket/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
import Emitter from 'emittery'
1313
import wsp from '@adonisjs/websocket-packet'
1414

15+
/**
16+
* Socket class holds details for a single subscription. The instance
17+
* of this class can be used to exchange messages with the server
18+
* on a given topic.
19+
*
20+
* @class Socket
21+
*/
1522
export default class Socket {
1623
constructor (topic, connection) {
1724
this.topic = topic
@@ -66,7 +73,7 @@ export default class Socket {
6673
this.emitter.emit('ready', this)
6774

6875
/**
69-
* Process queued events, before the subscription was ready
76+
* Process queued events
7077
*/
7178
this._emitBuffer.forEach((buf) => (this.emit(buf.event, buf.data)))
7279
this._emitBuffer = []

0 commit comments

Comments
 (0)