mobility-toolbox-js 2.0.0-beta.32 → 2.0.0-beta.33
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/RealtimeAPI.d.ts +268 -0
- package/api/RealtimeAPI.d.ts.map +1 -0
- package/api/RoutingAPI.d.ts +33 -0
- package/api/RoutingAPI.d.ts.map +1 -0
- package/api/StopsAPI.d.ts +36 -0
- package/api/StopsAPI.d.ts.map +1 -0
- package/api/index.d.ts +5 -0
- package/api/index.d.ts.map +1 -0
- package/api/typedefs.d.ts +105 -0
- package/api/typedefs.d.ts.map +1 -0
- package/common/api/HttpAPI.d.ts +31 -0
- package/common/api/HttpAPI.d.ts.map +1 -0
- package/common/api/WebSocketAPI.d.ts +95 -0
- package/common/api/WebSocketAPI.d.ts.map +1 -0
- package/common/controls/Control.d.ts +74 -0
- package/common/controls/Control.d.ts.map +1 -0
- package/common/{index.js → index.d.ts} +1 -0
- package/common/index.d.ts.map +1 -0
- package/common/layers/Layer.d.ts +80 -0
- package/common/layers/Layer.d.ts.map +1 -0
- package/common/mixins/CopyrightMixin.d.ts +22 -0
- package/common/mixins/CopyrightMixin.d.ts.map +1 -0
- package/common/mixins/MapboxLayerMixin.d.ts +27 -0
- package/common/mixins/MapboxLayerMixin.d.ts.map +1 -0
- package/common/mixins/RealtimeLayerMixin.d.ts +58 -0
- package/common/mixins/RealtimeLayerMixin.d.ts.map +1 -0
- package/common/mixins/StopFinderMixin.d.ts +40 -0
- package/common/mixins/StopFinderMixin.d.ts.map +1 -0
- package/common/mixins/UserInteractionsLayerMixin.d.ts +42 -0
- package/common/mixins/UserInteractionsLayerMixin.d.ts.map +1 -0
- package/common/styles/{index.js → index.d.ts} +1 -0
- package/common/styles/index.d.ts.map +1 -0
- package/common/styles/realtimeDefaultStyle.d.ts +15 -0
- package/common/styles/realtimeDefaultStyle.d.ts.map +1 -0
- package/common/styles/realtimeDelayStyle.d.ts +11 -0
- package/common/styles/realtimeDelayStyle.d.ts.map +1 -0
- package/common/styles/realtimeSimpleStyle.d.ts +3 -0
- package/common/styles/realtimeSimpleStyle.d.ts.map +1 -0
- package/common/typedefs.d.ts +50 -0
- package/common/typedefs.d.ts.map +1 -0
- package/common/utils/cleanStopTime.d.ts +7 -0
- package/common/utils/cleanStopTime.d.ts.map +1 -0
- package/common/utils/compareDepartures.d.ts +9 -0
- package/common/utils/compareDepartures.d.ts.map +1 -0
- package/common/utils/createCanvas.d.ts +9 -0
- package/common/utils/createCanvas.d.ts.map +1 -0
- package/common/utils/createTrackerFilters.d.ts +12 -0
- package/common/utils/createTrackerFilters.d.ts.map +1 -0
- package/common/utils/getLayersAsFlatArray.d.ts +3 -0
- package/common/utils/getLayersAsFlatArray.d.ts.map +1 -0
- package/common/utils/getMapboxMapCopyrights.d.ts +8 -0
- package/common/utils/getMapboxMapCopyrights.d.ts.map +1 -0
- package/common/utils/getMapboxRender.d.ts +5 -0
- package/common/utils/getMapboxRender.d.ts.map +1 -0
- package/common/utils/getMaplibreRender.d.ts +5 -0
- package/common/utils/getMaplibreRender.d.ts.map +1 -0
- package/common/utils/getRealtimeModeSuffix.d.ts +8 -0
- package/common/utils/getRealtimeModeSuffix.d.ts.map +1 -0
- package/common/utils/getUrlWithParams.d.ts +9 -0
- package/common/utils/getUrlWithParams.d.ts.map +1 -0
- package/common/utils/getVehiclePosition.d.ts +14 -0
- package/common/utils/getVehiclePosition.d.ts.map +1 -0
- package/common/utils/{index.js → index.d.ts} +3 -0
- package/common/utils/index.d.ts.map +1 -0
- package/common/utils/removeDuplicate.d.ts +10 -0
- package/common/utils/removeDuplicate.d.ts.map +1 -0
- package/common/utils/renderTrajectories.d.ts +20 -0
- package/common/utils/renderTrajectories.d.ts.map +1 -0
- package/common/utils/sortByDelay.d.ts +3 -0
- package/common/utils/sortByDelay.d.ts.map +1 -0
- package/common/utils/timeUtils.d.ts +5 -0
- package/common/utils/timeUtils.d.ts.map +1 -0
- package/common/utils/trackerConfig.d.ts +24 -0
- package/common/utils/trackerConfig.d.ts.map +1 -0
- package/iife.d.ts +3 -0
- package/iife.d.ts.map +1 -0
- package/index.d.ts +6 -0
- package/index.d.ts.map +1 -0
- package/mapbox/controls/CopyrightControl.d.ts +32 -0
- package/mapbox/controls/CopyrightControl.d.ts.map +1 -0
- package/mapbox/controls/{index.js → index.d.ts} +1 -0
- package/mapbox/controls/index.d.ts.map +1 -0
- package/mapbox/{index.js → index.d.ts} +1 -0
- package/mapbox/index.d.ts.map +1 -0
- package/mapbox/layers/Layer.d.ts +47 -0
- package/mapbox/layers/Layer.d.ts.map +1 -0
- package/mapbox/layers/RealtimeLayer.d.ts +118 -0
- package/mapbox/layers/RealtimeLayer.d.ts.map +1 -0
- package/mapbox/layers/{index.js → index.d.ts} +1 -0
- package/mapbox/layers/index.d.ts.map +1 -0
- package/mapbox/utils.d.ts +8 -0
- package/mapbox/utils.d.ts.map +1 -0
- package/mbt.js +2049 -2106
- package/mbt.js.map +3 -3
- package/mbt.min.js +18 -18
- package/mbt.min.js.map +3 -3
- package/ol/controls/CopyrightControl.d.ts +31 -0
- package/ol/controls/CopyrightControl.d.ts.map +1 -0
- package/ol/controls/RoutingControl.d.ts +180 -0
- package/ol/controls/RoutingControl.d.ts.map +1 -0
- package/ol/controls/StopFinderControl.d.ts +32 -0
- package/ol/controls/StopFinderControl.d.ts.map +1 -0
- package/ol/controls/{index.js → index.d.ts} +1 -0
- package/ol/controls/index.d.ts.map +1 -0
- package/ol/{index.js → index.d.ts} +1 -0
- package/ol/index.d.ts.map +1 -0
- package/ol/layers/Layer.d.ts +49 -0
- package/ol/layers/Layer.d.ts.map +1 -0
- package/ol/layers/MapboxLayer.d.ts +42 -0
- package/ol/layers/MapboxLayer.d.ts.map +1 -0
- package/ol/layers/MapboxStyleLayer.d.ts +146 -0
- package/ol/layers/MapboxStyleLayer.d.ts.map +1 -0
- package/ol/layers/MaplibreLayer.d.ts +27 -0
- package/ol/layers/MaplibreLayer.d.ts.map +1 -0
- package/ol/layers/RealtimeLayer.d.ts +119 -0
- package/ol/layers/RealtimeLayer.d.ts.map +1 -0
- package/ol/layers/RoutingLayer.d.ts +24 -0
- package/ol/layers/RoutingLayer.d.ts.map +1 -0
- package/ol/layers/VectorLayer.d.ts +23 -0
- package/ol/layers/VectorLayer.d.ts.map +1 -0
- package/ol/layers/WMSLayer.d.ts +38 -0
- package/ol/layers/WMSLayer.d.ts.map +1 -0
- package/ol/layers/{index.js → index.d.ts} +1 -0
- package/ol/layers/index.d.ts.map +1 -0
- package/ol/styles/fullTrajectoryDelayStyle.d.ts +4 -0
- package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +1 -0
- package/ol/styles/fullTrajectoryStyle.d.ts +4 -0
- package/ol/styles/fullTrajectoryStyle.d.ts.map +1 -0
- package/ol/styles/{index.js → index.d.ts} +1 -0
- package/ol/styles/index.d.ts.map +1 -0
- package/package.json +14 -36
- package/setupTests.d.ts +2 -0
- package/setupTests.d.ts.map +1 -0
- package/types/index.d.ts +7 -6
- package/types/routing.d.ts +25 -25
- package/types/stops.d.ts +19 -19
- package/api/RealtimeAPI.js +0 -357
- package/api/RealtimeAPI.test.js +0 -67
- package/api/RoutingAPI.js +0 -15
- package/api/RoutingAPI.test.js +0 -25
- package/api/StopsAPI.js +0 -12
- package/api/StopsAPI.test.js +0 -22
- package/api/index.js +0 -3
- package/api/typedefs.js +0 -0
- package/common/api/HttpAPI.js +0 -32
- package/common/api/HttpAPI.test.js +0 -50
- package/common/api/WebSocketAPI.js +0 -175
- package/common/api/WebSocketAPI.test.js +0 -311
- package/common/controls/Control.js +0 -81
- package/common/controls/Control.test.js +0 -87
- package/common/layers/Layer.js +0 -140
- package/common/layers/Layer.test.js +0 -134
- package/common/mixins/CopyrightMixin.js +0 -24
- package/common/mixins/RealtimeLayerMixin.js +0 -436
- package/common/mixins/StopFinderMixin.js +0 -110
- package/common/mixins/UserInteractionsLayerMixin.js +0 -124
- package/common/mixins/UserInteractionsLayerMixin.test.js +0 -199
- package/common/styles/realtimeDefaultStyle.js +0 -202
- package/common/styles/realtimeDelayStyle.js +0 -8
- package/common/styles/realtimeSimpleStyle.js +0 -18
- package/common/typedefs.js +0 -0
- package/common/utils/cleanStopTime.js +0 -19
- package/common/utils/compareDepartures.js +0 -25
- package/common/utils/createCanvas.js +0 -17
- package/common/utils/createTrackerFilters.js +0 -56
- package/common/utils/createTrackerFilters.test.js +0 -79
- package/common/utils/getLayersAsFlatArray.js +0 -14
- package/common/utils/getMapboxMapCopyrights.js +0 -19
- package/common/utils/getMapboxMapCopyrights.test.js +0 -40
- package/common/utils/getRealtimeModeSuffix.js +0 -2
- package/common/utils/getUrlWithParams.js +0 -11
- package/common/utils/getVehiclePosition.js +0 -42
- package/common/utils/removeDuplicate.js +0 -8
- package/common/utils/removeDuplicate.test.js +0 -19
- package/common/utils/renderTrajectories.js +0 -86
- package/common/utils/sortByDelay.js +0 -18
- package/common/utils/timeUtils.js +0 -20
- package/common/utils/timeUtils.test.js +0 -10
- package/common/utils/trackerConfig.js +0 -129
- package/common/utils/trackerConfig.test.js +0 -23
- package/index.js +0 -8
- package/mapbox/controls/CopyrightControl.js +0 -29
- package/mapbox/layers/Layer.js +0 -56
- package/mapbox/layers/Layer.test.js +0 -182
- package/mapbox/layers/RealtimeLayer.js +0 -185
- package/mapbox/layers/RealtimeLayer.test.js +0 -10
- package/mapbox/utils.js +0 -32
- package/ol/controls/CopyrightControl.js +0 -42
- package/ol/controls/CopyrightControl.test.js +0 -165
- package/ol/controls/RoutingControl.js +0 -388
- package/ol/controls/RoutingControl.test.js +0 -146
- package/ol/controls/StopFinderControl.js +0 -10
- package/ol/controls/StopFinderControl.test.js +0 -48
- package/ol/layers/Layer.js +0 -83
- package/ol/layers/Layer.test.js +0 -175
- package/ol/layers/MapboxLayer.js +0 -204
- package/ol/layers/MapboxLayer.test.js +0 -160
- package/ol/layers/MapboxStyleLayer.js +0 -187
- package/ol/layers/MapboxStyleLayer.test.js +0 -226
- package/ol/layers/MaplibreLayer.js +0 -140
- package/ol/layers/RealtimeLayer.js +0 -187
- package/ol/layers/RealtimeLayer.test.js +0 -67
- package/ol/layers/RoutingLayer.js +0 -61
- package/ol/layers/RoutingLayer.test.js +0 -39
- package/ol/layers/VectorLayer.js +0 -22
- package/ol/layers/VectorLayer.test.js +0 -76
- package/ol/layers/WMSLayer.js +0 -38
- package/ol/layers/WMSLayer.test.js +0 -57
- package/ol/styles/fullTrajectoryDelayStyle.js +0 -31
- package/ol/styles/fullTrajectoryStyle.js +0 -42
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
class WebSocketAPI {
|
|
2
|
-
constructor() {
|
|
3
|
-
this.defineProperties();
|
|
4
|
-
}
|
|
5
|
-
defineProperties() {
|
|
6
|
-
Object.defineProperties(this, {
|
|
7
|
-
closed: {
|
|
8
|
-
get: () => !!(this.websocket && this.websocket.readyState === this.websocket.CLOSED)
|
|
9
|
-
},
|
|
10
|
-
closing: {
|
|
11
|
-
get: () => !!(this.websocket && this.websocket.readyState === this.websocket.CLOSING)
|
|
12
|
-
},
|
|
13
|
-
connecting: {
|
|
14
|
-
get: () => !!(this.websocket && this.websocket.readyState === this.websocket.CONNECTING)
|
|
15
|
-
},
|
|
16
|
-
open: {
|
|
17
|
-
get: () => !!(this.websocket && this.websocket.readyState === this.websocket.OPEN)
|
|
18
|
-
},
|
|
19
|
-
messagesOnOpen: {
|
|
20
|
-
value: [],
|
|
21
|
-
writable: true
|
|
22
|
-
},
|
|
23
|
-
subscriptions: {
|
|
24
|
-
value: [],
|
|
25
|
-
writable: true
|
|
26
|
-
},
|
|
27
|
-
subscribed: {
|
|
28
|
-
value: {},
|
|
29
|
-
writable: true
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
static getRequestString(method, params) {
|
|
34
|
-
let reqStr = `${method} ${params.channel}`;
|
|
35
|
-
reqStr += params.args ? ` ${params.args}` : "";
|
|
36
|
-
reqStr += params.id ? ` ${params.id}` : "";
|
|
37
|
-
return reqStr.trim();
|
|
38
|
-
}
|
|
39
|
-
connect(url, onOpen = () => {
|
|
40
|
-
}) {
|
|
41
|
-
if (this.websocket && !this.closed) {
|
|
42
|
-
this.websocket.close();
|
|
43
|
-
}
|
|
44
|
-
this.websocket = new WebSocket(url);
|
|
45
|
-
if (!this.open) {
|
|
46
|
-
this.websocket.addEventListener("open", () => {
|
|
47
|
-
onOpen();
|
|
48
|
-
this.subscribePreviousSubscriptions();
|
|
49
|
-
});
|
|
50
|
-
} else {
|
|
51
|
-
onOpen();
|
|
52
|
-
this.subscribePreviousSubscriptions();
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
close() {
|
|
56
|
-
if (this.websocket) {
|
|
57
|
-
this.websocket.onclose = null;
|
|
58
|
-
this.websocket.close();
|
|
59
|
-
this.websocket = null;
|
|
60
|
-
this.messagesOnOpen = [];
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
send(message) {
|
|
64
|
-
if (!this.websocket) {
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
const send = () => {
|
|
68
|
-
this.websocket.send(message);
|
|
69
|
-
};
|
|
70
|
-
if (!this.open) {
|
|
71
|
-
if (!this.messagesOnOpen.includes(message)) {
|
|
72
|
-
this.messagesOnOpen.push(message);
|
|
73
|
-
this.websocket.addEventListener("open", () => {
|
|
74
|
-
this.messagesOnOpen = [];
|
|
75
|
-
send();
|
|
76
|
-
});
|
|
77
|
-
this.websocket.addEventListener("close", () => {
|
|
78
|
-
this.messagesOnOpen = [];
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
} else if (!this.messagesOnOpen.includes(message)) {
|
|
82
|
-
send();
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
listen(params, cb, errorCb) {
|
|
86
|
-
this.unlisten(params, cb);
|
|
87
|
-
const onMessage = (evt) => {
|
|
88
|
-
let data = {};
|
|
89
|
-
try {
|
|
90
|
-
data = JSON.parse(evt.data);
|
|
91
|
-
} catch (err) {
|
|
92
|
-
console.error("WebSocket: unable to parse JSON data", err, evt.data);
|
|
93
|
-
}
|
|
94
|
-
let source = params.channel;
|
|
95
|
-
source += params.args ? ` ${params.args}` : "";
|
|
96
|
-
const contents = data.source === "buffer" ? data.content : [data];
|
|
97
|
-
contents.forEach((content) => {
|
|
98
|
-
if (content?.source === source && (!params.id || params.id === data.client_reference)) {
|
|
99
|
-
cb(content);
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
};
|
|
103
|
-
if (this.websocket) {
|
|
104
|
-
this.websocket.addEventListener("message", onMessage);
|
|
105
|
-
if (errorCb) {
|
|
106
|
-
this.websocket.addEventListener("error", errorCb);
|
|
107
|
-
this.websocket.addEventListener("close", errorCb);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
return { onMessageCb: onMessage, onErrorCb: errorCb };
|
|
111
|
-
}
|
|
112
|
-
unlisten(params, cb) {
|
|
113
|
-
if (!this.websocket) {
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
this.subscriptions.filter((s) => s.params.channel === params.channel && (!cb || s.cb === cb)).forEach(({ onMessageCb, onErrorCb }) => {
|
|
117
|
-
if (this.websocket) {
|
|
118
|
-
this.websocket.removeEventListener("message", onMessageCb);
|
|
119
|
-
if (onErrorCb) {
|
|
120
|
-
this.websocket.removeEventListener("error", onErrorCb);
|
|
121
|
-
this.websocket.removeEventListener("close", onErrorCb);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
get(params, cb, errorCb) {
|
|
127
|
-
const reqStr = WebSocketAPI.getRequestString("GET", params);
|
|
128
|
-
this.send(reqStr);
|
|
129
|
-
this.listen(params, cb, errorCb);
|
|
130
|
-
}
|
|
131
|
-
subscribe(params, cb, errorCb, quiet = false) {
|
|
132
|
-
const { onMessageCb, onErrorCb } = this.listen(params, cb, errorCb);
|
|
133
|
-
const reqStr = WebSocketAPI.getRequestString("", params);
|
|
134
|
-
const index = this.subscriptions.findIndex((subcr) => params.channel === subcr.params.channel && cb === subcr.cb);
|
|
135
|
-
const newSubscr = { params, cb, errorCb, onMessageCb, onErrorCb, quiet };
|
|
136
|
-
if (index > -1) {
|
|
137
|
-
this.subscriptions[index] = newSubscr;
|
|
138
|
-
} else {
|
|
139
|
-
this.subscriptions.push(newSubscr);
|
|
140
|
-
}
|
|
141
|
-
if (!this.subscribed[reqStr]) {
|
|
142
|
-
if (!newSubscr.quiet) {
|
|
143
|
-
this.send(`GET ${reqStr}`);
|
|
144
|
-
this.send(`SUB ${reqStr}`);
|
|
145
|
-
}
|
|
146
|
-
this.subscribed[reqStr] = true;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
unsubscribe(source, cb) {
|
|
150
|
-
const toRemove = this.subscriptions.filter((s) => s.params.channel === source && (!cb || s.cb === cb));
|
|
151
|
-
toRemove.forEach(({ onMessageCb, onErrorCb }) => {
|
|
152
|
-
if (this.websocket) {
|
|
153
|
-
this.websocket.removeEventListener("message", onMessageCb);
|
|
154
|
-
if (onErrorCb) {
|
|
155
|
-
this.websocket.removeEventListener("error", onErrorCb);
|
|
156
|
-
this.websocket.removeEventListener("close", onErrorCb);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
this.subscriptions = this.subscriptions.filter((s) => s.params.channel !== source || cb && s.cb !== cb);
|
|
161
|
-
if (source && this.subscribed[source] && !this.subscriptions.find((s) => s.params.channel === source) && toRemove.find((subscr) => !subscr.quiet)) {
|
|
162
|
-
this.send(`DEL ${source}`);
|
|
163
|
-
this.subscribed[source] = false;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
subscribePreviousSubscriptions() {
|
|
167
|
-
Object.keys(this.subscribed).forEach((key) => {
|
|
168
|
-
this.subscribed[key] = false;
|
|
169
|
-
});
|
|
170
|
-
[...this.subscriptions].forEach((s) => {
|
|
171
|
-
this.subscribe(s.params, s.cb, s.errorCb, s.quiet);
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
export default WebSocketAPI;
|
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
import WS from "jest-websocket-mock";
|
|
2
|
-
import WebSocketAPI from "./WebSocketAPI";
|
|
3
|
-
describe("WebSocketAPI", () => {
|
|
4
|
-
describe("#constructor", () => {
|
|
5
|
-
let server;
|
|
6
|
-
let server2;
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
server = new WS(`ws://foo:1234`);
|
|
9
|
-
server2 = new WS(`ws://foo:12345`);
|
|
10
|
-
});
|
|
11
|
-
afterEach(() => {
|
|
12
|
-
server.close();
|
|
13
|
-
server2.close();
|
|
14
|
-
WS.clean();
|
|
15
|
-
});
|
|
16
|
-
describe("#constructor", () => {
|
|
17
|
-
test("doesn't connect.", async () => {
|
|
18
|
-
const client = new WebSocketAPI();
|
|
19
|
-
expect(client.websocket).toBe();
|
|
20
|
-
expect(client.closed).toBe(false);
|
|
21
|
-
expect(client.closing).toBe(false);
|
|
22
|
-
expect(client.connecting).toBe(false);
|
|
23
|
-
expect(client.open).toBe(false);
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
describe("#close", () => {
|
|
27
|
-
test("should close the websocket and clear some property", async () => {
|
|
28
|
-
const client = new WebSocketAPI();
|
|
29
|
-
const subsc2 = {
|
|
30
|
-
params: "foo",
|
|
31
|
-
cb: () => {
|
|
32
|
-
},
|
|
33
|
-
errorCb: () => {
|
|
34
|
-
},
|
|
35
|
-
quiet: false
|
|
36
|
-
};
|
|
37
|
-
client.subscriptions = [subsc2];
|
|
38
|
-
client.messagesOnOpen = ["GET foo"];
|
|
39
|
-
client.connect(`ws://foo:1234`);
|
|
40
|
-
client.websocket.addEventListener = jest.fn();
|
|
41
|
-
client.websocket.removeEventListener = jest.fn();
|
|
42
|
-
client.websocket.close = jest.fn();
|
|
43
|
-
await server.connected;
|
|
44
|
-
expect(client.websocket).toBeDefined();
|
|
45
|
-
expect(client.messagesOnOpen).toEqual(["GET foo"]);
|
|
46
|
-
client.close();
|
|
47
|
-
expect(client.messagesOnOpen).toEqual([]);
|
|
48
|
-
expect(client.websocket).toBeNull();
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
describe("#connect", () => {
|
|
52
|
-
test("create a new WebSocket.", async () => {
|
|
53
|
-
const client = new WebSocketAPI();
|
|
54
|
-
client.connect(`ws://foo:1234`);
|
|
55
|
-
await server.connected;
|
|
56
|
-
client.send("hello");
|
|
57
|
-
await expect(server).toReceiveMessage("hello");
|
|
58
|
-
expect(server).toHaveReceivedMessages(["hello"]);
|
|
59
|
-
});
|
|
60
|
-
test("close previous connection.", async () => {
|
|
61
|
-
const client = new WebSocketAPI();
|
|
62
|
-
client.connect(`ws://foo:1234`);
|
|
63
|
-
await server.connected;
|
|
64
|
-
expect(client.websocket).toBeDefined();
|
|
65
|
-
const old = client.websocket;
|
|
66
|
-
expect(old.readyState).toBe(WebSocket.OPEN);
|
|
67
|
-
client.connect(`ws://foo:12345`);
|
|
68
|
-
expect(old.readyState).toBe(WebSocket.CLOSING);
|
|
69
|
-
expect(client.websocket.readyState).toBe(WebSocket.CONNECTING);
|
|
70
|
-
});
|
|
71
|
-
test("call onOpen function", async () => {
|
|
72
|
-
const onOpen = jest.fn();
|
|
73
|
-
const client = new WebSocketAPI();
|
|
74
|
-
client.subscribe = jest.fn();
|
|
75
|
-
client.connect(`ws://foo:1234`, onOpen);
|
|
76
|
-
await server.connected;
|
|
77
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
78
|
-
expect(onOpen).toHaveBeenCalledTimes(1);
|
|
79
|
-
});
|
|
80
|
-
test("subscribe previous subscriptions on open (quiet or not)", async () => {
|
|
81
|
-
const client = new WebSocketAPI();
|
|
82
|
-
client.subscribe = jest.fn();
|
|
83
|
-
client.send = jest.fn();
|
|
84
|
-
const subsc = {
|
|
85
|
-
params: "foo",
|
|
86
|
-
cb: () => {
|
|
87
|
-
},
|
|
88
|
-
errorCb: () => {
|
|
89
|
-
},
|
|
90
|
-
quiet: true
|
|
91
|
-
};
|
|
92
|
-
const subsc2 = {
|
|
93
|
-
params: "foo",
|
|
94
|
-
cb: () => {
|
|
95
|
-
},
|
|
96
|
-
errorCb: () => {
|
|
97
|
-
},
|
|
98
|
-
quiet: false
|
|
99
|
-
};
|
|
100
|
-
client.subscriptions = [subsc, subsc2];
|
|
101
|
-
client.connect(`ws://foo:1234`);
|
|
102
|
-
await server.connected;
|
|
103
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
104
|
-
expect(client.subscribe).toHaveBeenCalledTimes(2);
|
|
105
|
-
client.subscribe.mockReset();
|
|
106
|
-
client.connect(`ws://foo:12345`);
|
|
107
|
-
await server2.connected;
|
|
108
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
109
|
-
expect(client.subscribe).toHaveBeenCalledTimes(2);
|
|
110
|
-
expect(client.subscribe).toHaveBeenCalledWith(subsc.params, subsc.cb, subsc.errorCb, subsc.quiet);
|
|
111
|
-
});
|
|
112
|
-
test("send GET and SUB for not quiet previous subscriptions", async () => {
|
|
113
|
-
const client = new WebSocketAPI();
|
|
114
|
-
client.send = jest.fn();
|
|
115
|
-
const subsc = {
|
|
116
|
-
params: { channel: "foo" },
|
|
117
|
-
cb: () => {
|
|
118
|
-
},
|
|
119
|
-
errorCb: () => {
|
|
120
|
-
},
|
|
121
|
-
quiet: false
|
|
122
|
-
};
|
|
123
|
-
client.subscriptions = [subsc];
|
|
124
|
-
client.connect(`ws://foo:1234`);
|
|
125
|
-
client.websocket.addEventListener = jest.fn();
|
|
126
|
-
client.websocket.removeEventListener = jest.fn();
|
|
127
|
-
await server.connected;
|
|
128
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
129
|
-
expect(client.send).toHaveBeenCalledTimes(2);
|
|
130
|
-
expect(client.send.mock.calls[0]).toEqual(["GET foo"]);
|
|
131
|
-
expect(client.send.mock.calls[1]).toEqual(["SUB foo"]);
|
|
132
|
-
client.send.mockReset();
|
|
133
|
-
client.connect(`ws://foo:12345`);
|
|
134
|
-
client.websocket.addEventListener = jest.fn();
|
|
135
|
-
client.websocket.removeEventListener = jest.fn();
|
|
136
|
-
await server2.connected;
|
|
137
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
138
|
-
expect(client.send).toHaveBeenCalledTimes(2);
|
|
139
|
-
expect(client.send.mock.calls[0]).toEqual(["GET foo"]);
|
|
140
|
-
expect(client.send.mock.calls[1]).toEqual(["SUB foo"]);
|
|
141
|
-
});
|
|
142
|
-
test("doesn send GET and SUB for quiet previous subscriptions", async () => {
|
|
143
|
-
const client = new WebSocketAPI();
|
|
144
|
-
client.send = jest.fn();
|
|
145
|
-
const subsc = {
|
|
146
|
-
params: { channel: "foo" },
|
|
147
|
-
cb: () => {
|
|
148
|
-
},
|
|
149
|
-
errorCb: () => {
|
|
150
|
-
},
|
|
151
|
-
quiet: true
|
|
152
|
-
};
|
|
153
|
-
client.subscriptions = [subsc];
|
|
154
|
-
client.connect(`ws://foo:1234`);
|
|
155
|
-
client.websocket.addEventListener = jest.fn();
|
|
156
|
-
client.websocket.removeEventListener = jest.fn();
|
|
157
|
-
await server.connected;
|
|
158
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
159
|
-
expect(client.send).toHaveBeenCalledTimes(0);
|
|
160
|
-
client.send.mockReset();
|
|
161
|
-
client.connect(`ws://foo:12345`);
|
|
162
|
-
client.websocket.addEventListener = jest.fn();
|
|
163
|
-
client.websocket.removeEventListener = jest.fn();
|
|
164
|
-
await server2.connected;
|
|
165
|
-
expect(client.websocket.readyState).toBe(WebSocket.OPEN);
|
|
166
|
-
expect(client.send).toHaveBeenCalledTimes(0);
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
describe("#subscribe", () => {
|
|
170
|
-
test("adds subscription to subscriptions array", async () => {
|
|
171
|
-
const client = new WebSocketAPI();
|
|
172
|
-
client.connect(`ws://foo:1234`);
|
|
173
|
-
await server.connected;
|
|
174
|
-
const params = { channel: "bar", args: ["baz"], id: "id" };
|
|
175
|
-
const cb = jest.fn();
|
|
176
|
-
const errorCb = jest.fn();
|
|
177
|
-
client.subscribe(params, cb, errorCb);
|
|
178
|
-
expect(client.subscriptions.length).toBe(1);
|
|
179
|
-
expect(client.subscriptions[0].params).toBe(params);
|
|
180
|
-
expect(client.subscriptions[0].cb).toBe(cb);
|
|
181
|
-
expect(client.subscriptions[0].errorCb).toBe(errorCb);
|
|
182
|
-
expect(client.subscriptions[0].quiet).toBe(false);
|
|
183
|
-
const obj = { source: "bar baz", client_reference: "id" };
|
|
184
|
-
server.send(JSON.stringify(obj));
|
|
185
|
-
expect(cb).toHaveBeenCalledTimes(1);
|
|
186
|
-
expect(cb).toHaveBeenCalledWith(obj);
|
|
187
|
-
});
|
|
188
|
-
test("doesn't duplicate subscriptions", async () => {
|
|
189
|
-
const client = new WebSocketAPI();
|
|
190
|
-
client.connect(`ws://foo:1234`);
|
|
191
|
-
await server.connected;
|
|
192
|
-
const params = { channel: "bar", args: ["baz"], id: "id" };
|
|
193
|
-
const cb = jest.fn();
|
|
194
|
-
const errorCb = jest.fn();
|
|
195
|
-
client.subscribe(params, cb, errorCb, true);
|
|
196
|
-
client.subscribe(params, cb, errorCb, true);
|
|
197
|
-
expect(client.subscriptions.length).toBe(1);
|
|
198
|
-
const obj = { source: "bar baz", client_reference: "id" };
|
|
199
|
-
server.send(JSON.stringify(obj));
|
|
200
|
-
expect(cb).toHaveBeenCalledTimes(1);
|
|
201
|
-
expect(cb).toHaveBeenCalledWith(obj);
|
|
202
|
-
});
|
|
203
|
-
test("send GET and SUB requests.", async () => {
|
|
204
|
-
const client = new WebSocketAPI();
|
|
205
|
-
client.connect(`ws://foo:1234`);
|
|
206
|
-
client.send = jest.fn();
|
|
207
|
-
const params = { channel: "bar", args: ["baz"], id: "id" };
|
|
208
|
-
const cb = jest.fn();
|
|
209
|
-
const errorCb = jest.fn();
|
|
210
|
-
client.subscribe(params, cb, errorCb);
|
|
211
|
-
expect(client.send).toHaveBeenCalledTimes(2);
|
|
212
|
-
expect(client.send).toHaveBeenCalledWith("GET bar baz id");
|
|
213
|
-
expect(client.send).toHaveBeenCalledWith("SUB bar baz id");
|
|
214
|
-
client.send.mockRestore();
|
|
215
|
-
});
|
|
216
|
-
test("should register callback without sending GET and SUB requests (quiet=true).", async () => {
|
|
217
|
-
const client = new WebSocketAPI();
|
|
218
|
-
client.connect(`ws://foo:1234`);
|
|
219
|
-
await server.connected;
|
|
220
|
-
const params = { channel: "bar", args: ["baz"], id: "id" };
|
|
221
|
-
const cb = jest.fn();
|
|
222
|
-
const errorCb = jest.fn();
|
|
223
|
-
client.send = jest.fn();
|
|
224
|
-
client.subscribe(params, cb, errorCb, true);
|
|
225
|
-
expect(client.subscriptions.length).toBe(1);
|
|
226
|
-
expect(client.subscriptions[0].params).toBe(params);
|
|
227
|
-
expect(client.subscriptions[0].cb).toBe(cb);
|
|
228
|
-
expect(client.subscriptions[0].errorCb).toBe(errorCb);
|
|
229
|
-
expect(client.subscriptions[0].quiet).toBe(true);
|
|
230
|
-
expect(client.send).toBeCalledTimes(0);
|
|
231
|
-
client.send.mockRestore();
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
describe("#unsubscribe", () => {
|
|
235
|
-
test("should only unsubscribe the subscription using the good cb", async () => {
|
|
236
|
-
const client = new WebSocketAPI();
|
|
237
|
-
client.connect(`ws://foo:1234`);
|
|
238
|
-
await server.connected;
|
|
239
|
-
const params = { channel: "foo", id: "id" };
|
|
240
|
-
const cb = jest.fn();
|
|
241
|
-
const cb2 = jest.fn();
|
|
242
|
-
client.subscribe(params, cb);
|
|
243
|
-
client.subscribe(params, cb2);
|
|
244
|
-
expect(client.subscriptions.length).toBe(2);
|
|
245
|
-
expect(client.subscriptions[0].params).toBe(params);
|
|
246
|
-
expect(client.subscriptions[0].cb).toBe(cb);
|
|
247
|
-
expect(client.subscriptions[1].params).toBe(params);
|
|
248
|
-
expect(client.subscriptions[1].cb).toBe(cb2);
|
|
249
|
-
client.unsubscribe("foo", cb);
|
|
250
|
-
expect(client.subscriptions.length).toBe(1);
|
|
251
|
-
expect(cb).toHaveBeenCalledTimes(0);
|
|
252
|
-
expect(cb2).toHaveBeenCalledTimes(0);
|
|
253
|
-
const obj = { source: "foo", client_reference: "id" };
|
|
254
|
-
server.send(JSON.stringify(obj));
|
|
255
|
-
expect(cb2).toHaveBeenCalledTimes(1);
|
|
256
|
-
expect(cb).toHaveBeenCalledTimes(0);
|
|
257
|
-
});
|
|
258
|
-
test("should unsubscribe all subscriptions related to a channel", () => {
|
|
259
|
-
const client = new WebSocketAPI();
|
|
260
|
-
client.connect(`ws://foo:1234`);
|
|
261
|
-
client.websocket.removeEventListener = jest.fn();
|
|
262
|
-
client.websocket.addEventListener = jest.fn();
|
|
263
|
-
const params = { channel: "foo" };
|
|
264
|
-
const params2 = { channel: "bar" };
|
|
265
|
-
const cb = jest.fn();
|
|
266
|
-
const cb2 = jest.fn();
|
|
267
|
-
client.subscribe(params, cb);
|
|
268
|
-
client.subscribe(params, cb);
|
|
269
|
-
client.subscribe(params, cb);
|
|
270
|
-
client.subscribe(params, cb2);
|
|
271
|
-
client.subscribe(params2, cb2);
|
|
272
|
-
expect(client.subscriptions.length).toBe(3);
|
|
273
|
-
expect(client.websocket.removeEventListener).toBeCalledTimes(2);
|
|
274
|
-
expect(client.websocket.addEventListener.mock.calls.filter((c) => c[0] === "message").length).toBe(5);
|
|
275
|
-
client.unsubscribe("foo");
|
|
276
|
-
expect(client.subscriptions.length).toBe(1);
|
|
277
|
-
expect(client.subscriptions[0].params).toBe(params2);
|
|
278
|
-
expect(client.subscriptions[0].cb).toBe(cb2);
|
|
279
|
-
});
|
|
280
|
-
test("send DEL when there is no more unquiet subscriptions on the channel", async () => {
|
|
281
|
-
const client = new WebSocketAPI();
|
|
282
|
-
client.connect(`ws://foo:1234`);
|
|
283
|
-
await server.connected;
|
|
284
|
-
client.send = jest.fn();
|
|
285
|
-
client.websocket.removeEventListener = jest.fn();
|
|
286
|
-
client.websocket.addEventListener = jest.fn();
|
|
287
|
-
const params = { channel: "foo" };
|
|
288
|
-
const cb = jest.fn();
|
|
289
|
-
client.subscribe(params, cb);
|
|
290
|
-
expect(client.send).toHaveBeenCalledWith("GET foo");
|
|
291
|
-
expect(client.send).toHaveBeenCalledWith("SUB foo");
|
|
292
|
-
client.unsubscribe("foo");
|
|
293
|
-
expect(client.send).toHaveBeenCalledWith("DEL foo");
|
|
294
|
-
});
|
|
295
|
-
test("doesn't send DEL when we unsubscribe a quiet channel", () => {
|
|
296
|
-
const client = new WebSocketAPI();
|
|
297
|
-
client.connect(`ws://foo:1234`);
|
|
298
|
-
client.send = jest.fn();
|
|
299
|
-
client.websocket.removeEventListener = jest.fn();
|
|
300
|
-
client.websocket.addEventListener = jest.fn();
|
|
301
|
-
const params = { channel: "foo" };
|
|
302
|
-
const cb = jest.fn();
|
|
303
|
-
client.subscribe(params, cb, null, true);
|
|
304
|
-
expect(cb).toHaveBeenCalledTimes(0);
|
|
305
|
-
client.unsubscribe("foo");
|
|
306
|
-
expect(cb).toHaveBeenCalledTimes(0);
|
|
307
|
-
client.send.mockRestore();
|
|
308
|
-
});
|
|
309
|
-
});
|
|
310
|
-
});
|
|
311
|
-
});
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import BaseObject from "ol/Object";
|
|
2
|
-
class Control extends BaseObject {
|
|
3
|
-
constructor(options = {}) {
|
|
4
|
-
super(options);
|
|
5
|
-
this.defineProperties(options);
|
|
6
|
-
const { active } = {
|
|
7
|
-
active: options.active !== false,
|
|
8
|
-
...options
|
|
9
|
-
};
|
|
10
|
-
this.active = active;
|
|
11
|
-
}
|
|
12
|
-
defineProperties(options) {
|
|
13
|
-
const { target, element, render } = {
|
|
14
|
-
...options
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperties(this, {
|
|
17
|
-
active: {
|
|
18
|
-
get: () => this.get("active"),
|
|
19
|
-
set: (newActive) => {
|
|
20
|
-
this.set("active", newActive);
|
|
21
|
-
if (newActive) {
|
|
22
|
-
this.activate();
|
|
23
|
-
} else {
|
|
24
|
-
this.deactivate();
|
|
25
|
-
}
|
|
26
|
-
this.render();
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
map: {
|
|
30
|
-
get: () => this.get("map"),
|
|
31
|
-
set: (map) => {
|
|
32
|
-
if (this.map && this.element && this.element.parentNode) {
|
|
33
|
-
this.element.parentNode.removeChild(this.element);
|
|
34
|
-
}
|
|
35
|
-
this.deactivate();
|
|
36
|
-
this.set("map", map);
|
|
37
|
-
if (this.map) {
|
|
38
|
-
const targett = this.target || this.map.getTargetElement && this.map.getTargetElement() || this.map.getContainer && this.map.getContainer();
|
|
39
|
-
if (!this.element) {
|
|
40
|
-
this.createDefaultElement();
|
|
41
|
-
}
|
|
42
|
-
if (this.element) {
|
|
43
|
-
targett.appendChild(this.element);
|
|
44
|
-
}
|
|
45
|
-
if (this.active) {
|
|
46
|
-
this.activate();
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
this.render();
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
target: {
|
|
53
|
-
value: target
|
|
54
|
-
},
|
|
55
|
-
element: {
|
|
56
|
-
value: element,
|
|
57
|
-
writable: true
|
|
58
|
-
},
|
|
59
|
-
render: {
|
|
60
|
-
value: render || this.render,
|
|
61
|
-
writable: true
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
attachToMap(map) {
|
|
66
|
-
this.map = map;
|
|
67
|
-
}
|
|
68
|
-
detachFromMap() {
|
|
69
|
-
this.map = null;
|
|
70
|
-
}
|
|
71
|
-
activate() {
|
|
72
|
-
this.deactivate();
|
|
73
|
-
}
|
|
74
|
-
deactivate() {
|
|
75
|
-
}
|
|
76
|
-
render() {
|
|
77
|
-
}
|
|
78
|
-
createDefaultElement() {
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
export default Control;
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import Control from "./Control";
|
|
2
|
-
describe("Control", () => {
|
|
3
|
-
test("should be activated by default", () => {
|
|
4
|
-
const control = new Control();
|
|
5
|
-
expect(control.active).toBe(true);
|
|
6
|
-
});
|
|
7
|
-
test("should not be activated if set to false in the options", () => {
|
|
8
|
-
const control = new Control({ active: false });
|
|
9
|
-
expect(control.active).toBe(false);
|
|
10
|
-
});
|
|
11
|
-
test("should call activate/deactivate when active is set to true/false", () => {
|
|
12
|
-
const control = new Control();
|
|
13
|
-
const spy1 = jest.spyOn(control, "activate");
|
|
14
|
-
const spy2 = jest.spyOn(control, "deactivate");
|
|
15
|
-
const spy3 = jest.spyOn(control, "render");
|
|
16
|
-
control.active = false;
|
|
17
|
-
expect(spy1).toBeCalledTimes(0);
|
|
18
|
-
expect(spy2).toBeCalledTimes(1);
|
|
19
|
-
expect(spy3).toBeCalledTimes(1);
|
|
20
|
-
control.active = true;
|
|
21
|
-
expect(spy1).toBeCalledTimes(1);
|
|
22
|
-
expect(spy2).toBeCalledTimes(2);
|
|
23
|
-
expect(spy3).toBeCalledTimes(2);
|
|
24
|
-
});
|
|
25
|
-
test("should append/remove the element to the map container when map is set", () => {
|
|
26
|
-
const element = document.createElement("div");
|
|
27
|
-
const target = document.createElement("div");
|
|
28
|
-
const control = new Control({
|
|
29
|
-
element
|
|
30
|
-
});
|
|
31
|
-
const spy1 = jest.spyOn(control, "activate");
|
|
32
|
-
const spy2 = jest.spyOn(control, "deactivate");
|
|
33
|
-
const spy3 = jest.spyOn(control, "render");
|
|
34
|
-
control.map = {
|
|
35
|
-
getContainer() {
|
|
36
|
-
return target;
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
expect(target.childNodes[0]).toBe(element);
|
|
40
|
-
expect(spy1).toBeCalledTimes(1);
|
|
41
|
-
expect(spy2).toBeCalledTimes(2);
|
|
42
|
-
expect(spy3).toBeCalledTimes(1);
|
|
43
|
-
control.detachFromMap();
|
|
44
|
-
expect(target.childNodes[0]).toBe();
|
|
45
|
-
expect(spy1).toBeCalledTimes(1);
|
|
46
|
-
expect(spy2).toBeCalledTimes(3);
|
|
47
|
-
expect(spy3).toBeCalledTimes(2);
|
|
48
|
-
});
|
|
49
|
-
test("should append/remove the element to the target property when map is set", () => {
|
|
50
|
-
const element = document.createElement("div");
|
|
51
|
-
const target = document.createElement("div");
|
|
52
|
-
const control = new Control({
|
|
53
|
-
target,
|
|
54
|
-
element
|
|
55
|
-
});
|
|
56
|
-
const spy1 = jest.spyOn(control, "activate");
|
|
57
|
-
const spy2 = jest.spyOn(control, "deactivate");
|
|
58
|
-
const spy3 = jest.spyOn(control, "render");
|
|
59
|
-
control.map = {};
|
|
60
|
-
expect(target.childNodes[0]).toBe(element);
|
|
61
|
-
expect(spy1).toBeCalledTimes(1);
|
|
62
|
-
expect(spy2).toBeCalledTimes(2);
|
|
63
|
-
expect(spy3).toBeCalledTimes(1);
|
|
64
|
-
control.detachFromMap();
|
|
65
|
-
expect(target.childNodes[0]).toBe();
|
|
66
|
-
expect(spy1).toBeCalledTimes(1);
|
|
67
|
-
expect(spy2).toBeCalledTimes(3);
|
|
68
|
-
expect(spy3).toBeCalledTimes(2);
|
|
69
|
-
});
|
|
70
|
-
test("set a custom render method", () => {
|
|
71
|
-
const spy = jest.fn();
|
|
72
|
-
const control = new Control({
|
|
73
|
-
render: spy
|
|
74
|
-
});
|
|
75
|
-
expect(spy).toBeCalledTimes(1);
|
|
76
|
-
});
|
|
77
|
-
test("pass function params to custom render method", () => {
|
|
78
|
-
const spy = jest.fn();
|
|
79
|
-
const control = new Control({
|
|
80
|
-
render: spy
|
|
81
|
-
});
|
|
82
|
-
control.render("foo", "bar");
|
|
83
|
-
expect(spy).toBeCalledTimes(2);
|
|
84
|
-
expect(spy.mock.calls[1][0]).toBe("foo");
|
|
85
|
-
expect(spy.mock.calls[1][1]).toBe("bar");
|
|
86
|
-
});
|
|
87
|
-
});
|