mobility-toolbox-js 3.0.0-beta.19 → 3.0.0-beta.20
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 +5 -5
- package/api/RealtimeAPI.d.ts +204 -171
- package/api/RealtimeAPI.js +306 -258
- package/api/RoutingAPI.d.ts +4 -4
- package/api/StopsAPI.d.ts +4 -4
- package/api/WebSocketAPI.d.ts +60 -66
- package/api/WebSocketAPI.js +164 -164
- package/api/index.js +1 -1
- package/common/controls/StopFinderControlCommon.d.ts +11 -11
- package/common/controls/StopFinderControlCommon.js +30 -30
- package/common/index.d.ts +1 -1
- package/common/index.js +1 -1
- package/common/mixins/RealtimeLayerMixin.d.ts +149 -155
- package/common/mixins/RealtimeLayerMixin.js +395 -395
- package/common/styles/realtimeDefaultStyle.js +6 -6
- package/common/styles/realtimeHeadingStyle.js +5 -5
- package/common/utils/getMapGlCopyrights.d.ts +1 -1
- package/common/utils/getMapGlCopyrights.js +3 -3
- package/common/utils/getVehiclePosition.d.ts +2 -2
- package/common/utils/getVehiclePosition.js +7 -7
- package/common/utils/renderTrajectories.js +5 -5
- package/common/utils/sortByDelay.js +5 -5
- package/maplibre/layers/RealtimeLayer.d.ts +59 -64
- package/maplibre/layers/RealtimeLayer.js +8 -8
- package/maplibre/utils/getSourceCoordinates.js +5 -5
- package/mbt.js +7205 -7031
- package/mbt.js.map +4 -4
- package/mbt.min.js +25 -25
- package/mbt.min.js.map +4 -4
- package/ol/controls/RoutingControl.d.ts +81 -87
- package/ol/controls/RoutingControl.js +216 -218
- package/ol/layers/Layer.d.ts +9 -9
- package/ol/layers/MaplibreLayer.d.ts +10 -10
- package/ol/layers/MaplibreLayer.js +9 -3
- package/ol/layers/MaplibreStyleLayer.d.ts +77 -76
- package/ol/layers/MaplibreStyleLayer.js +237 -238
- package/ol/layers/RealtimeLayer.d.ts +92 -96
- package/ol/layers/RealtimeLayer.js +139 -131
- package/ol/mixins/MobilityLayerMixin.d.ts +9 -9
- package/ol/mixins/PropertiesLayerMixin.d.ts +33 -36
- package/ol/mixins/PropertiesLayerMixin.js +73 -72
- package/ol/renderers/MaplibreLayerRenderer.js +3 -3
- package/ol/renderers/MaplibreStyleLayerRenderer.d.ts +6 -6
- package/ol/renderers/MaplibreStyleLayerRenderer.js +14 -17
- package/ol/renderers/RealtimeLayerRenderer.d.ts +6 -6
- package/ol/renderers/RealtimeLayerRenderer.js +54 -52
- package/ol/utils/getFeatureInfoAtCoordinate.d.ts +1 -1
- package/ol/utils/getFeatureInfoAtCoordinate.js +10 -16
- package/package.json +6 -5
- package/setupTests.js +3 -4
- package/types/common.d.ts +53 -49
- package/types/index.d.ts +1 -1
- package/types/realtime.d.ts +91 -93
- package/types/routing.d.ts +60 -60
- package/types/stops.d.ts +62 -62
package/api/RoutingAPI.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import HttpAPI from './HttpAPI';
|
|
2
1
|
import { RoutingParameters, RoutingResponse } from '../types';
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import HttpAPI from './HttpAPI';
|
|
3
|
+
export interface RoutingAPIOptions {
|
|
5
4
|
apiKey?: string;
|
|
6
|
-
|
|
5
|
+
url?: string;
|
|
6
|
+
}
|
|
7
7
|
/**
|
|
8
8
|
* This class provides convenience methods to use to the [geOps Routing API](https://developer.geops.io/apis/routing).
|
|
9
9
|
*
|
package/api/StopsAPI.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import HttpAPI from './HttpAPI';
|
|
2
1
|
import { StopsParameters, StopsResponse } from '../types';
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import HttpAPI from './HttpAPI';
|
|
3
|
+
export interface StopsAPIOptions {
|
|
5
4
|
apiKey?: string;
|
|
6
|
-
|
|
5
|
+
url?: string;
|
|
6
|
+
}
|
|
7
7
|
/**
|
|
8
8
|
* This class provides convenience methods to use to the [geOps Stops API](https://developer.geops.io/apis/stops/).
|
|
9
9
|
*
|
package/api/WebSocketAPI.d.ts
CHANGED
|
@@ -1,49 +1,43 @@
|
|
|
1
1
|
import { RealtimeTrajectoryResponse } from '../types';
|
|
2
|
-
export declare
|
|
2
|
+
export declare interface WebSocketAPIParameters {
|
|
3
|
+
args?: number | string;
|
|
3
4
|
channel?: string;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
timestamp: number;
|
|
9
|
-
source: string;
|
|
5
|
+
id?: number | string;
|
|
6
|
+
}
|
|
7
|
+
export declare interface WebSocketAPIMessageEventData<T> {
|
|
8
|
+
client_reference: null | number | string;
|
|
10
9
|
content: T;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
source: string;
|
|
11
|
+
timestamp: number;
|
|
12
|
+
}
|
|
13
|
+
export type WebSocketAPIBufferMessageEventData = {
|
|
14
14
|
source: 'buffer';
|
|
15
|
-
}
|
|
16
|
-
export type WebSocketAPIMessageEvent =
|
|
15
|
+
} & WebSocketAPIMessageEventData<RealtimeTrajectoryResponse[]>;
|
|
16
|
+
export type WebSocketAPIMessageEvent = {
|
|
17
17
|
data: string;
|
|
18
|
-
};
|
|
19
|
-
export
|
|
20
|
-
(evt: WebSocketAPIMessageEvent): void;
|
|
21
|
-
}
|
|
18
|
+
} & Event;
|
|
19
|
+
export type WebSocketAPIMessageEventListener = (evt: WebSocketAPIMessageEvent) => void;
|
|
22
20
|
/**
|
|
23
21
|
* This type represents a function that has been call with each feature returned by the websocket.
|
|
24
22
|
*/
|
|
25
|
-
export
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
export declare type WebSocketAPISubscription = {
|
|
29
|
-
params: WebSocketAPIParameters;
|
|
23
|
+
export type WebSocketAPIMessageCallback<T> = (data: WebSocketAPIMessageEventData<T>) => void;
|
|
24
|
+
export declare interface WebSocketAPISubscription {
|
|
30
25
|
cb: WebSocketAPIMessageCallback<any>;
|
|
31
26
|
errorCb?: EventListener;
|
|
32
|
-
onMessageCb: WebSocketAPIMessageEventListener;
|
|
33
27
|
onErrorCb?: EventListener;
|
|
34
|
-
|
|
35
|
-
};
|
|
36
|
-
export declare type WebSocketAPISubscribed = {
|
|
37
|
-
[index: string]: boolean;
|
|
38
|
-
};
|
|
39
|
-
export declare type WebSocketAPIRequest = {
|
|
28
|
+
onMessageCb: WebSocketAPIMessageEventListener;
|
|
40
29
|
params: WebSocketAPIParameters;
|
|
30
|
+
quiet: boolean;
|
|
31
|
+
}
|
|
32
|
+
export type WebSocketAPISubscribed = Record<string, boolean>;
|
|
33
|
+
export declare interface WebSocketAPIRequest {
|
|
41
34
|
cb: WebSocketAPIMessageCallback<any>;
|
|
42
35
|
errorCb?: EventListener;
|
|
43
|
-
onMessageCb: WebSocketAPIMessageEventListener;
|
|
44
36
|
onErrorCb?: EventListener;
|
|
45
|
-
|
|
46
|
-
|
|
37
|
+
onMessageCb: WebSocketAPIMessageEventListener;
|
|
38
|
+
params: WebSocketAPIParameters;
|
|
39
|
+
requestString: string;
|
|
40
|
+
}
|
|
47
41
|
/**
|
|
48
42
|
* Class used to facilitate connection to a WebSocketAPI and
|
|
49
43
|
* also to manage properly messages send to the WebSocketAPI.
|
|
@@ -51,17 +45,16 @@ export declare type WebSocketAPIRequest = {
|
|
|
51
45
|
* @private
|
|
52
46
|
*/
|
|
53
47
|
declare class WebSocketAPI {
|
|
54
|
-
websocket?: WebSocket;
|
|
55
48
|
closed?: boolean;
|
|
56
49
|
closing?: boolean;
|
|
57
50
|
connecting?: boolean;
|
|
51
|
+
messagesOnOpen: string[];
|
|
58
52
|
open?: boolean;
|
|
59
|
-
|
|
60
|
-
subscriptions: Array<WebSocketAPISubscription>;
|
|
53
|
+
requests: WebSocketAPIRequest[];
|
|
61
54
|
subscribed: WebSocketAPISubscribed;
|
|
62
|
-
|
|
55
|
+
subscriptions: WebSocketAPISubscription[];
|
|
56
|
+
websocket?: WebSocket;
|
|
63
57
|
constructor();
|
|
64
|
-
defineProperties(): void;
|
|
65
58
|
/**
|
|
66
59
|
* Get the websocket request string.
|
|
67
60
|
*
|
|
@@ -74,29 +67,32 @@ declare class WebSocketAPI {
|
|
|
74
67
|
* @private
|
|
75
68
|
*/
|
|
76
69
|
static getRequestString(method: string, params?: WebSocketAPIParameters): string;
|
|
70
|
+
addEvents(onMessage: WebSocketAPIMessageEventListener, onError?: EventListener): void;
|
|
77
71
|
/**
|
|
78
|
-
*
|
|
72
|
+
* Close the websocket definitively.
|
|
79
73
|
*
|
|
80
|
-
* @param {string} url Websocket url.
|
|
81
|
-
* @param {function} onOpen Callback called when the websocket connection is opened and before subscriptions of previous subscriptions.
|
|
82
74
|
* @private
|
|
83
75
|
*/
|
|
84
|
-
|
|
76
|
+
close(): void;
|
|
85
77
|
/**
|
|
86
|
-
*
|
|
78
|
+
* (Re)connect the websocket.
|
|
87
79
|
*
|
|
80
|
+
* @param {string} url Websocket url.
|
|
81
|
+
* @param {function} onOpen Callback called when the websocket connection is opened and before subscriptions of previous subscriptions.
|
|
88
82
|
* @private
|
|
89
83
|
*/
|
|
90
|
-
|
|
84
|
+
connect(url: string, onOpen?: () => void): void;
|
|
85
|
+
defineProperties(): void;
|
|
91
86
|
/**
|
|
92
|
-
* Sends a
|
|
87
|
+
* Sends a get request to the websocket.
|
|
88
|
+
* The callback is called only once, when the response is received or when the call returns an error.
|
|
93
89
|
*
|
|
94
|
-
* @param {
|
|
90
|
+
* @param {Object} params Parameters for the websocket get request
|
|
91
|
+
* @param {function} cb callback on message event
|
|
92
|
+
* @param {function} errorCb Callback on error and close event
|
|
95
93
|
* @private
|
|
96
94
|
*/
|
|
97
|
-
|
|
98
|
-
addEvents(onMessage: WebSocketAPIMessageEventListener, onError?: EventListener): void;
|
|
99
|
-
removeEvents(onMessage: WebSocketAPIMessageEventListener, onError?: EventListener): void;
|
|
95
|
+
get(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<any>, errorCb?: EventListener): void;
|
|
100
96
|
/**
|
|
101
97
|
* Listen to websocket messages.
|
|
102
98
|
*
|
|
@@ -107,27 +103,17 @@ declare class WebSocketAPI {
|
|
|
107
103
|
* @private
|
|
108
104
|
*/
|
|
109
105
|
listen(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<any>, errorCb?: EventListener): {
|
|
110
|
-
onMessageCb: WebSocketAPIMessageEventListener;
|
|
111
106
|
onErrorCb?: EventListener;
|
|
107
|
+
onMessageCb: WebSocketAPIMessageEventListener;
|
|
112
108
|
};
|
|
109
|
+
removeEvents(onMessage: WebSocketAPIMessageEventListener, onError?: EventListener): void;
|
|
113
110
|
/**
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* @param {Object} params Parameters for the websocket get request.
|
|
117
|
-
* @param {function} cb Callback used when listen.
|
|
118
|
-
* @private
|
|
119
|
-
*/
|
|
120
|
-
unlisten(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<any>): void;
|
|
121
|
-
/**
|
|
122
|
-
* Sends a get request to the websocket.
|
|
123
|
-
* The callback is called only once, when the response is received or when the call returns an error.
|
|
111
|
+
* Sends a message to the websocket.
|
|
124
112
|
*
|
|
125
|
-
* @param {
|
|
126
|
-
* @param {function} cb callback on message event
|
|
127
|
-
* @param {function} errorCb Callback on error and close event
|
|
113
|
+
* @param {message} message Message to send.
|
|
128
114
|
* @private
|
|
129
115
|
*/
|
|
130
|
-
|
|
116
|
+
send(message: string): void;
|
|
131
117
|
/**
|
|
132
118
|
* Subscribe to a given channel.
|
|
133
119
|
*
|
|
@@ -138,6 +124,18 @@ declare class WebSocketAPI {
|
|
|
138
124
|
* @private
|
|
139
125
|
*/
|
|
140
126
|
subscribe(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<any>, errorCb?: EventListener, quiet?: boolean): void;
|
|
127
|
+
/**
|
|
128
|
+
* After an auto reconnection we need to re-subscribe to the channels.
|
|
129
|
+
*/
|
|
130
|
+
subscribePreviousSubscriptions(): void;
|
|
131
|
+
/**
|
|
132
|
+
* Unlisten websocket messages.
|
|
133
|
+
*
|
|
134
|
+
* @param {Object} params Parameters for the websocket get request.
|
|
135
|
+
* @param {function} cb Callback used when listen.
|
|
136
|
+
* @private
|
|
137
|
+
*/
|
|
138
|
+
unlisten(params: WebSocketAPIParameters, cb: WebSocketAPIMessageCallback<any>): void;
|
|
141
139
|
/**
|
|
142
140
|
* Unsubscribe from a channel.
|
|
143
141
|
* @param {string} source source to unsubscribe from
|
|
@@ -145,9 +143,5 @@ declare class WebSocketAPI {
|
|
|
145
143
|
* @private
|
|
146
144
|
*/
|
|
147
145
|
unsubscribe(source: string, cb?: WebSocketAPIMessageCallback<any>): void;
|
|
148
|
-
/**
|
|
149
|
-
* After an auto reconnection we need to re-subscribe to the channels.
|
|
150
|
-
*/
|
|
151
|
-
subscribePreviousSubscriptions(): void;
|
|
152
146
|
}
|
|
153
147
|
export default WebSocketAPI;
|
package/api/WebSocketAPI.js
CHANGED
|
@@ -8,52 +8,6 @@ class WebSocketAPI {
|
|
|
8
8
|
constructor() {
|
|
9
9
|
this.defineProperties();
|
|
10
10
|
}
|
|
11
|
-
defineProperties() {
|
|
12
|
-
Object.defineProperties(this, {
|
|
13
|
-
closed: {
|
|
14
|
-
get: () => !!(!this.websocket ||
|
|
15
|
-
this.websocket.readyState === this.websocket.CLOSED),
|
|
16
|
-
},
|
|
17
|
-
closing: {
|
|
18
|
-
get: () => !!(this.websocket &&
|
|
19
|
-
this.websocket.readyState === this.websocket.CLOSING),
|
|
20
|
-
},
|
|
21
|
-
connecting: {
|
|
22
|
-
get: () => !!(this.websocket &&
|
|
23
|
-
this.websocket.readyState === this.websocket.CONNECTING),
|
|
24
|
-
},
|
|
25
|
-
open: {
|
|
26
|
-
get: () => !!(this.websocket && this.websocket.readyState === this.websocket.OPEN),
|
|
27
|
-
},
|
|
28
|
-
/**
|
|
29
|
-
* Array of message to send on open.
|
|
30
|
-
* @type {Array<string>}
|
|
31
|
-
* @private
|
|
32
|
-
*/
|
|
33
|
-
messagesOnOpen: {
|
|
34
|
-
value: [],
|
|
35
|
-
writable: true,
|
|
36
|
-
},
|
|
37
|
-
/**
|
|
38
|
-
* Array of subscriptions.
|
|
39
|
-
* @type {Array<WebSocketSubscription>}
|
|
40
|
-
* @private
|
|
41
|
-
*/
|
|
42
|
-
subscriptions: {
|
|
43
|
-
value: [],
|
|
44
|
-
writable: true,
|
|
45
|
-
},
|
|
46
|
-
/**
|
|
47
|
-
* List of channels subscribed.
|
|
48
|
-
* @type {WebSocketSubscribed}
|
|
49
|
-
* @private
|
|
50
|
-
*/
|
|
51
|
-
subscribed: {
|
|
52
|
-
value: {},
|
|
53
|
-
writable: true,
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
11
|
/**
|
|
58
12
|
* Get the websocket request string.
|
|
59
13
|
*
|
|
@@ -71,6 +25,27 @@ class WebSocketAPI {
|
|
|
71
25
|
reqStr += params.id ? ` ${params.id}` : '';
|
|
72
26
|
return reqStr.trim();
|
|
73
27
|
}
|
|
28
|
+
addEvents(onMessage, onError) {
|
|
29
|
+
if (this.websocket) {
|
|
30
|
+
this.websocket.addEventListener('message', onMessage);
|
|
31
|
+
if (onError) {
|
|
32
|
+
this.websocket.addEventListener('error', onError);
|
|
33
|
+
this.websocket.addEventListener('close', onError);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Close the websocket definitively.
|
|
39
|
+
*
|
|
40
|
+
* @private
|
|
41
|
+
*/
|
|
42
|
+
close() {
|
|
43
|
+
if (this.websocket && (this.open || this.connecting)) {
|
|
44
|
+
this.websocket.onclose = () => { };
|
|
45
|
+
this.websocket.close();
|
|
46
|
+
this.messagesOnOpen = [];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
74
49
|
/**
|
|
75
50
|
* (Re)connect the websocket.
|
|
76
51
|
*
|
|
@@ -109,65 +84,94 @@ class WebSocketAPI {
|
|
|
109
84
|
this.subscribePreviousSubscriptions();
|
|
110
85
|
}
|
|
111
86
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
87
|
+
defineProperties() {
|
|
88
|
+
Object.defineProperties(this, {
|
|
89
|
+
closed: {
|
|
90
|
+
get: () => !!(!this.websocket ||
|
|
91
|
+
this.websocket.readyState === this.websocket.CLOSED),
|
|
92
|
+
},
|
|
93
|
+
closing: {
|
|
94
|
+
get: () => !!(this.websocket &&
|
|
95
|
+
this.websocket.readyState === this.websocket.CLOSING),
|
|
96
|
+
},
|
|
97
|
+
connecting: {
|
|
98
|
+
get: () => !!(this.websocket &&
|
|
99
|
+
this.websocket.readyState === this.websocket.CONNECTING),
|
|
100
|
+
},
|
|
101
|
+
/**
|
|
102
|
+
* Array of message to send on open.
|
|
103
|
+
* @type {Array<string>}
|
|
104
|
+
* @private
|
|
105
|
+
*/
|
|
106
|
+
messagesOnOpen: {
|
|
107
|
+
value: [],
|
|
108
|
+
writable: true,
|
|
109
|
+
},
|
|
110
|
+
open: {
|
|
111
|
+
get: () => !!(this.websocket && this.websocket.readyState === this.websocket.OPEN),
|
|
112
|
+
},
|
|
113
|
+
/**
|
|
114
|
+
* List of channels subscribed.
|
|
115
|
+
* @type {WebSocketSubscribed}
|
|
116
|
+
* @private
|
|
117
|
+
*/
|
|
118
|
+
subscribed: {
|
|
119
|
+
value: {},
|
|
120
|
+
writable: true,
|
|
121
|
+
},
|
|
122
|
+
/**
|
|
123
|
+
* Array of subscriptions.
|
|
124
|
+
* @type {Array<WebSocketSubscription>}
|
|
125
|
+
* @private
|
|
126
|
+
*/
|
|
127
|
+
subscriptions: {
|
|
128
|
+
value: [],
|
|
129
|
+
writable: true,
|
|
130
|
+
},
|
|
131
|
+
});
|
|
123
132
|
}
|
|
124
133
|
/**
|
|
125
|
-
* Sends a
|
|
134
|
+
* Sends a get request to the websocket.
|
|
135
|
+
* The callback is called only once, when the response is received or when the call returns an error.
|
|
126
136
|
*
|
|
127
|
-
* @param {
|
|
137
|
+
* @param {Object} params Parameters for the websocket get request
|
|
138
|
+
* @param {function} cb callback on message event
|
|
139
|
+
* @param {function} errorCb Callback on error and close event
|
|
128
140
|
* @private
|
|
129
141
|
*/
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
|
|
142
|
+
get(params, cb, errorCb) {
|
|
143
|
+
const requestString = WebSocketAPI.getRequestString('GET', params);
|
|
144
|
+
this.send(requestString);
|
|
145
|
+
// We wrap the callbacks to make sure they are called only once.
|
|
146
|
+
const once = (callback) =>
|
|
147
|
+
// @ts-expect-error : Spread error
|
|
148
|
+
(...args) => {
|
|
149
|
+
// @ts-expect-error : Spread error
|
|
150
|
+
callback(...args);
|
|
151
|
+
const index = this.requests.findIndex((request) => requestString === request.requestString && cb === request.cb);
|
|
152
|
+
const { onErrorCb, onMessageCb } = this.requests[index];
|
|
153
|
+
this.removeEvents(onMessageCb, onErrorCb);
|
|
154
|
+
this.requests.splice(index, 1);
|
|
137
155
|
};
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
this.websocket.addEventListener('open', () => {
|
|
143
|
-
this.messagesOnOpen = [];
|
|
144
|
-
send();
|
|
145
|
-
});
|
|
146
|
-
this.websocket.addEventListener('close', () => {
|
|
147
|
-
this.messagesOnOpen = [];
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
else if (!this.messagesOnOpen.includes(message)) {
|
|
152
|
-
send();
|
|
156
|
+
const { onErrorCb, onMessageCb } = this.listen(params, once(cb), errorCb && once(errorCb));
|
|
157
|
+
// Store requests and callbacks to be able to remove them.
|
|
158
|
+
if (!this.requests) {
|
|
159
|
+
this.requests = [];
|
|
153
160
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
161
|
+
const index = this.requests.findIndex((request) => requestString === request.requestString && cb === request.cb);
|
|
162
|
+
const newReq = {
|
|
163
|
+
cb,
|
|
164
|
+
errorCb,
|
|
165
|
+
onErrorCb,
|
|
166
|
+
onMessageCb,
|
|
167
|
+
params,
|
|
168
|
+
requestString,
|
|
169
|
+
};
|
|
170
|
+
if (index > -1) {
|
|
171
|
+
this.requests[index] = newReq;
|
|
162
172
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (this.websocket) {
|
|
166
|
-
this.websocket.removeEventListener('message', onMessage);
|
|
167
|
-
if (onError) {
|
|
168
|
-
this.websocket.removeEventListener('error', onError);
|
|
169
|
-
this.websocket.removeEventListener('close', onError);
|
|
170
|
-
}
|
|
173
|
+
else {
|
|
174
|
+
this.requests.push(newReq);
|
|
171
175
|
}
|
|
172
176
|
}
|
|
173
177
|
/**
|
|
@@ -213,64 +217,46 @@ class WebSocketAPI {
|
|
|
213
217
|
});
|
|
214
218
|
};
|
|
215
219
|
this.addEvents(onMessage, errorCb);
|
|
216
|
-
return {
|
|
220
|
+
return { onErrorCb: errorCb, onMessageCb: onMessage };
|
|
217
221
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
[...(this.subscriptions || []), ...(this.requests || [])]
|
|
227
|
-
.filter((s) => s.params.channel === params.channel && (!cb || s.cb === cb))
|
|
228
|
-
.forEach(({ onMessageCb, onErrorCb }) => {
|
|
229
|
-
this.removeEvents(onMessageCb, onErrorCb);
|
|
230
|
-
});
|
|
222
|
+
removeEvents(onMessage, onError) {
|
|
223
|
+
if (this.websocket) {
|
|
224
|
+
this.websocket.removeEventListener('message', onMessage);
|
|
225
|
+
if (onError) {
|
|
226
|
+
this.websocket.removeEventListener('error', onError);
|
|
227
|
+
this.websocket.removeEventListener('close', onError);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
231
230
|
}
|
|
232
231
|
/**
|
|
233
|
-
* Sends a
|
|
234
|
-
* The callback is called only once, when the response is received or when the call returns an error.
|
|
232
|
+
* Sends a message to the websocket.
|
|
235
233
|
*
|
|
236
|
-
* @param {
|
|
237
|
-
* @param {function} cb callback on message event
|
|
238
|
-
* @param {function} errorCb Callback on error and close event
|
|
234
|
+
* @param {message} message Message to send.
|
|
239
235
|
* @private
|
|
240
236
|
*/
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
// We wrap the callbacks to make sure they are called only once.
|
|
245
|
-
const once = (callback) =>
|
|
246
|
-
// @ts-ignore: Spread error
|
|
247
|
-
(...args) => {
|
|
248
|
-
// @ts-ignore: Spread error
|
|
249
|
-
callback(...args);
|
|
250
|
-
const index = this.requests.findIndex((request) => requestString === request.requestString && cb === request.cb);
|
|
251
|
-
const { onMessageCb, onErrorCb } = this.requests[index];
|
|
252
|
-
this.removeEvents(onMessageCb, onErrorCb);
|
|
253
|
-
this.requests.splice(index, 1);
|
|
254
|
-
};
|
|
255
|
-
const { onMessageCb, onErrorCb } = this.listen(params, once(cb), errorCb && once(errorCb));
|
|
256
|
-
// Store requests and callbacks to be able to remove them.
|
|
257
|
-
if (!this.requests) {
|
|
258
|
-
this.requests = [];
|
|
237
|
+
send(message) {
|
|
238
|
+
if (!this.websocket || this.closed || this.closing) {
|
|
239
|
+
return;
|
|
259
240
|
}
|
|
260
|
-
const
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
requestString,
|
|
264
|
-
cb,
|
|
265
|
-
errorCb,
|
|
266
|
-
onMessageCb,
|
|
267
|
-
onErrorCb,
|
|
241
|
+
const send = () => {
|
|
242
|
+
var _a;
|
|
243
|
+
(_a = this.websocket) === null || _a === void 0 ? void 0 : _a.send(message);
|
|
268
244
|
};
|
|
269
|
-
if (
|
|
270
|
-
|
|
245
|
+
if (!this.open) {
|
|
246
|
+
// This 'if' avoid sending 2 identical BBOX message on open,
|
|
247
|
+
if (!this.messagesOnOpen.includes(message)) {
|
|
248
|
+
this.messagesOnOpen.push(message);
|
|
249
|
+
this.websocket.addEventListener('open', () => {
|
|
250
|
+
this.messagesOnOpen = [];
|
|
251
|
+
send();
|
|
252
|
+
});
|
|
253
|
+
this.websocket.addEventListener('close', () => {
|
|
254
|
+
this.messagesOnOpen = [];
|
|
255
|
+
});
|
|
256
|
+
}
|
|
271
257
|
}
|
|
272
|
-
else {
|
|
273
|
-
|
|
258
|
+
else if (!this.messagesOnOpen.includes(message)) {
|
|
259
|
+
send();
|
|
274
260
|
}
|
|
275
261
|
}
|
|
276
262
|
/**
|
|
@@ -283,10 +269,10 @@ class WebSocketAPI {
|
|
|
283
269
|
* @private
|
|
284
270
|
*/
|
|
285
271
|
subscribe(params, cb, errorCb, quiet = false) {
|
|
286
|
-
const {
|
|
272
|
+
const { onErrorCb, onMessageCb } = this.listen(params, cb, errorCb);
|
|
287
273
|
const reqStr = WebSocketAPI.getRequestString('', params);
|
|
288
274
|
const index = this.subscriptions.findIndex((subcr) => params.channel === subcr.params.channel && cb === subcr.cb);
|
|
289
|
-
const newSubscr = {
|
|
275
|
+
const newSubscr = { cb, errorCb, onErrorCb, onMessageCb, params, quiet };
|
|
290
276
|
if (index > -1) {
|
|
291
277
|
this.subscriptions[index] = newSubscr;
|
|
292
278
|
}
|
|
@@ -301,6 +287,35 @@ class WebSocketAPI {
|
|
|
301
287
|
this.subscribed[reqStr] = true;
|
|
302
288
|
}
|
|
303
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
* After an auto reconnection we need to re-subscribe to the channels.
|
|
292
|
+
*/
|
|
293
|
+
subscribePreviousSubscriptions() {
|
|
294
|
+
// Before to subscribe previous subscriptions we make sure they
|
|
295
|
+
// are all defined as unsubscribed, because this code is asynchrone
|
|
296
|
+
// and a subscription could have been added in between.
|
|
297
|
+
Object.keys(this.subscribed).forEach((key) => {
|
|
298
|
+
this.subscribed[key] = false;
|
|
299
|
+
});
|
|
300
|
+
// Subscribe all previous subscriptions.
|
|
301
|
+
[...this.subscriptions].forEach((s) => {
|
|
302
|
+
this.subscribe(s.params, s.cb, s.errorCb, s.quiet);
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Unlisten websocket messages.
|
|
307
|
+
*
|
|
308
|
+
* @param {Object} params Parameters for the websocket get request.
|
|
309
|
+
* @param {function} cb Callback used when listen.
|
|
310
|
+
* @private
|
|
311
|
+
*/
|
|
312
|
+
unlisten(params, cb) {
|
|
313
|
+
[...(this.subscriptions || []), ...(this.requests || [])]
|
|
314
|
+
.filter((s) => s.params.channel === params.channel && (!cb || s.cb === cb))
|
|
315
|
+
.forEach(({ onErrorCb, onMessageCb }) => {
|
|
316
|
+
this.removeEvents(onMessageCb, onErrorCb);
|
|
317
|
+
});
|
|
318
|
+
}
|
|
304
319
|
/**
|
|
305
320
|
* Unsubscribe from a channel.
|
|
306
321
|
* @param {string} source source to unsubscribe from
|
|
@@ -309,7 +324,7 @@ class WebSocketAPI {
|
|
|
309
324
|
*/
|
|
310
325
|
unsubscribe(source, cb) {
|
|
311
326
|
const toRemove = this.subscriptions.filter((s) => s.params.channel === source && (!cb || s.cb === cb));
|
|
312
|
-
toRemove.forEach(({
|
|
327
|
+
toRemove.forEach(({ onErrorCb, onMessageCb }) => {
|
|
313
328
|
this.removeEvents(onMessageCb, onErrorCb);
|
|
314
329
|
});
|
|
315
330
|
this.subscriptions = this.subscriptions.filter((s) => s.params.channel !== source || (cb && s.cb !== cb));
|
|
@@ -323,20 +338,5 @@ class WebSocketAPI {
|
|
|
323
338
|
this.subscribed[source] = false;
|
|
324
339
|
}
|
|
325
340
|
}
|
|
326
|
-
/**
|
|
327
|
-
* After an auto reconnection we need to re-subscribe to the channels.
|
|
328
|
-
*/
|
|
329
|
-
subscribePreviousSubscriptions() {
|
|
330
|
-
// Before to subscribe previous subscriptions we make sure they
|
|
331
|
-
// are all defined as unsubscribed, because this code is asynchrone
|
|
332
|
-
// and a subscription could have been added in between.
|
|
333
|
-
Object.keys(this.subscribed).forEach((key) => {
|
|
334
|
-
this.subscribed[key] = false;
|
|
335
|
-
});
|
|
336
|
-
// Subscribe all previous subscriptions.
|
|
337
|
-
[...this.subscriptions].forEach((s) => {
|
|
338
|
-
this.subscribe(s.params, s.cb, s.errorCb, s.quiet);
|
|
339
|
-
});
|
|
340
|
-
}
|
|
341
341
|
}
|
|
342
342
|
export default WebSocketAPI;
|
package/api/index.js
CHANGED