mobility-toolbox-js 3.1.1 → 3.2.0-beta.0
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/api/HttpAPI.d.ts +1 -1
- package/api/HttpAPI.js +0 -1
- package/api/RealtimeAPI.d.ts +6 -6
- package/api/RealtimeAPI.js +3 -3
- package/api/WebSocketAPI.d.ts +30 -30
- package/api/WebSocketAPI.js +52 -23
- package/common/utils/debounceWebsocketMessages.js +1 -1
- package/mbt.js +24506 -21939
- package/mbt.js.map +4 -4
- package/mbt.min.js +291 -143
- package/mbt.min.js.map +4 -4
- package/ol/layers/MaplibreLayer.d.ts +1 -1
- package/ol/layers/MaplibreLayer.js +4 -3
- package/ol/layers/index.d.ts +1 -1
- package/ol/layers/index.js +2 -2
- package/ol/renderers/MaplibreLayerRenderer.d.ts +2 -1
- package/ol/renderers/MaplibreLayerRenderer.js +3 -2
- package/package.json +25 -33
- package/types/index.d.ts +4 -3
- package/types/realtime.d.ts +224 -224
package/api/HttpAPI.d.ts
CHANGED
package/api/HttpAPI.js
CHANGED
|
@@ -28,7 +28,6 @@ class HttpAPI {
|
|
|
28
28
|
throw new Error(`No url defined for request to ${this.url}/${path}`);
|
|
29
29
|
}
|
|
30
30
|
if (!this.url && !this.apiKey && !this.url.includes('key=')) {
|
|
31
|
-
// eslint-disable-next-line no-console
|
|
32
31
|
throw new Error(`No apiKey defined for request to ${this.url}`);
|
|
33
32
|
}
|
|
34
33
|
// Clean requets parameters, removing undefined and null values.
|
package/api/RealtimeAPI.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import WebSocketAPI, { WebSocketAPIMessageCallback, WebSocketAPIMessageEventData, WebSocketAPIParameters } from './WebSocketAPI';
|
|
2
1
|
import type { RealtimeBbox, RealtimeDeparture, RealtimeExtraGeom, RealtimeFullTrajectory, RealtimeGeneralizationLevel, RealtimeMode, RealtimeNews, RealtimeStation, RealtimeStationId, RealtimeStopSequence, RealtimeTenant, RealtimeTrainId, RealtimeTrajectory, RealtimeVersion } from '../types';
|
|
2
|
+
import WebSocketAPI, { WebSocketAPIMessageCallback, WebSocketAPIMessageEventData, WebSocketAPIParameters } from './WebSocketAPI';
|
|
3
|
+
export type RealtimeAPIDeparturesById = Record<string, RealtimeDeparture>;
|
|
4
|
+
export type RealtimeAPIExtraGeomsById = Record<string, RealtimeExtraGeom>;
|
|
3
5
|
/**
|
|
4
6
|
* @typedef RealtimeAPIOptions
|
|
5
7
|
*/
|
|
@@ -12,8 +14,6 @@ export interface RealtimeAPIOptions {
|
|
|
12
14
|
url?: string;
|
|
13
15
|
version?: RealtimeVersion;
|
|
14
16
|
}
|
|
15
|
-
export type RealtimeAPIExtraGeomsById = Record<string, RealtimeExtraGeom>;
|
|
16
|
-
export type RealtimeAPIDeparturesById = Record<string, RealtimeDeparture>;
|
|
17
17
|
export interface RealtimeModesType {
|
|
18
18
|
RAW: RealtimeMode;
|
|
19
19
|
SCHEMATIC: RealtimeMode;
|
|
@@ -135,7 +135,7 @@ declare class RealtimeAPI {
|
|
|
135
135
|
* @return {Promise<WebSocketAPIMessageEventData<?>>} A websocket response.
|
|
136
136
|
* @public
|
|
137
137
|
*/
|
|
138
|
-
get(channelOrParams: string | WebSocketAPIParameters): Promise<WebSocketAPIMessageEventData<
|
|
138
|
+
get<T>(channelOrParams: string | WebSocketAPIParameters): Promise<WebSocketAPIMessageEventData<T>>;
|
|
139
139
|
/**
|
|
140
140
|
* Get a full trajectory of a vehicule .
|
|
141
141
|
*
|
|
@@ -211,7 +211,7 @@ declare class RealtimeAPI {
|
|
|
211
211
|
* @param {boolean} [quiet=false] If true avoid to store the subscription in the subscriptions list.
|
|
212
212
|
* @public
|
|
213
213
|
*/
|
|
214
|
-
subscribe(channel: string, onSuccess: WebSocketAPIMessageCallback<
|
|
214
|
+
subscribe<T>(channel: string, onSuccess: WebSocketAPIMessageCallback<T>, onError?: EventListener, quiet?: boolean): void;
|
|
215
215
|
/**
|
|
216
216
|
* Subscribe to deleted_vhicles channel.
|
|
217
217
|
*
|
|
@@ -327,7 +327,7 @@ declare class RealtimeAPI {
|
|
|
327
327
|
* @param {function} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
|
|
328
328
|
* @public
|
|
329
329
|
*/
|
|
330
|
-
unsubscribe(channel: string, suffix?: string, onMessage?: WebSocketAPIMessageCallback<
|
|
330
|
+
unsubscribe<T>(channel: string, suffix?: string, onMessage?: WebSocketAPIMessageCallback<T>): void;
|
|
331
331
|
/**
|
|
332
332
|
* Unsubscribe to deleted_vhicles channels.
|
|
333
333
|
* @param {function(data: { content: RealtimeTrainId })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
|
package/api/RealtimeAPI.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
/* eslint-disable no-underscore-dangle */
|
|
2
1
|
import debounceWebsocketMessages from '../common/utils/debounceWebsocketMessages';
|
|
3
2
|
import getModeSuffix from '../common/utils/getRealtimeModeSuffix';
|
|
4
|
-
/* eslint-disable class-methods-use-this */
|
|
5
3
|
import WebSocketAPI from './WebSocketAPI';
|
|
6
4
|
/**
|
|
7
5
|
* Enum for Realtime modes.
|
|
@@ -261,7 +259,9 @@ class RealtimeAPI {
|
|
|
261
259
|
window.clearTimeout(this.pingInterval);
|
|
262
260
|
window.clearTimeout(this.reconnectTimeout);
|
|
263
261
|
if (this.reconnectTimeoutMs) {
|
|
264
|
-
this.reconnectTimeout = window.setTimeout(() =>
|
|
262
|
+
this.reconnectTimeout = window.setTimeout(() => {
|
|
263
|
+
return this.open();
|
|
264
|
+
}, this.reconnectTimeoutMs);
|
|
265
265
|
}
|
|
266
266
|
}
|
|
267
267
|
/**
|
package/api/WebSocketAPI.d.ts
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
1
|
import { RealtimeTrajectoryResponse } from '../types';
|
|
2
|
-
export declare interface WebSocketAPIParameters {
|
|
3
|
-
args?: number | string;
|
|
4
|
-
channel?: string;
|
|
5
|
-
id?: number | string;
|
|
6
|
-
}
|
|
7
2
|
export declare interface WebSocketAPIMessageEventData<T> {
|
|
8
3
|
client_reference: null | number | string;
|
|
9
4
|
content: T;
|
|
10
5
|
source: string;
|
|
11
6
|
timestamp: number;
|
|
12
7
|
}
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* This type represents a function that has been call with each feature returned by the websocket.
|
|
22
|
-
*/
|
|
23
|
-
export type WebSocketAPIMessageCallback<T> = (data: WebSocketAPIMessageEventData<T>) => void;
|
|
24
|
-
export declare interface WebSocketAPISubscription {
|
|
25
|
-
cb: WebSocketAPIMessageCallback<any>;
|
|
8
|
+
export declare interface WebSocketAPIParameters {
|
|
9
|
+
args?: number | string;
|
|
10
|
+
channel?: string;
|
|
11
|
+
id?: number | string;
|
|
12
|
+
}
|
|
13
|
+
export declare interface WebSocketAPIRequest<T> {
|
|
14
|
+
cb: WebSocketAPIMessageCallback<T>;
|
|
26
15
|
errorCb?: EventListener;
|
|
27
16
|
onErrorCb?: EventListener;
|
|
28
17
|
onMessageCb: WebSocketAPIMessageEventListener;
|
|
29
18
|
params: WebSocketAPIParameters;
|
|
30
|
-
|
|
19
|
+
requestString: string;
|
|
31
20
|
}
|
|
32
|
-
export
|
|
33
|
-
|
|
34
|
-
cb: WebSocketAPIMessageCallback<any>;
|
|
21
|
+
export declare interface WebSocketAPISubscription<T> {
|
|
22
|
+
cb: WebSocketAPIMessageCallback<T>;
|
|
35
23
|
errorCb?: EventListener;
|
|
36
24
|
onErrorCb?: EventListener;
|
|
37
25
|
onMessageCb: WebSocketAPIMessageEventListener;
|
|
38
26
|
params: WebSocketAPIParameters;
|
|
39
|
-
|
|
27
|
+
quiet: boolean;
|
|
40
28
|
}
|
|
29
|
+
export type WebSocketAPIBufferMessageEventData = {
|
|
30
|
+
source: 'buffer';
|
|
31
|
+
} & WebSocketAPIMessageEventData<RealtimeTrajectoryResponse[]>;
|
|
32
|
+
/**
|
|
33
|
+
* This type represents a function that has been call with each feature returned by the websocket.
|
|
34
|
+
*/
|
|
35
|
+
export type WebSocketAPIMessageCallback<T> = (data: WebSocketAPIMessageEventData<T>) => void;
|
|
36
|
+
export type WebSocketAPIMessageEvent = {
|
|
37
|
+
data: string;
|
|
38
|
+
} & Event;
|
|
39
|
+
export type WebSocketAPIMessageEventListener = (evt: WebSocketAPIMessageEvent) => void;
|
|
40
|
+
export type WebSocketAPISubscribed = Record<string, boolean>;
|
|
41
41
|
/**
|
|
42
42
|
* Class used to facilitate connection to a WebSocketAPI and
|
|
43
43
|
* also to manage properly messages send to the WebSocketAPI.
|
|
@@ -50,9 +50,9 @@ declare class WebSocketAPI {
|
|
|
50
50
|
connecting?: boolean;
|
|
51
51
|
messagesOnOpen: string[];
|
|
52
52
|
open?: boolean;
|
|
53
|
-
requests: WebSocketAPIRequest[];
|
|
53
|
+
requests: WebSocketAPIRequest<unknown>[];
|
|
54
54
|
subscribed: WebSocketAPISubscribed;
|
|
55
|
-
subscriptions: WebSocketAPISubscription[];
|
|
55
|
+
subscriptions: WebSocketAPISubscription<unknown>[];
|
|
56
56
|
websocket?: WebSocket;
|
|
57
57
|
constructor();
|
|
58
58
|
/**
|
|
@@ -92,7 +92,7 @@ declare class WebSocketAPI {
|
|
|
92
92
|
* @param {function} errorCb Callback on error and close event
|
|
93
93
|
* @private
|
|
94
94
|
*/
|
|
95
|
-
get(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<
|
|
95
|
+
get<T>(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<T>, errorCb?: EventListener): void;
|
|
96
96
|
/**
|
|
97
97
|
* Listen to websocket messages.
|
|
98
98
|
*
|
|
@@ -102,7 +102,7 @@ declare class WebSocketAPI {
|
|
|
102
102
|
* @return {{onMessage: function, errorCb: function}} Object with onMessage and error callbacks
|
|
103
103
|
* @private
|
|
104
104
|
*/
|
|
105
|
-
listen(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<
|
|
105
|
+
listen<T>(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<T>, errorCb?: EventListener): {
|
|
106
106
|
onErrorCb?: EventListener;
|
|
107
107
|
onMessageCb: WebSocketAPIMessageEventListener;
|
|
108
108
|
};
|
|
@@ -123,7 +123,7 @@ declare class WebSocketAPI {
|
|
|
123
123
|
* @param {boolean} quiet if false, no GET or SUB requests are send, only the callback is registered.
|
|
124
124
|
* @private
|
|
125
125
|
*/
|
|
126
|
-
subscribe(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<
|
|
126
|
+
subscribe<T>(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<T>, errorCb?: EventListener, quiet?: boolean): void;
|
|
127
127
|
/**
|
|
128
128
|
* After an auto reconnection we need to re-subscribe to the channels.
|
|
129
129
|
*/
|
|
@@ -135,13 +135,13 @@ declare class WebSocketAPI {
|
|
|
135
135
|
* @param {function} cb Callback used when listen.
|
|
136
136
|
* @private
|
|
137
137
|
*/
|
|
138
|
-
unlisten(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<
|
|
138
|
+
unlisten<T>(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<T>): void;
|
|
139
139
|
/**
|
|
140
140
|
* Unsubscribe from a channel.
|
|
141
141
|
* @param {string} source source to unsubscribe from
|
|
142
142
|
* @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
|
|
143
143
|
* @private
|
|
144
144
|
*/
|
|
145
|
-
unsubscribe(source: string, cb?: WebSocketAPIMessageCallback<
|
|
145
|
+
unsubscribe<T>(source: string, cb?: WebSocketAPIMessageCallback<T>): void;
|
|
146
146
|
}
|
|
147
147
|
export default WebSocketAPI;
|
package/api/WebSocketAPI.js
CHANGED
|
@@ -86,16 +86,22 @@ class WebSocketAPI {
|
|
|
86
86
|
defineProperties() {
|
|
87
87
|
Object.defineProperties(this, {
|
|
88
88
|
closed: {
|
|
89
|
-
get: () =>
|
|
90
|
-
this.websocket
|
|
89
|
+
get: () => {
|
|
90
|
+
return !!(!this.websocket ||
|
|
91
|
+
this.websocket.readyState === this.websocket.CLOSED);
|
|
92
|
+
},
|
|
91
93
|
},
|
|
92
94
|
closing: {
|
|
93
|
-
get: () =>
|
|
94
|
-
this.websocket
|
|
95
|
+
get: () => {
|
|
96
|
+
return !!(this.websocket &&
|
|
97
|
+
this.websocket.readyState === this.websocket.CLOSING);
|
|
98
|
+
},
|
|
95
99
|
},
|
|
96
100
|
connecting: {
|
|
97
|
-
get: () =>
|
|
98
|
-
this.websocket
|
|
101
|
+
get: () => {
|
|
102
|
+
return !!(this.websocket &&
|
|
103
|
+
this.websocket.readyState === this.websocket.CONNECTING);
|
|
104
|
+
},
|
|
99
105
|
},
|
|
100
106
|
/**
|
|
101
107
|
* Array of message to send on open.
|
|
@@ -107,7 +113,9 @@ class WebSocketAPI {
|
|
|
107
113
|
writable: true,
|
|
108
114
|
},
|
|
109
115
|
open: {
|
|
110
|
-
get: () =>
|
|
116
|
+
get: () => {
|
|
117
|
+
return !!(this.websocket && this.websocket.readyState === this.websocket.OPEN);
|
|
118
|
+
},
|
|
111
119
|
},
|
|
112
120
|
/**
|
|
113
121
|
* List of channels subscribed.
|
|
@@ -142,22 +150,27 @@ class WebSocketAPI {
|
|
|
142
150
|
const requestString = WebSocketAPI.getRequestString('GET', params);
|
|
143
151
|
this.send(requestString);
|
|
144
152
|
// We wrap the callbacks to make sure they are called only once.
|
|
145
|
-
const once = (callback) =>
|
|
146
|
-
// @ts-expect-error : Spread error
|
|
147
|
-
(...args) => {
|
|
153
|
+
const once = (callback) => {
|
|
148
154
|
// @ts-expect-error : Spread error
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
155
|
+
return (...args) => {
|
|
156
|
+
// @ts-expect-error : Spread error
|
|
157
|
+
callback(...args);
|
|
158
|
+
const index = this.requests.findIndex((request) => {
|
|
159
|
+
return requestString === request.requestString && cb === request.cb;
|
|
160
|
+
});
|
|
161
|
+
const { onErrorCb, onMessageCb } = this.requests[index];
|
|
162
|
+
this.removeEvents(onMessageCb, onErrorCb);
|
|
163
|
+
this.requests.splice(index, 1);
|
|
164
|
+
};
|
|
154
165
|
};
|
|
155
166
|
const { onErrorCb, onMessageCb } = this.listen(params, once(cb), errorCb && once(errorCb));
|
|
156
167
|
// Store requests and callbacks to be able to remove them.
|
|
157
168
|
if (!this.requests) {
|
|
158
169
|
this.requests = [];
|
|
159
170
|
}
|
|
160
|
-
const index = this.requests.findIndex((request) =>
|
|
171
|
+
const index = this.requests.findIndex((request) => {
|
|
172
|
+
return requestString === request.requestString && cb === request.cb;
|
|
173
|
+
});
|
|
161
174
|
const newReq = {
|
|
162
175
|
cb,
|
|
163
176
|
errorCb,
|
|
@@ -167,9 +180,11 @@ class WebSocketAPI {
|
|
|
167
180
|
requestString,
|
|
168
181
|
};
|
|
169
182
|
if (index > -1) {
|
|
183
|
+
// @ts-expect-error - We know that the requests is an array of WebSocketAPIRequest
|
|
170
184
|
this.requests[index] = newReq;
|
|
171
185
|
}
|
|
172
186
|
else {
|
|
187
|
+
// @ts-expect-error - We know that the requests is an array of WebSocketAPIRequest
|
|
173
188
|
this.requests.push(newReq);
|
|
174
189
|
}
|
|
175
190
|
}
|
|
@@ -192,7 +207,6 @@ class WebSocketAPI {
|
|
|
192
207
|
data = JSON.parse(evt.data);
|
|
193
208
|
}
|
|
194
209
|
catch (err) {
|
|
195
|
-
// eslint-disable-next-line no-console
|
|
196
210
|
console.error('WebSocket: unable to parse JSON data', err, evt.data);
|
|
197
211
|
return;
|
|
198
212
|
}
|
|
@@ -201,6 +215,7 @@ class WebSocketAPI {
|
|
|
201
215
|
// Buffer channel message return a list of other channels to propagate to proper callbacks.
|
|
202
216
|
let contents;
|
|
203
217
|
if (data.source === 'buffer') {
|
|
218
|
+
// @ts-expect-error - We know that the data is a WebSocketAPIBufferMessageEventData
|
|
204
219
|
contents = data
|
|
205
220
|
.content;
|
|
206
221
|
}
|
|
@@ -270,12 +285,16 @@ class WebSocketAPI {
|
|
|
270
285
|
subscribe(params, cb, errorCb, quiet = false) {
|
|
271
286
|
const { onErrorCb, onMessageCb } = this.listen(params, cb, errorCb);
|
|
272
287
|
const reqStr = WebSocketAPI.getRequestString('', params);
|
|
273
|
-
const index = this.subscriptions.findIndex((subcr) =>
|
|
288
|
+
const index = this.subscriptions.findIndex((subcr) => {
|
|
289
|
+
return params.channel === subcr.params.channel && cb === subcr.cb;
|
|
290
|
+
});
|
|
274
291
|
const newSubscr = { cb, errorCb, onErrorCb, onMessageCb, params, quiet };
|
|
275
292
|
if (index > -1) {
|
|
293
|
+
// @ts-expect-error - We know that the subscriptions is an array of WebSocketAPISubscription
|
|
276
294
|
this.subscriptions[index] = newSubscr;
|
|
277
295
|
}
|
|
278
296
|
else {
|
|
297
|
+
// @ts-expect-error - We know that the subscriptions is an array of WebSocketAPISubscription
|
|
279
298
|
this.subscriptions.push(newSubscr);
|
|
280
299
|
}
|
|
281
300
|
if (!this.subscribed[reqStr]) {
|
|
@@ -310,7 +329,9 @@ class WebSocketAPI {
|
|
|
310
329
|
*/
|
|
311
330
|
unlisten(params, cb) {
|
|
312
331
|
[...(this.subscriptions || []), ...(this.requests || [])]
|
|
313
|
-
.filter((s) =>
|
|
332
|
+
.filter((s) => {
|
|
333
|
+
return s.params.channel === params.channel && (!cb || s.cb === cb);
|
|
334
|
+
})
|
|
314
335
|
.forEach(({ onErrorCb, onMessageCb }) => {
|
|
315
336
|
this.removeEvents(onMessageCb, onErrorCb);
|
|
316
337
|
});
|
|
@@ -322,17 +343,25 @@ class WebSocketAPI {
|
|
|
322
343
|
* @private
|
|
323
344
|
*/
|
|
324
345
|
unsubscribe(source, cb) {
|
|
325
|
-
const toRemove = this.subscriptions.filter((s) =>
|
|
346
|
+
const toRemove = this.subscriptions.filter((s) => {
|
|
347
|
+
return s.params.channel === source && (!cb || s.cb === cb);
|
|
348
|
+
});
|
|
326
349
|
toRemove.forEach(({ onErrorCb, onMessageCb }) => {
|
|
327
350
|
this.removeEvents(onMessageCb, onErrorCb);
|
|
328
351
|
});
|
|
329
|
-
this.subscriptions = this.subscriptions.filter((s) =>
|
|
352
|
+
this.subscriptions = this.subscriptions.filter((s) => {
|
|
353
|
+
return s.params.channel !== source || (cb && s.cb !== cb);
|
|
354
|
+
});
|
|
330
355
|
// If there is no more subscriptions to this channel, and the removed subscriptions didn't register quietly,
|
|
331
356
|
// we DEL it.
|
|
332
357
|
if (source &&
|
|
333
358
|
this.subscribed[source] &&
|
|
334
|
-
!this.subscriptions.find((s) =>
|
|
335
|
-
|
|
359
|
+
!this.subscriptions.find((s) => {
|
|
360
|
+
return s.params.channel === source;
|
|
361
|
+
}) &&
|
|
362
|
+
toRemove.find((subscr) => {
|
|
363
|
+
return !subscr.quiet;
|
|
364
|
+
})) {
|
|
336
365
|
this.send(`DEL ${source}`);
|
|
337
366
|
this.subscribed[source] = false;
|
|
338
367
|
}
|
|
@@ -11,7 +11,7 @@ const debounceWebsocketMessages = (onUpdate, getObjectId, timeout = 100) => {
|
|
|
11
11
|
const objectsById = {};
|
|
12
12
|
const objects = [];
|
|
13
13
|
return (data) => {
|
|
14
|
-
const {
|
|
14
|
+
const { content, source } = data;
|
|
15
15
|
if (updateTimeout[source]) {
|
|
16
16
|
window.clearTimeout(updateTimeout[source]);
|
|
17
17
|
}
|