@osimatic/helpers-js 1.0.45 → 1.0.46

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/index.js CHANGED
@@ -41,6 +41,7 @@ const { GoogleCharts } = require('./google_charts');
41
41
  const { GoogleRecaptcha } = require('./google_recaptcha');
42
42
  const { GoogleMap } = require('./google_maps');
43
43
  const { OpenStreetMap } = require('./open_street_map');
44
+ const { WebSocket, EventBus } = require('./web_socket');
44
45
 
45
46
  module.exports = {
46
47
  Array, Object, Number, String,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.45",
3
+ "version": "1.0.46",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/web_socket.js ADDED
@@ -0,0 +1,102 @@
1
+ class WebSocket {
2
+ /**
3
+ * socketEvents : events en provenance de la socket qui seront dispatchés à travers le bus event
4
+ * busEvents : events en provenance de l'event bus qui seront dispatchés vers la socket
5
+ * customBusEvents : events "spéciaux" en provenance de l'event bus qui seront dispatchés vers la socket : array({ 'custom_name' => 'event_name' }, ...)
6
+ */
7
+ static setEventListeners(socketEvents, busEvents, customBusEvents) {
8
+ this.socketEvents = socketEvents;
9
+ this.busEvents = busEvents;
10
+ this.customBusEvents = customBusEvents;
11
+ }
12
+
13
+ static getEventBus() {
14
+ return this.eventBus;
15
+ }
16
+
17
+ static connect(url, options, connectInitPayload, onConnectionAckCallback, onUnavailableCallback) {
18
+ const io = require('socket.io-client');
19
+
20
+ this.eventBus = new EventBus();
21
+ this.logged = false;
22
+ this.instance = io(url, options);
23
+
24
+ this.initEventsListeners();
25
+ this.initConnectionListeners(onUnavailableCallback);
26
+
27
+ this.instance.on('connect', () => {
28
+ if (this.logged) {
29
+ this.clear(onUnavailableCallback);
30
+ }
31
+
32
+ this.logged = true;
33
+ this.instance.emit('connect_init', connectInitPayload, onConnectionAckCallback);
34
+ });
35
+ }
36
+
37
+ static initConnectionListeners(onUnavailableCallback) {
38
+ ['connect_error', 'connect_timeout', 'reconnect_error', 'reconnect_failed', 'disconnect'].forEach(event => this.instance.on(event, () => this.clear(event, onUnavailableCallback)));
39
+ this.instance.on('error', (error) => this.clear(error, onUnavailableCallback));
40
+ }
41
+
42
+ static clear(error, onUnavailableCallback) {
43
+ this.unregisterOutcomingBusEvents();
44
+
45
+ if (onUnavailableCallback != 'undefined') {
46
+ onUnavailableCallback(error);
47
+ }
48
+ }
49
+
50
+ static unregisterIncomingBusEvents() {
51
+
52
+ }
53
+
54
+ static unregisterOutcomingBusEvents() {
55
+ this.busEvents.forEach(event => this.eventBus.unsubscribe(event, {}));
56
+ this.customBusEvents.forEach((object) => this.eventBus.unsubscribe(object.custom_name, {}));
57
+ }
58
+
59
+ static initEventsListeners() {
60
+ this.socketEvents.forEach(event => this.instance.on(event, (payload) => this.eventBus.publish(event, payload)));
61
+ this.busEvents.forEach(event => this.eventBus.subscribe(event, (payload) => this.instance.emit(event, payload)));
62
+ this.customBusEvents.forEach((object) => this.eventBus.subscribe(object.custom_name, (payload) => this.instance.emit(object.event_name, payload)));
63
+ }
64
+ }
65
+
66
+ class EventBus {
67
+ constructor() {
68
+ this.events = {};
69
+ }
70
+
71
+ publish(name, data) {
72
+ var handlers = this.events[name];
73
+
74
+ if (!!handlers === false) {
75
+ return;
76
+ }
77
+
78
+ handlers.forEach((handler) => handler.call(this, data));
79
+ }
80
+
81
+ subscribe(name, handler) {
82
+ var handlers = this.events[name];
83
+
84
+ if (!!handlers === false) {
85
+ handlers = this.events[name] = [];
86
+ }
87
+
88
+ handlers.push(handler);
89
+ }
90
+
91
+ unsubscribe(name, handler) {
92
+ var handlers = this.events[name];
93
+
94
+ if (!!handlers === false) {
95
+ return;
96
+ }
97
+
98
+ handlers.splice(handlers.indexOf(handler));
99
+ }
100
+ }
101
+
102
+ module.exports = { WebSocket, EventBus };
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="WEB_MODULE" version="4">
3
- <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$" />
5
- <orderEntry type="inheritedJdk" />
6
- <orderEntry type="sourceFolder" forTests="false" />
7
- </component>
8
- </module>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/helpers-js.iml" filepath="$PROJECT_DIR$/.idea/helpers-js.iml" />
6
- </modules>
7
- </component>
8
- </project>
package/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
- </component>
6
- </project>