@osimatic/helpers-js 1.0.47 → 1.0.50
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 +1 -1
- package/web_socket.js +12 -19
package/package.json
CHANGED
package/web_socket.js
CHANGED
|
@@ -5,24 +5,21 @@ class WebSocket {
|
|
|
5
5
|
* customBusEvents : events "spéciaux" en provenance de l'event bus qui seront dispatchés vers la socket : array({ 'custom_name' => 'event_name' }, ...)
|
|
6
6
|
*/
|
|
7
7
|
static setEventListeners(socketEvents, busEvents, customBusEvents) {
|
|
8
|
+
const { EventBus } = require('./event_bus');
|
|
9
|
+
|
|
10
|
+
this.eventBus = new EventBus();
|
|
8
11
|
this.socketEvents = socketEvents;
|
|
9
12
|
this.busEvents = busEvents;
|
|
10
13
|
this.customBusEvents = customBusEvents;
|
|
11
14
|
}
|
|
12
15
|
|
|
13
|
-
static getEventBus() {
|
|
14
|
-
return this.eventBus;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
16
|
static connect(url, options, connectInitPayload, onConnectionAckCallback, onUnavailableCallback) {
|
|
18
17
|
const io = require('socket.io-client');
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
this.eventBus = new EventBus();
|
|
18
|
+
|
|
22
19
|
this.logged = false;
|
|
23
20
|
this.instance = io(url, options);
|
|
24
21
|
|
|
25
|
-
this.
|
|
22
|
+
this.initEventListeners();
|
|
26
23
|
this.initConnectionListeners(onUnavailableCallback);
|
|
27
24
|
|
|
28
25
|
this.instance.on('connect', () => {
|
|
@@ -41,27 +38,23 @@ class WebSocket {
|
|
|
41
38
|
}
|
|
42
39
|
|
|
43
40
|
static clear(error, onUnavailableCallback) {
|
|
44
|
-
this.
|
|
41
|
+
this.unregisterEventListener();
|
|
45
42
|
|
|
46
43
|
if (onUnavailableCallback != 'undefined') {
|
|
47
44
|
onUnavailableCallback(error);
|
|
48
45
|
}
|
|
49
46
|
}
|
|
50
47
|
|
|
51
|
-
static
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
static unregisterOutcomingBusEvents() {
|
|
56
|
-
this.busEvents.forEach(event => this.eventBus.unsubscribe(event, {}));
|
|
57
|
-
this.customBusEvents.forEach((object) => this.eventBus.unsubscribe(object.custom_name, {}));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
static initEventsListeners() {
|
|
48
|
+
static initEventListeners() {
|
|
61
49
|
this.socketEvents.forEach(event => this.instance.on(event, (payload) => this.eventBus.publish(event, payload)));
|
|
62
50
|
this.busEvents.forEach(event => this.eventBus.subscribe(event, (payload) => this.instance.emit(event, payload)));
|
|
63
51
|
this.customBusEvents.forEach((object) => this.eventBus.subscribe(object.custom_name, (payload) => this.instance.emit(object.event_name, payload)));
|
|
64
52
|
}
|
|
53
|
+
|
|
54
|
+
static unregisterEventListener() {
|
|
55
|
+
this.busEvents.forEach(event => this.eventBus.unsubscribe(event, {}));
|
|
56
|
+
this.customBusEvents.forEach((object) => this.eventBus.unsubscribe(object.custom_name, {}));
|
|
57
|
+
}
|
|
65
58
|
}
|
|
66
59
|
|
|
67
60
|
module.exports = { WebSocket };
|