mobility-toolbox-js 2.0.0-beta.34 → 2.0.0-beta.37

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 (195) hide show
  1. package/README.md +14 -8
  2. package/api/RealtimeAPI.d.ts +6 -6
  3. package/api/RealtimeAPI.d.ts.map +1 -1
  4. package/api/RealtimeAPI.js +170 -197
  5. package/api/RealtimeAPI.test.d.ts +2 -0
  6. package/api/RealtimeAPI.test.d.ts.map +1 -0
  7. package/api/RealtimeAPI.test.js +67 -0
  8. package/api/RoutingAPI.d.ts +6 -2
  9. package/api/RoutingAPI.d.ts.map +1 -1
  10. package/api/RoutingAPI.js +8 -38
  11. package/api/RoutingAPI.test.d.ts +2 -0
  12. package/api/RoutingAPI.test.d.ts.map +1 -0
  13. package/api/RoutingAPI.test.js +29 -0
  14. package/api/StopsAPI.d.ts +1 -1
  15. package/api/StopsAPI.d.ts.map +1 -1
  16. package/api/StopsAPI.js +8 -40
  17. package/api/StopsAPI.test.d.ts +2 -0
  18. package/api/StopsAPI.test.d.ts.map +1 -0
  19. package/api/StopsAPI.test.js +26 -0
  20. package/api/index.d.ts +3 -4
  21. package/api/index.d.ts.map +1 -1
  22. package/api/index.js +3 -10
  23. package/api/typedefs.js +1 -0
  24. package/common/api/HttpAPI.d.ts +2 -2
  25. package/common/api/HttpAPI.d.ts.map +1 -1
  26. package/common/api/HttpAPI.js +21 -48
  27. package/common/api/HttpAPI.test.d.ts +2 -0
  28. package/common/api/HttpAPI.test.d.ts.map +1 -0
  29. package/common/api/HttpAPI.test.js +54 -0
  30. package/common/api/WebSocketAPI.d.ts +8 -4
  31. package/common/api/WebSocketAPI.d.ts.map +1 -1
  32. package/common/api/WebSocketAPI.js +125 -129
  33. package/common/api/WebSocketAPI.test.d.ts +2 -0
  34. package/common/api/WebSocketAPI.test.d.ts.map +1 -0
  35. package/common/api/WebSocketAPI.test.js +380 -0
  36. package/common/controls/Control.d.ts +5 -5
  37. package/common/controls/Control.d.ts.map +1 -1
  38. package/common/controls/Control.js +44 -77
  39. package/common/controls/Control.test.d.ts +2 -0
  40. package/common/controls/Control.test.d.ts.map +1 -0
  41. package/common/controls/Control.test.js +89 -0
  42. package/common/index.js +2 -18
  43. package/common/layers/Layer.d.ts +11 -11
  44. package/common/layers/Layer.d.ts.map +1 -1
  45. package/common/layers/Layer.js +70 -104
  46. package/common/layers/Layer.test.d.ts +2 -0
  47. package/common/layers/Layer.test.d.ts.map +1 -0
  48. package/common/layers/Layer.test.js +137 -0
  49. package/common/mixins/CopyrightMixin.js +23 -52
  50. package/common/mixins/MapboxLayerMixin.js +162 -204
  51. package/common/mixins/RealtimeLayerMixin.js +580 -635
  52. package/common/mixins/StopFinderMixin.d.ts +3 -3
  53. package/common/mixins/StopFinderMixin.d.ts.map +1 -1
  54. package/common/mixins/StopFinderMixin.js +115 -157
  55. package/common/mixins/UserInteractionsLayerMixin.js +142 -175
  56. package/common/mixins/UserInteractionsLayerMixin.test.d.ts +2 -0
  57. package/common/mixins/UserInteractionsLayerMixin.test.d.ts.map +1 -0
  58. package/common/mixins/UserInteractionsLayerMixin.test.js +214 -0
  59. package/common/styles/index.js +4 -24
  60. package/common/styles/realtimeDefaultStyle.d.ts.map +1 -1
  61. package/common/styles/realtimeDefaultStyle.js +67 -78
  62. package/common/styles/realtimeDelayStyle.js +4 -17
  63. package/common/styles/realtimeSimpleStyle.js +5 -7
  64. package/common/typedefs.js +1 -0
  65. package/common/utils/cleanStopTime.js +3 -5
  66. package/common/utils/compareDepartures.d.ts +1 -1
  67. package/common/utils/compareDepartures.d.ts.map +1 -1
  68. package/common/utils/compareDepartures.js +8 -11
  69. package/common/utils/createCanvas.js +3 -5
  70. package/common/utils/createTrackerFilters.d.ts +1 -1
  71. package/common/utils/createTrackerFilters.d.ts.map +1 -1
  72. package/common/utils/createTrackerFilters.js +22 -32
  73. package/common/utils/createTrackerFilters.test.d.ts +2 -0
  74. package/common/utils/createTrackerFilters.test.d.ts.map +1 -0
  75. package/common/utils/createTrackerFilters.test.js +79 -0
  76. package/common/utils/getLayersAsFlatArray.js +6 -8
  77. package/common/utils/getMapboxMapCopyrights.js +9 -11
  78. package/common/utils/getMapboxMapCopyrights.test.d.ts +2 -0
  79. package/common/utils/getMapboxMapCopyrights.test.d.ts.map +1 -0
  80. package/common/utils/getMapboxMapCopyrights.test.js +40 -0
  81. package/common/utils/getMapboxRender.js +12 -15
  82. package/common/utils/getMaplibreRender.js +10 -13
  83. package/common/utils/getRealtimeModeSuffix.js +2 -6
  84. package/common/utils/getUrlWithParams.js +5 -8
  85. package/common/utils/getVehiclePosition.js +20 -23
  86. package/common/utils/index.js +12 -37
  87. package/common/utils/removeDuplicate.d.ts +1 -1
  88. package/common/utils/removeDuplicate.d.ts.map +1 -1
  89. package/common/utils/removeDuplicate.js +6 -21
  90. package/common/utils/removeDuplicate.test.d.ts +2 -0
  91. package/common/utils/removeDuplicate.test.d.ts.map +1 -0
  92. package/common/utils/removeDuplicate.test.js +19 -0
  93. package/common/utils/renderTrajectories.js +31 -43
  94. package/common/utils/sortByDelay.js +4 -6
  95. package/common/utils/timeUtils.js +14 -24
  96. package/common/utils/timeUtils.test.d.ts +2 -0
  97. package/common/utils/timeUtils.test.d.ts.map +1 -0
  98. package/common/utils/timeUtils.test.js +10 -0
  99. package/common/utils/trackerConfig.js +27 -39
  100. package/common/utils/trackerConfig.test.d.ts +2 -0
  101. package/common/utils/trackerConfig.test.d.ts.map +1 -0
  102. package/common/utils/trackerConfig.test.js +23 -0
  103. package/iife.js +3 -5
  104. package/index.d.ts +4 -0
  105. package/index.js +9 -10
  106. package/mapbox/controls/CopyrightControl.d.ts +0 -1
  107. package/mapbox/controls/CopyrightControl.d.ts.map +1 -1
  108. package/mapbox/controls/CopyrightControl.js +18 -38
  109. package/mapbox/controls/index.js +1 -5
  110. package/mapbox/index.js +4 -20
  111. package/mapbox/layers/Layer.d.ts +1 -1
  112. package/mapbox/layers/Layer.d.ts.map +1 -1
  113. package/mapbox/layers/Layer.js +29 -71
  114. package/mapbox/layers/Layer.test.d.ts +2 -0
  115. package/mapbox/layers/Layer.test.d.ts.map +1 -0
  116. package/mapbox/layers/Layer.test.js +204 -0
  117. package/mapbox/layers/RealtimeLayer.d.ts +4 -4
  118. package/mapbox/layers/RealtimeLayer.d.ts.map +1 -1
  119. package/mapbox/layers/RealtimeLayer.js +83 -125
  120. package/mapbox/layers/RealtimeLayer.test.d.ts +2 -0
  121. package/mapbox/layers/RealtimeLayer.test.d.ts.map +1 -0
  122. package/mapbox/layers/RealtimeLayer.test.js +10 -0
  123. package/mapbox/layers/index.js +2 -7
  124. package/mapbox/utils.js +19 -33
  125. package/mbt.js +59 -37
  126. package/mbt.js.map +2 -2
  127. package/mbt.min.js +10 -10
  128. package/mbt.min.js.map +2 -2
  129. package/ol/controls/CopyrightControl.js +26 -47
  130. package/ol/controls/CopyrightControl.test.d.ts +2 -0
  131. package/ol/controls/CopyrightControl.test.d.ts.map +1 -0
  132. package/ol/controls/CopyrightControl.test.js +177 -0
  133. package/ol/controls/RoutingControl.d.ts +6 -5
  134. package/ol/controls/RoutingControl.d.ts.map +1 -1
  135. package/ol/controls/RoutingControl.js +209 -270
  136. package/ol/controls/RoutingControl.test.d.ts +2 -0
  137. package/ol/controls/RoutingControl.test.d.ts.map +1 -0
  138. package/ol/controls/RoutingControl.test.js +150 -0
  139. package/ol/controls/StopFinderControl.js +9 -32
  140. package/ol/controls/StopFinderControl.test.d.ts +2 -0
  141. package/ol/controls/StopFinderControl.test.d.ts.map +1 -0
  142. package/ol/controls/StopFinderControl.test.js +49 -0
  143. package/ol/controls/index.js +3 -9
  144. package/ol/index.js +5 -21
  145. package/ol/layers/Layer.d.ts +1 -1
  146. package/ol/layers/Layer.d.ts.map +1 -1
  147. package/ol/layers/Layer.js +40 -72
  148. package/ol/layers/Layer.test.d.ts +2 -0
  149. package/ol/layers/Layer.test.d.ts.map +1 -0
  150. package/ol/layers/Layer.test.js +196 -0
  151. package/ol/layers/MapboxLayer.d.ts +7 -7
  152. package/ol/layers/MapboxLayer.d.ts.map +1 -1
  153. package/ol/layers/MapboxLayer.js +30 -66
  154. package/ol/layers/MapboxLayer.test.d.ts +2 -0
  155. package/ol/layers/MapboxLayer.test.d.ts.map +1 -0
  156. package/ol/layers/MapboxLayer.test.js +164 -0
  157. package/ol/layers/MapboxStyleLayer.d.ts +3 -3
  158. package/ol/layers/MapboxStyleLayer.d.ts.map +1 -1
  159. package/ol/layers/MapboxStyleLayer.js +92 -135
  160. package/ol/layers/MapboxStyleLayer.test.d.ts +2 -0
  161. package/ol/layers/MapboxStyleLayer.test.d.ts.map +1 -0
  162. package/ol/layers/MapboxStyleLayer.test.js +232 -0
  163. package/ol/layers/MaplibreLayer.d.ts +1 -1
  164. package/ol/layers/MaplibreLayer.d.ts.map +1 -1
  165. package/ol/layers/MaplibreLayer.js +14 -48
  166. package/ol/layers/RealtimeLayer.d.ts +2 -2
  167. package/ol/layers/RealtimeLayer.d.ts.map +1 -1
  168. package/ol/layers/RealtimeLayer.js +111 -147
  169. package/ol/layers/RealtimeLayer.test.d.ts +2 -0
  170. package/ol/layers/RealtimeLayer.test.d.ts.map +1 -0
  171. package/ol/layers/RealtimeLayer.test.js +71 -0
  172. package/ol/layers/RoutingLayer.d.ts +2 -2
  173. package/ol/layers/RoutingLayer.d.ts.map +1 -1
  174. package/ol/layers/RoutingLayer.js +29 -61
  175. package/ol/layers/RoutingLayer.test.d.ts +2 -0
  176. package/ol/layers/RoutingLayer.test.d.ts.map +1 -0
  177. package/ol/layers/RoutingLayer.test.js +39 -0
  178. package/ol/layers/VectorLayer.d.ts +1 -1
  179. package/ol/layers/VectorLayer.d.ts.map +1 -1
  180. package/ol/layers/VectorLayer.js +14 -48
  181. package/ol/layers/VectorLayer.test.d.ts +2 -0
  182. package/ol/layers/VectorLayer.test.d.ts.map +1 -0
  183. package/ol/layers/VectorLayer.test.js +87 -0
  184. package/ol/layers/WMSLayer.d.ts +1 -1
  185. package/ol/layers/WMSLayer.d.ts.map +1 -1
  186. package/ol/layers/WMSLayer.js +34 -68
  187. package/ol/layers/WMSLayer.test.d.ts +2 -0
  188. package/ol/layers/WMSLayer.test.d.ts.map +1 -0
  189. package/ol/layers/WMSLayer.test.js +66 -0
  190. package/ol/layers/index.js +8 -19
  191. package/ol/styles/fullTrajectoryDelayStyle.js +11 -13
  192. package/ol/styles/fullTrajectoryStyle.js +16 -18
  193. package/ol/styles/index.js +2 -7
  194. package/package.json +5 -2
  195. package/setupTests.js +12 -14
