vrpc 3.0.0 → 3.0.1
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/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/vrpc/VrpcAdapter.js +41 -28
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
|
6
6
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [3.0.1] - Jan 24 2022
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
- non-functional event un-subscription on silent client disconnect and when
|
|
13
|
+
initially registered via `callAll`
|
|
14
|
+
|
|
8
15
|
## [3.0.0] - Jan 20 2022
|
|
9
16
|
|
|
10
17
|
### Added
|
package/package.json
CHANGED
package/vrpc/VrpcAdapter.js
CHANGED
|
@@ -101,7 +101,7 @@ class VrpcAdapter {
|
|
|
101
101
|
* NOTE: This function currently only supports registration of classes (either
|
|
102
102
|
* when provided as object or when exported on the provided module path)
|
|
103
103
|
*/
|
|
104
|
-
|
|
104
|
+
static register (code, options = {}) {
|
|
105
105
|
if (typeof code === 'string') {
|
|
106
106
|
const absPath = path.resolve(caller(), '../', code)
|
|
107
107
|
const relPath = path.relative(__dirname, absPath)
|
|
@@ -609,14 +609,12 @@ class VrpcAdapter {
|
|
|
609
609
|
static _unregisterClient (clientId) {
|
|
610
610
|
for (const [key, value] of Object.entries(VrpcAdapter._listeners)) {
|
|
611
611
|
const { instance, event, listener, clients } = value
|
|
612
|
-
|
|
613
|
-
if (
|
|
612
|
+
clients.delete(clientId)
|
|
613
|
+
if (clients.size === 0) {
|
|
614
614
|
if (event) {
|
|
615
615
|
VrpcAdapter.getInstance(instance).removeListener(event, listener)
|
|
616
616
|
}
|
|
617
617
|
delete VrpcAdapter._listeners[key]
|
|
618
|
-
} else {
|
|
619
|
-
VrpcAdapter._listeners[key].clients = filteredClients
|
|
620
618
|
}
|
|
621
619
|
}
|
|
622
620
|
}
|
|
@@ -630,42 +628,59 @@ class VrpcAdapter {
|
|
|
630
628
|
}
|
|
631
629
|
|
|
632
630
|
static _unwrapArguments (json, instanceId) {
|
|
633
|
-
const
|
|
634
|
-
|
|
631
|
+
const sender = json.s
|
|
632
|
+
let context = json.c
|
|
633
|
+
let func = json.f
|
|
634
|
+
let args = json.a
|
|
635
|
+
// things are different if we came from callAll (instanceId !== undefined)
|
|
636
|
+
if (instanceId) {
|
|
637
|
+
context = instanceId
|
|
638
|
+
func = args[0]
|
|
639
|
+
args = args.slice(1)
|
|
640
|
+
}
|
|
641
|
+
const unwrapped = instanceId ? [func] : []
|
|
635
642
|
for (const arg of args) {
|
|
636
643
|
// find those args that actually need to be functions
|
|
637
644
|
if (typeof arg === 'string' && arg.slice(0, 5) === '__f__') {
|
|
638
|
-
const callback = this._generateCallback(arg,
|
|
645
|
+
const callback = this._generateCallback(arg, sender, instanceId)
|
|
639
646
|
unwrapped.push(callback)
|
|
640
647
|
} else if (typeof arg === 'string' && arg.slice(0, 5) === '__e__') {
|
|
641
|
-
const listener = this._generateListener(arg,
|
|
648
|
+
const listener = this._generateListener(arg, sender, instanceId)
|
|
642
649
|
unwrapped.push(listener)
|
|
643
650
|
// start tracking clients that are registering event listeners
|
|
644
651
|
if (!VrpcAdapter._listeners[arg]) {
|
|
645
652
|
VrpcAdapter._listeners[arg] = {
|
|
646
653
|
listener,
|
|
647
|
-
clients: [sender],
|
|
648
|
-
|
|
654
|
+
clients: new Set([sender]),
|
|
655
|
+
instances: new Set(),
|
|
649
656
|
event: null
|
|
650
657
|
}
|
|
651
658
|
} else {
|
|
652
|
-
VrpcAdapter._listeners[arg].clients.
|
|
659
|
+
VrpcAdapter._listeners[arg].clients.add(sender)
|
|
653
660
|
}
|
|
654
661
|
// see whether the requested listener is an EventEmitter registration
|
|
655
662
|
if (func === 'on' || func === 'addListener') {
|
|
656
|
-
// if this event is already registered
|
|
657
|
-
|
|
663
|
+
// if this event is already registered on the given instance
|
|
664
|
+
// avoid a second registration
|
|
665
|
+
if (
|
|
666
|
+
VrpcAdapter._listeners[arg].event &&
|
|
667
|
+
VrpcAdapter._listeners[arg].instances.has(context)
|
|
668
|
+
) {
|
|
658
669
|
return null // means that this call won't be forwarded
|
|
659
670
|
}
|
|
660
671
|
VrpcAdapter._listeners[arg].event = args[0]
|
|
672
|
+
VrpcAdapter._listeners[arg].instances.add(context)
|
|
661
673
|
}
|
|
662
674
|
if (func === 'off' || func === 'removeListener') {
|
|
663
|
-
const {
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
}
|
|
675
|
+
const {
|
|
676
|
+
clients,
|
|
677
|
+
instances,
|
|
678
|
+
listener,
|
|
679
|
+
event
|
|
680
|
+
} = VrpcAdapter._listeners[arg]
|
|
681
|
+
clients.delete(sender)
|
|
682
|
+
instances.delete(context)
|
|
683
|
+
if (clients.size === 0) {
|
|
669
684
|
delete VrpcAdapter._listeners[arg]
|
|
670
685
|
VrpcAdapter.getInstance(context).off(event, listener)
|
|
671
686
|
}
|
|
@@ -682,10 +697,8 @@ class VrpcAdapter {
|
|
|
682
697
|
static _removeAllListeners (event, sender, context) {
|
|
683
698
|
for (const [key, value] of Object.entries(VrpcAdapter._listeners)) {
|
|
684
699
|
if (value.event === event) {
|
|
685
|
-
|
|
686
|
-
if (
|
|
687
|
-
VrpcAdapter._listeners[key].clients = filteredClients
|
|
688
|
-
} else {
|
|
700
|
+
value.clients.delete(sender)
|
|
701
|
+
if (value.clients.size === 0) {
|
|
689
702
|
delete VrpcAdapter._listeners[key]
|
|
690
703
|
VrpcAdapter.getInstance(context).off(event, value.listener)
|
|
691
704
|
}
|
|
@@ -693,21 +706,21 @@ class VrpcAdapter {
|
|
|
693
706
|
}
|
|
694
707
|
}
|
|
695
708
|
|
|
696
|
-
static _generateCallback (id,
|
|
709
|
+
static _generateCallback (id, sender, instanceId) {
|
|
697
710
|
return (...innerArgs) => {
|
|
698
711
|
// callAll expects the instanceId as first argument
|
|
699
712
|
const a = instanceId ? [instanceId, ...innerArgs] : [...innerArgs]
|
|
700
|
-
VrpcAdapter._callback({ a, s:
|
|
713
|
+
VrpcAdapter._callback({ a, s: sender, i: id })
|
|
701
714
|
}
|
|
702
715
|
}
|
|
703
716
|
|
|
704
|
-
static _generateListener (id,
|
|
717
|
+
static _generateListener (id, sender, instanceId) {
|
|
705
718
|
return (...innerArgs) => {
|
|
706
719
|
// do not even emit for already offline clients
|
|
707
720
|
if (!VrpcAdapter._listeners[id]) return
|
|
708
721
|
// callAll expects the instanceId as first argument
|
|
709
722
|
const a = instanceId ? [instanceId, ...innerArgs] : [...innerArgs]
|
|
710
|
-
VrpcAdapter._callback({ a, s:
|
|
723
|
+
VrpcAdapter._callback({ a, s: sender, i: id })
|
|
711
724
|
}
|
|
712
725
|
}
|
|
713
726
|
|