@osimatic/helpers-js 1.0.48 → 1.0.51

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/web_socket.js +21 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.48",
3
+ "version": "1.0.51",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/web_socket.js CHANGED
@@ -5,63 +5,57 @@ 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
- static connect(url, options, connectInitPayload, onConnectionAckCallback, onUnavailableCallback) {
16
+ static connect(url, options, connectInitPayload, onUnavailableCallback, onConnectionAckCallback) {
18
17
  const io = require('socket.io-client');
19
- const { EventBus } = require('./event_bus');
20
-
21
- this.eventBus = new EventBus();
18
+
22
19
  this.logged = false;
23
20
  this.instance = io(url, options);
24
21
 
25
- this.initEventsListeners();
22
+ this.initEventListeners();
26
23
  this.initConnectionListeners(onUnavailableCallback);
27
24
 
28
25
  this.instance.on('connect', () => {
29
26
  if (this.logged) {
30
- this.clear(onUnavailableCallback);
27
+ this.clear();
31
28
  }
32
29
 
33
30
  this.logged = true;
34
- this.instance.emit('connect_init', connectInitPayload, onConnectionAckCallback);
31
+
32
+ this.instance.emit('connect_init', connectInitPayload, onConnectionAckCallback);
35
33
  });
36
34
  }
37
35
 
38
36
  static initConnectionListeners(onUnavailableCallback) {
39
- ['connect_error', 'connect_timeout', 'reconnect_error', 'reconnect_failed', 'disconnect'].forEach(event => this.instance.on(event, () => this.clear(event, onUnavailableCallback)));
40
- this.instance.on('error', (error) => this.clear(error, onUnavailableCallback));
37
+ ['connect_error', 'connect_timeout', 'reconnect_error', 'reconnect_failed', 'disconnect'].forEach(event => this.instance.on(event, () => this.clear(onUnavailableCallback, event)));
38
+ this.instance.on('error', (error) => this.clear(onUnavailableCallback, error));
41
39
  }
42
40
 
43
- static clear(error, onUnavailableCallback) {
44
- this.unregisterOutcomingBusEvents();
45
-
46
- if (onUnavailableCallback != 'undefined') {
41
+ static clear(onUnavailableCallback, error) {
42
+ this.unregisterEventListener();
43
+
44
+ if (typeof onUnavailableCallback !== 'undefined') {
47
45
  onUnavailableCallback(error);
48
46
  }
49
47
  }
50
48
 
51
- static unregisterIncomingBusEvents() {
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() {
49
+ static initEventListeners() {
61
50
  this.socketEvents.forEach(event => this.instance.on(event, (payload) => this.eventBus.publish(event, payload)));
62
51
  this.busEvents.forEach(event => this.eventBus.subscribe(event, (payload) => this.instance.emit(event, payload)));
63
52
  this.customBusEvents.forEach((object) => this.eventBus.subscribe(object.custom_name, (payload) => this.instance.emit(object.event_name, payload)));
64
53
  }
54
+
55
+ static unregisterEventListener() {
56
+ this.busEvents.forEach(event => this.eventBus.unsubscribe(event, {}));
57
+ this.customBusEvents.forEach((object) => this.eventBus.unsubscribe(object.custom_name, {}));
58
+ }
65
59
  }
66
60
 
67
61
  module.exports = { WebSocket };