mobility-toolbox-js 3.1.2 → 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.js +0 -1
- package/api/RealtimeAPI.d.ts +3 -3
- package/api/RealtimeAPI.js +3 -3
- package/api/WebSocketAPI.d.ts +21 -21
- package/api/WebSocketAPI.js +47 -23
- package/mbt.js +13612 -13604
- package/mbt.js.map +4 -4
- package/mbt.min.js +38 -38
- package/mbt.min.js.map +4 -4
- package/ol/layers/MaplibreLayer.d.ts +1 -1
- package/ol/layers/MaplibreLayer.js +4 -3
- package/ol/renderers/MaplibreLayerRenderer.js +1 -0
- package/package.json +4 -12
- package/types/index.d.ts +4 -4
- package/types/realtime.d.ts +224 -224
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;
|
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
|
-
export type WebSocketAPIMessageEventListener = (evt: WebSocketAPIMessageEvent) => void;
|
|
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<T> {
|
|
8
|
+
export declare interface WebSocketAPIParameters {
|
|
9
|
+
args?: number | string;
|
|
10
|
+
channel?: string;
|
|
11
|
+
id?: number | string;
|
|
12
|
+
}
|
|
13
|
+
export declare interface WebSocketAPIRequest<T> {
|
|
25
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
|
-
export declare interface WebSocketAPIRequest<T> {
|
|
21
|
+
export declare interface WebSocketAPISubscription<T> {
|
|
34
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.
|
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,
|
|
@@ -194,7 +207,6 @@ class WebSocketAPI {
|
|
|
194
207
|
data = JSON.parse(evt.data);
|
|
195
208
|
}
|
|
196
209
|
catch (err) {
|
|
197
|
-
// eslint-disable-next-line no-console
|
|
198
210
|
console.error('WebSocket: unable to parse JSON data', err, evt.data);
|
|
199
211
|
return;
|
|
200
212
|
}
|
|
@@ -273,7 +285,9 @@ class WebSocketAPI {
|
|
|
273
285
|
subscribe(params, cb, errorCb, quiet = false) {
|
|
274
286
|
const { onErrorCb, onMessageCb } = this.listen(params, cb, errorCb);
|
|
275
287
|
const reqStr = WebSocketAPI.getRequestString('', params);
|
|
276
|
-
const index = this.subscriptions.findIndex((subcr) =>
|
|
288
|
+
const index = this.subscriptions.findIndex((subcr) => {
|
|
289
|
+
return params.channel === subcr.params.channel && cb === subcr.cb;
|
|
290
|
+
});
|
|
277
291
|
const newSubscr = { cb, errorCb, onErrorCb, onMessageCb, params, quiet };
|
|
278
292
|
if (index > -1) {
|
|
279
293
|
// @ts-expect-error - We know that the subscriptions is an array of WebSocketAPISubscription
|
|
@@ -315,7 +329,9 @@ class WebSocketAPI {
|
|
|
315
329
|
*/
|
|
316
330
|
unlisten(params, cb) {
|
|
317
331
|
[...(this.subscriptions || []), ...(this.requests || [])]
|
|
318
|
-
.filter((s) =>
|
|
332
|
+
.filter((s) => {
|
|
333
|
+
return s.params.channel === params.channel && (!cb || s.cb === cb);
|
|
334
|
+
})
|
|
319
335
|
.forEach(({ onErrorCb, onMessageCb }) => {
|
|
320
336
|
this.removeEvents(onMessageCb, onErrorCb);
|
|
321
337
|
});
|
|
@@ -327,17 +343,25 @@ class WebSocketAPI {
|
|
|
327
343
|
* @private
|
|
328
344
|
*/
|
|
329
345
|
unsubscribe(source, cb) {
|
|
330
|
-
const toRemove = this.subscriptions.filter((s) =>
|
|
346
|
+
const toRemove = this.subscriptions.filter((s) => {
|
|
347
|
+
return s.params.channel === source && (!cb || s.cb === cb);
|
|
348
|
+
});
|
|
331
349
|
toRemove.forEach(({ onErrorCb, onMessageCb }) => {
|
|
332
350
|
this.removeEvents(onMessageCb, onErrorCb);
|
|
333
351
|
});
|
|
334
|
-
this.subscriptions = this.subscriptions.filter((s) =>
|
|
352
|
+
this.subscriptions = this.subscriptions.filter((s) => {
|
|
353
|
+
return s.params.channel !== source || (cb && s.cb !== cb);
|
|
354
|
+
});
|
|
335
355
|
// If there is no more subscriptions to this channel, and the removed subscriptions didn't register quietly,
|
|
336
356
|
// we DEL it.
|
|
337
357
|
if (source &&
|
|
338
358
|
this.subscribed[source] &&
|
|
339
|
-
!this.subscriptions.find((s) =>
|
|
340
|
-
|
|
359
|
+
!this.subscriptions.find((s) => {
|
|
360
|
+
return s.params.channel === source;
|
|
361
|
+
}) &&
|
|
362
|
+
toRemove.find((subscr) => {
|
|
363
|
+
return !subscr.quiet;
|
|
364
|
+
})) {
|
|
341
365
|
this.send(`DEL ${source}`);
|
|
342
366
|
this.subscribed[source] = false;
|
|
343
367
|
}
|