@osimatic/helpers-js 1.0.46 → 1.0.49

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 ADDED
@@ -0,0 +1,37 @@
1
+ class EventBus {
2
+ constructor() {
3
+ this.events = {};
4
+ }
5
+
6
+ publish(name, data) {
7
+ var handlers = this.events[name];
8
+
9
+ if (!!handlers === false) {
10
+ return;
11
+ }
12
+
13
+ handlers.forEach((handler) => handler.call(this, data));
14
+ }
15
+
16
+ subscribe(name, handler) {
17
+ var handlers = this.events[name];
18
+
19
+ if (!!handlers === false) {
20
+ handlers = this.events[name] = [];
21
+ }
22
+
23
+ handlers.push(handler);
24
+ }
25
+
26
+ unsubscribe(name, handler) {
27
+ var handlers = this.events[name];
28
+
29
+ if (!!handlers === false) {
30
+ return;
31
+ }
32
+
33
+ handlers.splice(handlers.indexOf(handler));
34
+ }
35
+ }
36
+
37
+ module.exports = { EventBus };
package/index.js CHANGED
@@ -35,18 +35,19 @@ const { ImportFromCsv } = require('./import_from_csv');
35
35
  const { JwtToken, JwtSession } = require('./jwt');
36
36
  const { ListBox } = require('./list_box');
37
37
  const { WebRTC } = require('./web_rtc');
38
+ const { EventBus } = require('./event_bus');
38
39
 
39
40
  // exports surcouche lib externe
40
41
  const { GoogleCharts } = require('./google_charts');
41
42
  const { GoogleRecaptcha } = require('./google_recaptcha');
42
43
  const { GoogleMap } = require('./google_maps');
43
44
  const { OpenStreetMap } = require('./open_street_map');
44
- const { WebSocket, EventBus } = require('./web_socket');
45
+ const { WebSocket } = require('./web_socket');
45
46
 
46
47
  module.exports = {
47
48
  Array, Object, Number, String,
48
49
  HTTPRequest, Cookie, UrlAndQueryString, IBAN, BankCard, AudioMedia, UserMedia, PersonName, Email, TelephoneNumber, DateTime, TimestampUnix, SqlDate, SqlTime, SqlDateTime, Duration, File, CSV, Img, FormHelper, Country, PostalAddress, GeographicCoordinates, SocialNetwork,
49
- Browser, DataTable, Pagination, Navigation, DetailsSubArray, SelectAll, MultipleActionInTable, FormDate, InputPeriod, ShoppingCart, FlashMessage, CountDown, ImportFromCsv, JwtToken, JwtSession, ListBox, WebRTC,
50
+ Browser, DataTable, Pagination, Navigation, DetailsSubArray, SelectAll, MultipleActionInTable, FormDate, InputPeriod, ShoppingCart, FlashMessage, CountDown, ImportFromCsv, JwtToken, JwtSession, ListBox, WebRTC, WebSocket, EventBus,
50
51
  sleep, refresh, chr, ord, trim, empty,
51
52
  GoogleCharts, GoogleRecaptcha, GoogleMap, OpenStreetMap
52
53
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.46",
3
+ "version": "1.0.49",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/web_socket.js CHANGED
@@ -11,17 +11,22 @@ class WebSocket {
11
11
  }
12
12
 
13
13
  static getEventBus() {
14
+ if (this.eventBus == 'undefined') {
15
+ const { EventBus } = require('./event_bus');
16
+
17
+ this.eventBus = new EventBus();
18
+ }
19
+
14
20
  return this.eventBus;
15
21
  }
16
22
 
17
23
  static connect(url, options, connectInitPayload, onConnectionAckCallback, onUnavailableCallback) {
18
24
  const io = require('socket.io-client');
19
25
 
20
- this.eventBus = new EventBus();
21
26
  this.logged = false;
22
27
  this.instance = io(url, options);
23
28
 
24
- this.initEventsListeners();
29
+ this.initEventListeners();
25
30
  this.initConnectionListeners(onUnavailableCallback);
26
31
 
27
32
  this.instance.on('connect', () => {
@@ -40,63 +45,23 @@ class WebSocket {
40
45
  }
41
46
 
42
47
  static clear(error, onUnavailableCallback) {
43
- this.unregisterOutcomingBusEvents();
48
+ this.unregisterEventListener();
44
49
 
45
50
  if (onUnavailableCallback != 'undefined') {
46
51
  onUnavailableCallback(error);
47
52
  }
48
53
  }
49
54
 
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() {
55
+ static initEventListeners() {
60
56
  this.socketEvents.forEach(event => this.instance.on(event, (payload) => this.eventBus.publish(event, payload)));
61
57
  this.busEvents.forEach(event => this.eventBus.subscribe(event, (payload) => this.instance.emit(event, payload)));
62
58
  this.customBusEvents.forEach((object) => this.eventBus.subscribe(object.custom_name, (payload) => this.instance.emit(object.event_name, payload)));
63
59
  }
64
- }
65
60
 
66
- class EventBus {
67
- constructor() {
68
- this.events = {};
61
+ static unregisterEventListener() {
62
+ this.busEvents.forEach(event => this.eventBus.unsubscribe(event, {}));
63
+ this.customBusEvents.forEach((object) => this.eventBus.unsubscribe(object.custom_name, {}));
69
64
  }
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
65
  }
101
66
 
102
- module.exports = { WebSocket, EventBus };
67
+ module.exports = { WebSocket };