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.
- package/README.md +14 -8
- package/api/RealtimeAPI.d.ts +6 -6
- package/api/RealtimeAPI.d.ts.map +1 -1
- package/api/RealtimeAPI.js +170 -197
- package/api/RealtimeAPI.test.d.ts +2 -0
- package/api/RealtimeAPI.test.d.ts.map +1 -0
- package/api/RealtimeAPI.test.js +67 -0
- package/api/RoutingAPI.d.ts +6 -2
- package/api/RoutingAPI.d.ts.map +1 -1
- package/api/RoutingAPI.js +8 -38
- package/api/RoutingAPI.test.d.ts +2 -0
- package/api/RoutingAPI.test.d.ts.map +1 -0
- package/api/RoutingAPI.test.js +29 -0
- package/api/StopsAPI.d.ts +1 -1
- package/api/StopsAPI.d.ts.map +1 -1
- package/api/StopsAPI.js +8 -40
- package/api/StopsAPI.test.d.ts +2 -0
- package/api/StopsAPI.test.d.ts.map +1 -0
- package/api/StopsAPI.test.js +26 -0
- package/api/index.d.ts +3 -4
- package/api/index.d.ts.map +1 -1
- package/api/index.js +3 -10
- package/api/typedefs.js +1 -0
- package/common/api/HttpAPI.d.ts +2 -2
- package/common/api/HttpAPI.d.ts.map +1 -1
- package/common/api/HttpAPI.js +21 -48
- package/common/api/HttpAPI.test.d.ts +2 -0
- package/common/api/HttpAPI.test.d.ts.map +1 -0
- package/common/api/HttpAPI.test.js +54 -0
- package/common/api/WebSocketAPI.d.ts +8 -4
- package/common/api/WebSocketAPI.d.ts.map +1 -1
- package/common/api/WebSocketAPI.js +125 -129
- package/common/api/WebSocketAPI.test.d.ts +2 -0
- package/common/api/WebSocketAPI.test.d.ts.map +1 -0
- package/common/api/WebSocketAPI.test.js +380 -0
- package/common/controls/Control.d.ts +5 -5
- package/common/controls/Control.d.ts.map +1 -1
- package/common/controls/Control.js +44 -77
- package/common/controls/Control.test.d.ts +2 -0
- package/common/controls/Control.test.d.ts.map +1 -0
- package/common/controls/Control.test.js +89 -0
- package/common/index.js +2 -18
- package/common/layers/Layer.d.ts +11 -11
- package/common/layers/Layer.d.ts.map +1 -1
- package/common/layers/Layer.js +70 -104
- package/common/layers/Layer.test.d.ts +2 -0
- package/common/layers/Layer.test.d.ts.map +1 -0
- package/common/layers/Layer.test.js +137 -0
- package/common/mixins/CopyrightMixin.js +23 -52
- package/common/mixins/MapboxLayerMixin.js +162 -204
- package/common/mixins/RealtimeLayerMixin.js +580 -635
- package/common/mixins/StopFinderMixin.d.ts +3 -3
- package/common/mixins/StopFinderMixin.d.ts.map +1 -1
- package/common/mixins/StopFinderMixin.js +115 -157
- package/common/mixins/UserInteractionsLayerMixin.js +142 -175
- package/common/mixins/UserInteractionsLayerMixin.test.d.ts +2 -0
- package/common/mixins/UserInteractionsLayerMixin.test.d.ts.map +1 -0
- package/common/mixins/UserInteractionsLayerMixin.test.js +214 -0
- package/common/styles/index.js +4 -24
- package/common/styles/realtimeDefaultStyle.d.ts.map +1 -1
- package/common/styles/realtimeDefaultStyle.js +67 -78
- package/common/styles/realtimeDelayStyle.js +4 -17
- package/common/styles/realtimeSimpleStyle.js +5 -7
- package/common/typedefs.js +1 -0
- package/common/utils/cleanStopTime.js +3 -5
- package/common/utils/compareDepartures.d.ts +1 -1
- package/common/utils/compareDepartures.d.ts.map +1 -1
- package/common/utils/compareDepartures.js +8 -11
- package/common/utils/createCanvas.js +3 -5
- package/common/utils/createTrackerFilters.d.ts +1 -1
- package/common/utils/createTrackerFilters.d.ts.map +1 -1
- package/common/utils/createTrackerFilters.js +22 -32
- package/common/utils/createTrackerFilters.test.d.ts +2 -0
- package/common/utils/createTrackerFilters.test.d.ts.map +1 -0
- package/common/utils/createTrackerFilters.test.js +79 -0
- package/common/utils/getLayersAsFlatArray.js +6 -8
- package/common/utils/getMapboxMapCopyrights.js +9 -11
- package/common/utils/getMapboxMapCopyrights.test.d.ts +2 -0
- package/common/utils/getMapboxMapCopyrights.test.d.ts.map +1 -0
- package/common/utils/getMapboxMapCopyrights.test.js +40 -0
- package/common/utils/getMapboxRender.js +12 -15
- package/common/utils/getMaplibreRender.js +10 -13
- package/common/utils/getRealtimeModeSuffix.js +2 -6
- package/common/utils/getUrlWithParams.js +5 -8
- package/common/utils/getVehiclePosition.js +20 -23
- package/common/utils/index.js +12 -37
- package/common/utils/removeDuplicate.d.ts +1 -1
- package/common/utils/removeDuplicate.d.ts.map +1 -1
- package/common/utils/removeDuplicate.js +6 -21
- package/common/utils/removeDuplicate.test.d.ts +2 -0
- package/common/utils/removeDuplicate.test.d.ts.map +1 -0
- package/common/utils/removeDuplicate.test.js +19 -0
- package/common/utils/renderTrajectories.js +31 -43
- package/common/utils/sortByDelay.js +4 -6
- package/common/utils/timeUtils.js +14 -24
- package/common/utils/timeUtils.test.d.ts +2 -0
- package/common/utils/timeUtils.test.d.ts.map +1 -0
- package/common/utils/timeUtils.test.js +10 -0
- package/common/utils/trackerConfig.js +27 -39
- package/common/utils/trackerConfig.test.d.ts +2 -0
- package/common/utils/trackerConfig.test.d.ts.map +1 -0
- package/common/utils/trackerConfig.test.js +23 -0
- package/iife.js +3 -5
- package/index.d.ts +4 -0
- package/index.js +9 -10
- package/mapbox/controls/CopyrightControl.d.ts +0 -1
- package/mapbox/controls/CopyrightControl.d.ts.map +1 -1
- package/mapbox/controls/CopyrightControl.js +18 -38
- package/mapbox/controls/index.js +1 -5
- package/mapbox/index.js +4 -20
- package/mapbox/layers/Layer.d.ts +1 -1
- package/mapbox/layers/Layer.d.ts.map +1 -1
- package/mapbox/layers/Layer.js +29 -71
- package/mapbox/layers/Layer.test.d.ts +2 -0
- package/mapbox/layers/Layer.test.d.ts.map +1 -0
- package/mapbox/layers/Layer.test.js +204 -0
- package/mapbox/layers/RealtimeLayer.d.ts +4 -4
- package/mapbox/layers/RealtimeLayer.d.ts.map +1 -1
- package/mapbox/layers/RealtimeLayer.js +83 -125
- package/mapbox/layers/RealtimeLayer.test.d.ts +2 -0
- package/mapbox/layers/RealtimeLayer.test.d.ts.map +1 -0
- package/mapbox/layers/RealtimeLayer.test.js +10 -0
- package/mapbox/layers/index.js +2 -7
- package/mapbox/utils.js +19 -33
- package/mbt.js +59 -37
- package/mbt.js.map +2 -2
- package/mbt.min.js +10 -10
- package/mbt.min.js.map +2 -2
- package/ol/controls/CopyrightControl.js +26 -47
- package/ol/controls/CopyrightControl.test.d.ts +2 -0
- package/ol/controls/CopyrightControl.test.d.ts.map +1 -0
- package/ol/controls/CopyrightControl.test.js +177 -0
- package/ol/controls/RoutingControl.d.ts +6 -5
- package/ol/controls/RoutingControl.d.ts.map +1 -1
- package/ol/controls/RoutingControl.js +209 -270
- package/ol/controls/RoutingControl.test.d.ts +2 -0
- package/ol/controls/RoutingControl.test.d.ts.map +1 -0
- package/ol/controls/RoutingControl.test.js +150 -0
- package/ol/controls/StopFinderControl.js +9 -32
- package/ol/controls/StopFinderControl.test.d.ts +2 -0
- package/ol/controls/StopFinderControl.test.d.ts.map +1 -0
- package/ol/controls/StopFinderControl.test.js +49 -0
- package/ol/controls/index.js +3 -9
- package/ol/index.js +5 -21
- package/ol/layers/Layer.d.ts +1 -1
- package/ol/layers/Layer.d.ts.map +1 -1
- package/ol/layers/Layer.js +40 -72
- package/ol/layers/Layer.test.d.ts +2 -0
- package/ol/layers/Layer.test.d.ts.map +1 -0
- package/ol/layers/Layer.test.js +196 -0
- package/ol/layers/MapboxLayer.d.ts +7 -7
- package/ol/layers/MapboxLayer.d.ts.map +1 -1
- package/ol/layers/MapboxLayer.js +30 -66
- package/ol/layers/MapboxLayer.test.d.ts +2 -0
- package/ol/layers/MapboxLayer.test.d.ts.map +1 -0
- package/ol/layers/MapboxLayer.test.js +164 -0
- package/ol/layers/MapboxStyleLayer.d.ts +3 -3
- package/ol/layers/MapboxStyleLayer.d.ts.map +1 -1
- package/ol/layers/MapboxStyleLayer.js +92 -135
- package/ol/layers/MapboxStyleLayer.test.d.ts +2 -0
- package/ol/layers/MapboxStyleLayer.test.d.ts.map +1 -0
- package/ol/layers/MapboxStyleLayer.test.js +232 -0
- package/ol/layers/MaplibreLayer.d.ts +1 -1
- package/ol/layers/MaplibreLayer.d.ts.map +1 -1
- package/ol/layers/MaplibreLayer.js +14 -48
- package/ol/layers/RealtimeLayer.d.ts +2 -2
- package/ol/layers/RealtimeLayer.d.ts.map +1 -1
- package/ol/layers/RealtimeLayer.js +111 -147
- package/ol/layers/RealtimeLayer.test.d.ts +2 -0
- package/ol/layers/RealtimeLayer.test.d.ts.map +1 -0
- package/ol/layers/RealtimeLayer.test.js +71 -0
- package/ol/layers/RoutingLayer.d.ts +2 -2
- package/ol/layers/RoutingLayer.d.ts.map +1 -1
- package/ol/layers/RoutingLayer.js +29 -61
- package/ol/layers/RoutingLayer.test.d.ts +2 -0
- package/ol/layers/RoutingLayer.test.d.ts.map +1 -0
- package/ol/layers/RoutingLayer.test.js +39 -0
- package/ol/layers/VectorLayer.d.ts +1 -1
- package/ol/layers/VectorLayer.d.ts.map +1 -1
- package/ol/layers/VectorLayer.js +14 -48
- package/ol/layers/VectorLayer.test.d.ts +2 -0
- package/ol/layers/VectorLayer.test.d.ts.map +1 -0
- package/ol/layers/VectorLayer.test.js +87 -0
- package/ol/layers/WMSLayer.d.ts +1 -1
- package/ol/layers/WMSLayer.d.ts.map +1 -1
- package/ol/layers/WMSLayer.js +34 -68
- package/ol/layers/WMSLayer.test.d.ts +2 -0
- package/ol/layers/WMSLayer.test.d.ts.map +1 -0
- package/ol/layers/WMSLayer.test.js +66 -0
- package/ol/layers/index.js +8 -19
- package/ol/styles/fullTrajectoryDelayStyle.js +11 -13
- package/ol/styles/fullTrajectoryStyle.js +16 -18
- package/ol/styles/index.js +2 -7
- package/package.json +5 -2
- 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
|
-
|
|
18
|
-
|
|
6
|
+
class WebSocketAPI {
|
|
7
|
+
constructor() {
|
|
19
8
|
this.defineProperties();
|
|
20
9
|
}
|
|
21
|
-
|
|
22
|
-
var _this = this;
|
|
10
|
+
defineProperties() {
|
|
23
11
|
Object.defineProperties(this, {
|
|
24
12
|
closed: {
|
|
25
|
-
get:
|
|
26
|
-
|
|
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:
|
|
32
|
-
|
|
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:
|
|
38
|
-
|
|
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:
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
reqStr += params.args ?
|
|
90
|
-
reqStr += 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
|
-
|
|
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',
|
|
87
|
+
this.websocket.addEventListener('open', () => {
|
|
110
88
|
onOpen();
|
|
111
|
-
|
|
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
|
-
|
|
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
|
-
|
|
139
|
-
var _this = this;
|
|
116
|
+
send(message) {
|
|
140
117
|
if (!this.websocket) {
|
|
141
118
|
return;
|
|
142
119
|
}
|
|
143
|
-
|
|
144
|
-
|
|
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',
|
|
151
|
-
|
|
127
|
+
this.websocket.addEventListener('open', () => {
|
|
128
|
+
this.messagesOnOpen = [];
|
|
152
129
|
send();
|
|
153
130
|
});
|
|
154
|
-
this.websocket.addEventListener('close',
|
|
155
|
-
|
|
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
|
-
|
|
167
|
+
listen(params, cb, errorCb) {
|
|
173
168
|
// Remove the previous identical callback
|
|
174
|
-
this.unlisten(params, cb);
|
|
175
|
-
|
|
176
|
-
|
|
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
|
-
|
|
185
|
-
source += 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
|
-
|
|
188
|
-
contents.forEach(
|
|
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
|
-
|
|
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
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
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}
|
|
235
|
-
* @param {function}
|
|
214
|
+
* @param {function} onMessage callback on message event
|
|
215
|
+
* @param {function} onError Callback on error and close event
|
|
236
216
|
* @private
|
|
237
217
|
*/
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
this.send(
|
|
241
|
-
|
|
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
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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(
|
|
267
|
-
this.send(
|
|
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
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
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(
|
|
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(
|
|
297
|
-
toRemove.find(
|
|
298
|
-
this.send(
|
|
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
|
-
|
|
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(
|
|
311
|
-
|
|
307
|
+
Object.keys(this.subscribed).forEach((key) => {
|
|
308
|
+
this.subscribed[key] = false;
|
|
312
309
|
});
|
|
313
310
|
// Subscribe all previous subscriptions.
|
|
314
|
-
|
|
315
|
-
|
|
311
|
+
[...this.subscriptions].forEach((s) => {
|
|
312
|
+
this.subscribe(s.params, s.cb, s.errorCb, s.quiet);
|
|
316
313
|
});
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
exports.default = WebSocketAPI;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
export default WebSocketAPI;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebSocketAPI.test.d.ts","sourceRoot":"","sources":["../../../src/common/api/WebSocketAPI.test.js"],"names":[],"mappings":""}
|