@osimatic/helpers-js 1.0.51 → 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.51",
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
@@ -8,34 +8,82 @@ class WebSocket {
8
8
  const { EventBus } = require('./event_bus');
9
9
 
10
10
  this.eventBus = new EventBus();
11
+ this.socketConnectionErrors = ['connect_error', 'connect_timeout', 'reconnect_error', 'reconnect_failed'];
11
12
  this.socketEvents = socketEvents;
12
13
  this.busEvents = busEvents;
13
14
  this.customBusEvents = customBusEvents;
14
15
  }
15
16
 
16
17
  static connect(url, options, connectInitPayload, onUnavailableCallback, onConnectionAckCallback) {
17
- const io = require('socket.io-client');
18
-
19
- this.logged = false;
18
+ const { io } = require('socket.io-client');
19
+
20
+ this.logged = false;
20
21
  this.instance = io(url, options);
21
22
 
22
- this.initEventListeners();
23
- this.initConnectionListeners(onUnavailableCallback);
23
+ this.registerErrorListeners(onUnavailableCallback);
24
+ this.registerEventListeners(connectInitPayload, onConnectionAckCallback, onUnavailableCallback);
25
+ }
26
+
27
+ static registerErrorListeners(onUnavailableCallback) {
28
+ var self = this;
29
+
30
+ self.socketConnectionErrors.forEach(function(event) {
31
+ self.instance.on(event, function() {
32
+ self.clear(onUnavailableCallback, event);
33
+ });
34
+ });
35
+
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;
24
43
 
25
- this.instance.on('connect', () => {
26
- if (this.logged) {
27
- this.clear();
44
+ self.instance.on('connect', function() {
45
+ if (self.logged) { //reconnexion avant timeout : on supprime les listeners de l'event bus
46
+ self.clear();
28
47
  }
29
48
 
30
- this.logged = true;
49
+ self.logged = true;
50
+
51
+ self.instance.emit('connect_init', connectInitPayload, onConnectionAckCallback);
52
+ });
31
53
 
32
- this.instance.emit('connect_init', connectInitPayload, onConnectionAckCallback);
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
+ });
33
74
  });
34
75
  }
35
76
 
36
- static initConnectionListeners(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));
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
+ });
39
87
  }
40
88
 
41
89
  static clear(onUnavailableCallback, error) {
@@ -45,17 +93,6 @@ class WebSocket {
45
93
  onUnavailableCallback(error);
46
94
  }
47
95
  }
48
-
49
- static initEventListeners() {
50
- this.socketEvents.forEach(event => this.instance.on(event, (payload) => this.eventBus.publish(event, payload)));
51
- this.busEvents.forEach(event => this.eventBus.subscribe(event, (payload) => this.instance.emit(event, payload)));
52
- this.customBusEvents.forEach((object) => this.eventBus.subscribe(object.custom_name, (payload) => this.instance.emit(object.event_name, payload)));
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
- }
59
96
  }
60
97
 
61
98
  module.exports = { WebSocket };