@@ -1,48 +1,28 @@
1
- "use strict";
2
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
- if (ar || !(i in from)) {
5
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
- ar[i] = from[i];
7
- }
8
- }
9
- return to.concat(ar || Array.prototype.slice.call(from));
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
1
  /**
13
2
  * Class used to facilitate connection to a WebSocket and
14
3
  * also to manage properly messages send to the WebSocket.
15
4
  * This class must not contain any specific implementation.
16
5
  */
17
- var WebSocketAPI = /** @class */ (function () {
18
- function WebSocketAPI() {
6
+ class WebSocketAPI {
7
+ constructor() {
19
8
  this.defineProperties();
20
9
  }
21
- WebSocketAPI.prototype.defineProperties = function () {
22
- var _this = this;
10
+ defineProperties() {
23
11
  Object.defineProperties(this, {
24
12
  closed: {
25
- get: function () {
26
- return !!(_this.websocket &&
27
- _this.websocket.readyState === _this.websocket.CLOSED);
28
- },
13
+ get: () => !!(this.websocket &&
14
+ this.websocket.readyState === this.websocket.CLOSED),
29
15
  },
30
16
  closing: {
31
- get: function () {
32
- return !!(_this.websocket &&
33
- _this.websocket.readyState === _this.websocket.CLOSING);
34
- },
17
+ get: () => !!(this.websocket &&
18
+ this.websocket.readyState === this.websocket.CLOSING),
35
19
  },
36
20
  connecting: {
37
- get: function () {
38
- return !!(_this.websocket &&
39
- _this.websocket.readyState === _this.websocket.CONNECTING);
40
- },
21
+ get: () => !!(this.websocket &&
22
+ this.websocket.readyState === this.websocket.CONNECTING),
41
23
  },
42
24
  open: {
43
- get: function () {
44
- return !!(_this.websocket && _this.websocket.readyState === _this.websocket.OPEN);
45
- },
25
+ get: () => !!(this.websocket && this.websocket.readyState === this.websocket.OPEN),
46
26
  },
47
27
  /**
48
28
  * Array of message to send on open.
@@ -72,7 +52,7 @@ var WebSocketAPI = /** @class */ (function () {
72
52
  writable: true,
73
53
  },
74
54
  });
75
- };
55
+ }
76
56
  /**
77
57
  * Get the websocket request string.
78
58
  *
@@ -84,12 +64,12 @@ var WebSocketAPI = /** @class */ (function () {
84
64
  * @return {string} request string
85
65
  * @private
86
66
  */
87
- WebSocketAPI.getRequestString = function (method, params) {
88
- var reqStr = "".concat(method, " ").concat(params.channel);
89
- reqStr += params.args ? " ".concat(params.args) : '';
90
- reqStr += params.id ? " ".concat(params.id) : '';
67
+ static getRequestString(method, params) {
68
+ let reqStr = `${method} ${params.channel}`;
69
+ reqStr += params.args ? ` ${params.args}` : '';
70
+ reqStr += params.id ? ` ${params.id}` : '';
91
71
  return reqStr.trim();
92
- };
72
+ }
93
73
  /**
94
74
  * (Re)connect the websocket.
95
75
  *
@@ -97,69 +77,84 @@ var WebSocketAPI = /** @class */ (function () {
97
77
  * @param {function} onOpen Callback called when the websocket connection is opened and before subscriptions of previous subscriptions.
98
78
  * @private
99
79
  */
100
- WebSocketAPI.prototype.connect = function (url, onOpen) {
101
- var _this = this;
102
- if (onOpen === void 0) { onOpen = function () { }; }
80
+ connect(url, onOpen = () => { }) {
103
81
  if (this.websocket && !this.closed) {
104
82
  this.websocket.close();
105
83
  }
106
84
  /** @ignore */
107
85
  this.websocket = new WebSocket(url);
108
86
  if (!this.open) {
109
- this.websocket.addEventListener('open', function () {
87
+ this.websocket.addEventListener('open', () => {
110
88
  onOpen();
111
- _this.subscribePreviousSubscriptions();
89
+ this.subscribePreviousSubscriptions();
112
90
  });
113
91
  }
114
92
  else {
115
93
  onOpen();
116
94
  this.subscribePreviousSubscriptions();
117
95
  }
118
- };
96
+ }
119
97
  /**
120
98
  * Close the websocket definitively.
121
99
  *
122
100
  * @private
123
101
  */
124
- WebSocketAPI.prototype.close = function () {
102
+ close() {
125
103
  if (this.websocket) {
126
104
  this.websocket.onclose = null;
127
105
  this.websocket.close();
128
106
  this.websocket = null;
129
107
  this.messagesOnOpen = [];
130
108
  }
131
- };
109
+ }
132
110
  /**
133
111
  * Sends a message to the websocket.
134
112
  *
135
113
  * @param {message} message Message to send.
136
114
  * @private
137
115
  */
138
- WebSocketAPI.prototype.send = function (message) {
139
- var _this = this;
116
+ send(message) {
140
117
  if (!this.websocket) {
141
118
  return;
142
119
  }
143
- var send = function () {
144
- _this.websocket.send(message);
120
+ const send = () => {
121
+ this.websocket.send(message);
145
122
  };
146
123
  if (!this.open) {
147
124
  // This 'if' avoid sending 2 identical BBOX message on open,
148
125
  if (!this.messagesOnOpen.includes(message)) {
149
126
  this.messagesOnOpen.push(message);
150
- this.websocket.addEventListener('open', function () {
151
- _this.messagesOnOpen = [];
127
+ this.websocket.addEventListener('open', () => {
128
+ this.messagesOnOpen = [];
152
129
  send();
153
130
  });
154
- this.websocket.addEventListener('close', function () {
155
- _this.messagesOnOpen = [];
131
+ this.websocket.addEventListener('close', () => {
132
+ this.messagesOnOpen = [];
156
133
  });
157
134
  }
158
135
  }
159
136
  else if (!this.messagesOnOpen.includes(message)) {
160
137
  send();
161
138
  }
162
- };
139
+ }
140
+ addEvents(onMessage, onError) {
141
+ if (this.websocket) {
142
+ this.websocket.addEventListener('message', onMessage);
143
+ if (onError) {
144
+ this.websocket.addEventListener('error', onError);
145
+ this.websocket.addEventListener('close', onError);
146
+ }
147
+ }
148
+ }
149
+ removeEvents(onMessage, onError) {
150
+ if (this.websocket) {
151
+ this.websocket.removeEventListener('message', onMessage);
152
+ if (onError) {
153
+ this.websocket.removeEventListener('error', onError);
154
+ this.websocket.removeEventListener('close', onError);
155
+ }
156
+ }
157
+ }
163
158
  /**
164
159
  * Listen to websocket messages.
165
160
  *
@@ -169,11 +164,12 @@ var WebSocketAPI = /** @class */ (function () {
169
164
  * @return {{onMessage: function, errorCb: function}} Object with onMessage and error callbacks
170
165
  * @private
171
166
  */
172
- WebSocketAPI.prototype.listen = function (params, cb, errorCb) {
167
+ listen(params, cb, errorCb) {
173
168
  // Remove the previous identical callback
174
- this.unlisten(params, cb);
175
- var onMessage = function (evt) {
176
- var data = {};
169
+ this.unlisten(params, cb, errorCb);
170
+ // We wrap the message callback to be sure we only propagate the message if it is for the right channel.
171
+ const onMessage = (evt) => {
172
+ let data = {};
177
173
  try {
178
174
  data = JSON.parse(evt.data);
179
175
  }
@@ -181,11 +177,11 @@ var WebSocketAPI = /** @class */ (function () {
181
177
  // eslint-disable-next-line no-console
182
178
  console.error('WebSocket: unable to parse JSON data', err, evt.data);
183
179
  }
184
- var source = params.channel;
185
- source += params.args ? " ".concat(params.args) : '';
180
+ let source = params.channel;
181
+ source += params.args ? ` ${params.args}` : '';
186
182
  // Buffer channel message return a list of other channels to propagate to proper callbacks.
187
- var contents = data.source === 'buffer' ? data.content : [data];
188
- contents.forEach(function (content) {
183
+ const contents = data.source === 'buffer' ? data.content : [data];
184
+ contents.forEach((content) => {
189
185
  // Because of backend optimization, the last content is null.
190
186
  if ((content === null || content === void 0 ? void 0 : content.source) === source &&
191
187
  (!params.id || params.id === data.client_reference)) {
@@ -193,15 +189,9 @@ var WebSocketAPI = /** @class */ (function () {
193
189
  }
194
190
  });
195
191
  };
196
- if (this.websocket) {
197
- this.websocket.addEventListener('message', onMessage);
198
- if (errorCb) {
199
- this.websocket.addEventListener('error', errorCb);
200
- this.websocket.addEventListener('close', errorCb);
201
- }
202
- }
192
+ this.addEvents(onMessage, errorCb);
203
193
  return { onMessageCb: onMessage, onErrorCb: errorCb };
204
- };
194
+ }
205
195
  /**
206
196
  * Unlisten websocket messages.
207
197
  *
@@ -209,37 +199,54 @@ var WebSocketAPI = /** @class */ (function () {
209
199
  * @param {function} cb Callback used when listen.
210
200
  * @private
211
201
  */
212
- WebSocketAPI.prototype.unlisten = function (params, cb) {
213
- var _this = this;
214
- if (!this.websocket) {
215
- return;
216
- }
217
- this.subscriptions
218
- .filter(function (s) { return s.params.channel === params.channel && (!cb || s.cb === cb); })
219
- .forEach(function (_a) {
220
- var onMessageCb = _a.onMessageCb, onErrorCb = _a.onErrorCb;
221
- if (_this.websocket) {
222
- _this.websocket.removeEventListener('message', onMessageCb);
223
- if (onErrorCb) {
224
- _this.websocket.removeEventListener('error', onErrorCb);
225
- _this.websocket.removeEventListener('close', onErrorCb);
226
- }
227
- }
202
+ unlisten(params, cb) {
203
+ [...(this.subscriptions || []), ...(this.requests || [])]
204
+ .filter((s) => s.params.channel === params.channel && (!cb || s.cb === cb))
205
+ .forEach(({ onMessageCb, onErrorCb }) => {
206
+ this.removeEvents(onMessageCb, onErrorCb);
228
207
  });
229
- };
208
+ }
230
209
  /**
231
210
  * Sends a get request to the websocket.
211
+ * The callback is called only once, when the response is received or when the call returns an error.
232
212
  *
233
213
  * @param {Object} params Parameters for the websocket get request
234
- * @param {function} cb callback on listen
235
- * @param {function} errorCb Callback on error
214
+ * @param {function} onMessage callback on message event
215
+ * @param {function} onError Callback on error and close event
236
216
  * @private
237
217
  */
238
- WebSocketAPI.prototype.get = function (params, cb, errorCb) {
239
- var reqStr = WebSocketAPI.getRequestString('GET', params);
240
- this.send(reqStr);
241
- this.listen(params, cb, errorCb);
242
- };
218
+ get(params, cb, errorCb) {
219
+ const requestString = WebSocketAPI.getRequestString('GET', params);
220
+ this.send(requestString);
221
+ // We wrap the callbacks to make sure they are called only once.
222
+ const once = (callback) => (...args) => {
223
+ callback(...args);
224
+ const index = this.requests.findIndex((request) => requestString === request.requestString && cb === request.cb);
225
+ const { onMessageCb, onErrorCb } = this.requests[index];
226
+ this.removeEvents(onMessageCb, onErrorCb);
227
+ this.requests.splice(index, 1);
228
+ };
229
+ const { onMessageCb, onErrorCb } = this.listen(params, once(cb), once(errorCb));
230
+ // Store requests and callbacks to be able to remove them.
231
+ if (!this.requests) {
232
+ this.requests = [];
233
+ }
234
+ const index = this.requests.findIndex((request) => requestString === request.requestString && cb === request.cb);
235
+ const newReq = {
236
+ params,
237
+ requestString,
238
+ cb,
239
+ errorCb,
240
+ onMessageCb,
241
+ onErrorCb,
242
+ };
243
+ if (index > -1) {
244
+ this.requests[index] = newReq;
245
+ }
246
+ else {
247
+ this.requests.push(newReq);
248
+ }
249
+ }
243
250
  /**
244
251
  * Subscribe to a given channel.
245
252
  *
@@ -249,12 +256,11 @@ var WebSocketAPI = /** @class */ (function () {
249
256
  * @param {boolean} quiet if false, no GET or SUB requests are send, only the callback is registered.
250
257
  * @private
251
258
  */
252
- WebSocketAPI.prototype.subscribe = function (params, cb, errorCb, quiet) {
253
- if (quiet === void 0) { quiet = false; }
254
- var _a = this.listen(params, cb, errorCb), onMessageCb = _a.onMessageCb, onErrorCb = _a.onErrorCb;
255
- var reqStr = WebSocketAPI.getRequestString('', params);
256
- var index = this.subscriptions.findIndex(function (subcr) { return params.channel === subcr.params.channel && cb === subcr.cb; });
257
- var newSubscr = { params: params, cb: cb, errorCb: errorCb, onMessageCb: onMessageCb, onErrorCb: onErrorCb, quiet: quiet };
259
+ subscribe(params, cb, errorCb, quiet = false) {
260
+ const { onMessageCb, onErrorCb } = this.listen(params, cb, errorCb);
261
+ const reqStr = WebSocketAPI.getRequestString('', params);
262
+ const index = this.subscriptions.findIndex((subcr) => params.channel === subcr.params.channel && cb === subcr.cb);
263
+ const newSubscr = { params, cb, errorCb, onMessageCb, onErrorCb, quiet };
258
264
  if (index > -1) {
259
265
  this.subscriptions[index] = newSubscr;
260
266
  }
@@ -263,58 +269,48 @@ var WebSocketAPI = /** @class */ (function () {
263
269
  }
264
270
  if (!this.subscribed[reqStr]) {
265
271
  if (!newSubscr.quiet) {
266
- this.send("GET ".concat(reqStr));
267
- this.send("SUB ".concat(reqStr));
272
+ this.send(`GET ${reqStr}`);
273
+ this.send(`SUB ${reqStr}`);
268
274
  }
269
275
  this.subscribed[reqStr] = true;
270
276
  }
271
- };
277
+ }
272
278
  /**
273
279
  * Unsubscribe from a channel.
274
280
  * @param {string} source source to unsubscribe from
275
281
  * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
276
282
  * @private
277
283
  */
278
- WebSocketAPI.prototype.unsubscribe = function (source, cb) {
279
- var _this = this;
280
- var toRemove = this.subscriptions.filter(function (s) { return s.params.channel === source && (!cb || s.cb === cb); });
281
- toRemove.forEach(function (_a) {
282
- var onMessageCb = _a.onMessageCb, onErrorCb = _a.onErrorCb;
283
- if (_this.websocket) {
284
- _this.websocket.removeEventListener('message', onMessageCb);
285
- if (onErrorCb) {
286
- _this.websocket.removeEventListener('error', onErrorCb);
287
- _this.websocket.removeEventListener('close', onErrorCb);
288
- }
289
- }
284
+ unsubscribe(source, cb) {
285
+ const toRemove = this.subscriptions.filter((s) => s.params.channel === source && (!cb || s.cb === cb));
286
+ toRemove.forEach(({ onMessageCb, onErrorCb }) => {
287
+ this.removeEvents(onMessageCb, onErrorCb);
290
288
  });
291
- this.subscriptions = this.subscriptions.filter(function (s) { return s.params.channel !== source || (cb && s.cb !== cb); });
289
+ this.subscriptions = this.subscriptions.filter((s) => s.params.channel !== source || (cb && s.cb !== cb));
292
290
  // If there is no more subscriptions to this channel, and the removed subscriptions didn't register quietly,
293
291
  // we DEL it.
294
292
  if (source &&
295
293
  this.subscribed[source] &&
296
- !this.subscriptions.find(function (s) { return s.params.channel === source; }) &&
297
- toRemove.find(function (subscr) { return !subscr.quiet; })) {
298
- this.send("DEL ".concat(source));
294
+ !this.subscriptions.find((s) => s.params.channel === source) &&
295
+ toRemove.find((subscr) => !subscr.quiet)) {
296
+ this.send(`DEL ${source}`);
299
297
  this.subscribed[source] = false;
300
298
  }
301
- };
299
+ }
302
300
  /**
303
301
  * After an auto reconnection we need to re-subscribe to the channels.
304
302
  */
305
- WebSocketAPI.prototype.subscribePreviousSubscriptions = function () {
306
- var _this = this;
303
+ subscribePreviousSubscriptions() {
307
304
  // Before to subscribe previous subscriptions we make sure they
308
305
  // are all defined as unsubscribed, because this code is asynchrone
309
306
  // and a subscription could have been added in between.
310
- Object.keys(this.subscribed).forEach(function (key) {
311
- _this.subscribed[key] = false;
307
+ Object.keys(this.subscribed).forEach((key) => {
308
+ this.subscribed[key] = false;
312
309
  });
313
310
  // Subscribe all previous subscriptions.
314
- __spreadArray([], this.subscriptions, true).forEach(function (s) {
315
- _this.subscribe(s.params, s.cb, s.errorCb, s.quiet);
311
+ [...this.subscriptions].forEach((s) => {
312
+ this.subscribe(s.params, s.cb, s.errorCb, s.quiet);
316
313
  });
317
- };
318
- return WebSocketAPI;
319
- }());
320
- exports.default = WebSocketAPI;
314
+ }
315
+ }
316
+ export default WebSocketAPI;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=WebSocketAPI.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WebSocketAPI.test.d.ts","sourceRoot":"","sources":["../../../src/common/api/WebSocketAPI.test.js"],"names":[],"mappings":""}