@osimatic/helpers-js 1.0.49 → 1.0.52

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/event_bus.js CHANGED
@@ -10,7 +10,9 @@ class EventBus {
10
10
  return;
11
11
  }
12
12
 
13
- handlers.forEach((handler) => handler.call(this, data));
13
+ handlers.forEach(function(handler) {
14
+ handler.call(this, data)
15
+ });
14
16
  }
15
17
 
16
18
  subscribe(name, handler) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.49",
3
+ "version": "1.0.52",
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,94 @@ 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();
11
+ this.socketConnectionErrors = ['connect_error', 'connect_timeout', 'reconnect_error', 'reconnect_failed'];
8
12
  this.socketEvents = socketEvents;
9
13
  this.busEvents = busEvents;
10
14
  this.customBusEvents = customBusEvents;
11
15
  }
12
16
 
13
- static getEventBus() {
14
- if (this.eventBus == 'undefined') {
15
- const { EventBus } = require('./event_bus');
17
+ static connect(url, options, connectInitPayload, onUnavailableCallback, onConnectionAckCallback) {
18
+ const { io } = require('socket.io-client');
16
19
 
17
- this.eventBus = new EventBus();
18
- }
20
+ this.logged = false;
21
+ this.instance = io(url, options);
19
22
 
20
- return this.eventBus;
21
- }
23
+ this.registerErrorListeners(onUnavailableCallback);
24
+ this.registerEventListeners(connectInitPayload, onConnectionAckCallback, onUnavailableCallback);
25
+ }
22
26
 
23
- static connect(url, options, connectInitPayload, onConnectionAckCallback, onUnavailableCallback) {
24
- const io = require('socket.io-client');
27
+ static registerErrorListeners(onUnavailableCallback) {
28
+ var self = this;
25
29
 
26
- this.logged = false;
27
- this.instance = io(url, options);
30
+ self.socketConnectionErrors.forEach(function(event) {
31
+ self.instance.on(event, function() {
32
+ self.clear(onUnavailableCallback, event);
33
+ });
34
+ });
28
35
 
29
- this.initEventListeners();
30
- this.initConnectionListeners(onUnavailableCallback);
36
+ self.instance.on('error', function(error) {
37
+ self.clear(onUnavailableCallback, error);
38
+ });
39
+ }
40
+
41
+ static registerEventListeners(connectInitPayload, onConnectionAckCallback, onUnavailableCallback) {
42
+ var self = this;
31
43
 
32
- this.instance.on('connect', () => {
33
- if (this.logged) {
34
- this.clear(onUnavailableCallback);
44
+ self.instance.on('connect', function() {
45
+ if (self.logged) { //reconnexion avant timeout : on supprime les listeners de l'event bus
46
+ self.clear();
35
47
  }
36
48
 
37
- this.logged = true;
38
- this.instance.emit('connect_init', connectInitPayload, onConnectionAckCallback);
49
+ self.logged = true;
50
+
51
+ self.instance.emit('connect_init', connectInitPayload, onConnectionAckCallback);
52
+ });
53
+
54
+ self.instance.on('disconnect', function() {
55
+ self.clear(onUnavailableCallback);
56
+ });
57
+
58
+ self.socketEvents.forEach(function (event) {
59
+ self.instance.on(event, function(payload) {
60
+ self.eventBus.publish(event, payload);
61
+ });
62
+ });
63
+
64
+ self.busEvents.forEach(function(event) {
65
+ self.eventBus.subscribe(event, function(payload) {
66
+ self.instance.emit(event, payload);
67
+ });
68
+ });
69
+
70
+ self.customBusEvents.forEach(function(object) {
71
+ self.eventBus.subscribe(object.custom_name, function(payload) {
72
+ self.instance.emit(object.event_name, payload)
73
+ });
39
74
  });
40
75
  }
41
76
 
42
- static initConnectionListeners(onUnavailableCallback) {
43
- ['connect_error', 'connect_timeout', 'reconnect_error', 'reconnect_failed', 'disconnect'].forEach(event => this.instance.on(event, () => this.clear(event, onUnavailableCallback)));
44
- this.instance.on('error', (error) => this.clear(error, onUnavailableCallback));
77
+ static unregisterEventListener() {
78
+ var self = this;
79
+
80
+ self.busEvents.forEach(function (event) {
81
+ self.eventBus.unsubscribe(event, {});
82
+ });
83
+
84
+ self.customBusEvents.forEach(function(object) {
85
+ self.eventBus.unsubscribe(object.custom_name, {});
86
+ });
45
87
  }
46
88
 
47
- static clear(error, onUnavailableCallback) {
89
+ static clear(onUnavailableCallback, error) {
48
90
  this.unregisterEventListener();
49
-
50
- if (onUnavailableCallback != 'undefined') {
91
+
92
+ if (typeof onUnavailableCallback !== 'undefined') {
51
93
  onUnavailableCallback(error);
52
94
  }
53
95
  }
54
-
55
- static initEventListeners() {
56
- this.socketEvents.forEach(event => this.instance.on(event, (payload) => this.eventBus.publish(event, payload)));
57
- this.busEvents.forEach(event => this.eventBus.subscribe(event, (payload) => this.instance.emit(event, payload)));
58
- this.customBusEvents.forEach((object) => this.eventBus.subscribe(object.custom_name, (payload) => this.instance.emit(object.event_name, payload)));
59
- }
60
-
61
- static unregisterEventListener() {
62
- this.busEvents.forEach(event => this.eventBus.unsubscribe(event, {}));
63
- this.customBusEvents.forEach((object) => this.eventBus.unsubscribe(object.custom_name, {}));
64
- }
65
96
  }
66
97
 
67
98
  module.exports = { WebSocket };