vrpc 3.0.0-alpha.1 → 3.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vrpc",
3
- "version": "3.0.0-alpha.1",
3
+ "version": "3.0.0-alpha.2",
4
4
  "description": "Variadic Remote Procedure Calls",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -429,6 +429,7 @@ class VrpcAdapter {
429
429
  }
430
430
 
431
431
  static _call (json) {
432
+ const before = Object.keys(VrpcAdapter._listeners).length
432
433
  switch (json.f) {
433
434
  case '__createIsolated__':
434
435
  VrpcAdapter._handleCreateIsolated(json)
@@ -445,6 +446,10 @@ class VrpcAdapter {
445
446
  default:
446
447
  VrpcAdapter._handleCall(json)
447
448
  }
449
+ const after = Object.keys(VrpcAdapter._listeners).length
450
+ // if we took a listener on board we must tell our caller such that
451
+ // things can be cleaned up later.
452
+ return after > before
448
453
  }
449
454
 
450
455
  static _handleCreateIsolated (json) {
@@ -596,6 +601,18 @@ class VrpcAdapter {
596
601
  delete VrpcAdapter._listeners[clientId]
597
602
  }
598
603
 
604
+ static _unregisterEventListenersOfInstance (instance) {
605
+ for (const [key, value] of Object.entries(VrpcAdapter._listeners)) {
606
+ VrpcAdapter._listeners[key] = value.filter(({ i, e, f }) => {
607
+ if (instance === i) {
608
+ VrpcAdapter.getInstance(i).removeListener(e, f)
609
+ return false
610
+ }
611
+ return true
612
+ })
613
+ }
614
+ }
615
+
599
616
  static _validate (schema, params) {
600
617
  if (!VrpcAdapter._ajv) {
601
618
  VrpcAdapter._ajv = new Ajv({ useDefaults: true })
@@ -646,7 +663,7 @@ class VrpcAdapter {
646
663
  // do not even trigger a callback for already offline clients
647
664
  if (json.s && !VrpcAdapter._listeners[json.s]) return
648
665
  const a = instanceId ? [instanceId, ...innerArgs] : [...innerArgs]
649
- VrpcAdapter._callback({ ...json, a, i: id })
666
+ VrpcAdapter._callback({ a, s: json.s, f: json.f, i: id })
650
667
  }
651
668
  return wrapper
652
669
  }
package/vrpc/VrpcAgent.js CHANGED
@@ -169,7 +169,7 @@ class VrpcAgent extends EventEmitter {
169
169
  .createHash('md5')
170
170
  .update(this._domain + this._agent)
171
171
  .digest('hex')
172
- .substr(0, 18)
172
+ .substr(0, 20)
173
173
  let username = this._username
174
174
  let password = this._password
175
175
  if (this._token) {
@@ -181,7 +181,7 @@ class VrpcAgent extends EventEmitter {
181
181
  password,
182
182
  keepalive: 30,
183
183
  connectTimeout: 10 * 1000,
184
- clientId: `vrpca${md5}`,
184
+ clientId: `va3${md5}`,
185
185
  rejectUnauthorized: false,
186
186
  will: {
187
187
  topic: `${this._baseTopic}/__agentInfo__`,
@@ -516,7 +516,11 @@ class VrpcAgent extends EventEmitter {
516
516
  json.f = method
517
517
 
518
518
  // Mutates json and adds return value
519
- VrpcAdapter._call(json)
519
+ const mustTrack = VrpcAdapter._call(json)
520
+
521
+ if (mustTrack) {
522
+ this._mqttSubscribe(`${json.s}/__clientInfo__`)
523
+ }
520
524
 
521
525
  // Intersecting life-cycle functions
522
526
  switch (method) {
@@ -616,6 +620,7 @@ class VrpcAgent extends EventEmitter {
616
620
  _unregisterInstance (instanceId, clientId) {
617
621
  const entryIsolated = this._isolatedInstances.get(clientId)
618
622
  if (entryIsolated && entryIsolated.has(instanceId)) {
623
+ VrpcAdapter._unregisterEventListenersOfInstance(instanceId)
619
624
  entryIsolated.delete(instanceId)
620
625
  if (entryIsolated.length === 0) {
621
626
  this._isolatedInstances.delete(clientId)
@@ -628,6 +633,7 @@ class VrpcAgent extends EventEmitter {
628
633
  this._sharedInstances.forEach(async v => {
629
634
  if (v.has(instanceId)) {
630
635
  found = true
636
+ VrpcAdapter._unregisterEventListenersOfInstance(instanceId)
631
637
  v.delete(instanceId)
632
638
  if (v.length === 0) {
633
639
  this._sharedInstances.delete(clientId)
@@ -810,9 +810,8 @@ class VrpcClient extends EventEmitter {
810
810
  .createHash('md5')
811
811
  .update(clientInfo)
812
812
  .digest('hex')
813
- .substr(0, 13)
814
- // FIXME (3.x): use vrpcp -> vrpcc
815
- return `vc${this._instance}${md5}` // 2 + 8 + 13 = 23 (max clientId)
813
+ .substr(0, 12)
814
+ return `vc3${this._instance}${md5}` // 3 + 8 + 12 = 23 (max clientId)
816
815
  }
817
816
 
818
817
  _createVrpcClientId () {