mobility-toolbox-js 2.0.0 → 2.0.1-beta.13

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.
Files changed (102) hide show
  1. package/api/RoutingAPI.js +15 -0
  2. package/api/RoutingAPI.test.js +25 -0
  3. package/api/StopsAPI.js +12 -0
  4. package/api/StopsAPI.test.js +22 -0
  5. package/api/TralisAPI.js +359 -0
  6. package/api/TralisAPI.test.js +67 -0
  7. package/api/{tralis/TralisAPIUtils.js → TralisAPIUtils.js} +2 -32
  8. package/api/index.js +3 -3
  9. package/{ol/README.md → api/typedefs.js} +0 -0
  10. package/common/Tracker.js +14 -118
  11. package/common/api/HttpAPI.js +30 -0
  12. package/common/api/HttpAPI.test.js +50 -0
  13. package/common/api/WebSocketAPI.js +175 -0
  14. package/{api/tralis/WebSocketConnector.test.js → common/api/WebSocketAPI.test.js} +100 -145
  15. package/common/controls/Control.js +26 -91
  16. package/common/controls/Control.test.js +32 -43
  17. package/common/index.js +4 -0
  18. package/common/layers/Layer.js +53 -244
  19. package/common/layers/Layer.test.js +185 -244
  20. package/common/mixins/CopyrightMixin.js +20 -44
  21. package/common/mixins/SearchMixin.js +100 -166
  22. package/common/mixins/TralisLayerMixin.js +443 -894
  23. package/common/styles/index.js +4 -4
  24. package/common/styles/trackerDefaultStyle.js +39 -175
  25. package/common/styles/trackerDelayStyle.js +2 -11
  26. package/common/styles/trackerSimpleStyle.js +4 -8
  27. package/common/trackerConfig.js +61 -99
  28. package/common/trackerConfig.test.js +15 -17
  29. package/common/typedefs.js +0 -23
  30. package/common/utils/createTrackerFilters.js +10 -41
  31. package/common/utils/createTrackerFilters.test.js +40 -56
  32. package/common/utils/getMapboxMapCopyrights.js +3 -16
  33. package/common/utils/getMapboxMapCopyrights.test.js +32 -39
  34. package/common/utils/getMapboxStyleUrl.js +3 -13
  35. package/common/utils/getVehiclePosition.js +3 -33
  36. package/common/utils/index.js +5 -6
  37. package/common/utils/removeDuplicate.js +3 -17
  38. package/common/utils/removeDuplicate.test.js +17 -20
  39. package/common/utils/sortByDelay.js +2 -7
  40. package/common/utils/timeUtils.js +8 -32
  41. package/common/utils/timeUtils.test.js +7 -13
  42. package/index.js +8 -2
  43. package/mapbox/controls/CopyrightControl.js +9 -38
  44. package/mapbox/controls/index.js +1 -0
  45. package/mapbox/index.js +4 -3
  46. package/mapbox/layers/Layer.js +15 -76
  47. package/mapbox/layers/Layer.test.js +81 -101
  48. package/mapbox/layers/TralisLayer.js +46 -193
  49. package/mapbox/layers/TralisLayer.test.js +12 -14
  50. package/mapbox/layers/index.js +2 -0
  51. package/mapbox/utils.js +7 -21
  52. package/mbt.js +50444 -0
  53. package/mbt.js.map +7 -0
  54. package/mbt.min.js +1005 -0
  55. package/mbt.min.js.map +7 -0
  56. package/ol/controls/CopyrightControl.js +8 -46
  57. package/ol/controls/CopyrightControl.test.js +75 -121
  58. package/ol/controls/RoutingControl.js +167 -532
  59. package/ol/controls/RoutingControl.test.js +99 -164
  60. package/ol/controls/StopFinderControl.js +3 -31
  61. package/ol/controls/StopFinderControl.test.js +18 -29
  62. package/ol/controls/index.js +3 -0
  63. package/ol/index.js +5 -13
  64. package/ol/layers/Layer.js +23 -128
  65. package/ol/layers/Layer.test.js +79 -102
  66. package/ol/layers/MapboxLayer.js +62 -237
  67. package/ol/layers/MapboxLayer.test.js +58 -84
  68. package/ol/layers/MapboxStyleLayer.js +38 -268
  69. package/ol/layers/MapboxStyleLayer.test.js +97 -128
  70. package/ol/layers/MaplibreLayer.js +46 -187
  71. package/ol/layers/RoutingLayer.js +21 -51
  72. package/ol/layers/RoutingLayer.test.js +15 -24
  73. package/ol/layers/TralisLayer.js +102 -276
  74. package/ol/layers/TralisLayer.test.js +32 -50
  75. package/ol/layers/VectorLayer.js +3 -24
  76. package/ol/layers/VectorLayer.test.js +34 -45
  77. package/ol/layers/WMSLayer.js +15 -57
  78. package/ol/layers/WMSLayer.test.js +35 -43
  79. package/ol/layers/index.js +8 -0
  80. package/ol/styles/fullTrajectoryDelayStyle.js +11 -15
  81. package/ol/styles/fullTrajectoryStyle.js +17 -25
  82. package/ol/styles/index.js +2 -2
  83. package/package.json +35 -62
  84. package/api/routing/RoutingAPI.js +0 -44
  85. package/api/routing/RoutingAPI.test.js +0 -41
  86. package/api/stops/StopsAPI.js +0 -41
  87. package/api/stops/StopsAPI.test.js +0 -34
  88. package/api/tralis/TralisAPI.js +0 -731
  89. package/api/tralis/TralisAPI.test.js +0 -75
  90. package/api/tralis/WebSocketConnector.js +0 -338
  91. package/api/tralis/typedefs.js +0 -81
  92. package/common/api/api.js +0 -64
  93. package/common/api/api.test.js +0 -68
  94. package/index.js.map +0 -1
  95. package/module.js +0 -23
  96. package/ol/controls/snapshots/RoutingControlRouteGen10.json +0 -58
  97. package/ol/controls/snapshots/RoutingControlRouteGen100.json +0 -292
  98. package/ol/controls/snapshots/RoutingControlRouteGen30.json +0 -69
  99. package/ol/controls/snapshots/RoutingControlRouteGen5.json +0 -58
  100. package/ol/controls/snapshots/RoutingControlRouteOSM.json +0 -759
  101. package/ol/controls/snapshots/RoutingControlStation1.json +0 -60
  102. package/ol/controls/snapshots/RoutingControlStation2.json +0 -49
@@ -1,25 +1,21 @@
1
- import WS from 'jest-websocket-mock';
2
- import Connector from './WebSocketConnector';
3
-
4
- describe('WebSocketConnector', () => {
5
- describe('#constructor', () => {
1
+ import WS from "jest-websocket-mock";
2
+ import WebSocketAPI from "./WebSocketAPI";
3
+ describe("WebSocketAPI", () => {
4
+ describe("#constructor", () => {
6
5
  let server;
7
6
  let server2;
8
-
9
7
  beforeEach(() => {
10
8
  server = new WS(`ws://foo:1234`);
11
9
  server2 = new WS(`ws://foo:12345`);
12
10
  });
13
-
14
11
  afterEach(() => {
15
12
  server.close();
16
13
  server2.close();
17
14
  WS.clean();
18
15
  });
19
-
20
- describe('#constructor', () => {
16
+ describe("#constructor", () => {
21
17
  test("doesn't connect.", async () => {
22
- const client = new Connector();
18
+ const client = new WebSocketAPI();
23
19
  expect(client.websocket).toBe();
24
20
  expect(client.closed).toBe(false);
25
21
  expect(client.closing).toBe(false);
@@ -27,44 +23,42 @@ describe('WebSocketConnector', () => {
27
23
  expect(client.open).toBe(false);
28
24
  });
29
25
  });
30
-
31
- describe('#close', () => {
32
- test('should close the websocket and clear some property', async () => {
33
- // eslint-disable-next-line no-unused-vars
34
- const client = new Connector();
26
+ describe("#close", () => {
27
+ test("should close the websocket and clear some property", async () => {
28
+ const client = new WebSocketAPI();
35
29
  const subsc2 = {
36
- params: 'foo',
37
- cb: () => {},
38
- errorCb: () => {},
39
- quiet: false,
30
+ params: "foo",
31
+ cb: () => {
32
+ },
33
+ errorCb: () => {
34
+ },
35
+ quiet: false
40
36
  };
41
37
  client.subscriptions = [subsc2];
42
- client.messagesOnOpen = ['GET foo'];
38
+ client.messagesOnOpen = ["GET foo"];
43
39
  client.connect(`ws://foo:1234`);
44
40
  client.websocket.addEventListener = jest.fn();
45
41
  client.websocket.removeEventListener = jest.fn();
46
42
  client.websocket.close = jest.fn();
47
43
  await server.connected;
48
44
  expect(client.websocket).toBeDefined();
49
- expect(client.messagesOnOpen).toEqual(['GET foo']);
45
+ expect(client.messagesOnOpen).toEqual(["GET foo"]);
50
46
  client.close();
51
47
  expect(client.messagesOnOpen).toEqual([]);
52
48
  expect(client.websocket).toBeNull();
53
49
  });
54
50
  });
55
-
56
- describe('#connect', () => {
57
- test('create a new WebSocket.', async () => {
58
- const client = new Connector();
51
+ describe("#connect", () => {
52
+ test("create a new WebSocket.", async () => {
53
+ const client = new WebSocketAPI();
59
54
  client.connect(`ws://foo:1234`);
60
55
  await server.connected;
61
- client.send('hello');
62
- await expect(server).toReceiveMessage('hello');
63
- expect(server).toHaveReceivedMessages(['hello']);
56
+ client.send("hello");
57
+ await expect(server).toReceiveMessage("hello");
58
+ expect(server).toHaveReceivedMessages(["hello"]);
64
59
  });
65
-
66
- test('close previous connection.', async () => {
67
- const client = new Connector();
60
+ test("close previous connection.", async () => {
61
+ const client = new WebSocketAPI();
68
62
  client.connect(`ws://foo:1234`);
69
63
  await server.connected;
70
64
  expect(client.websocket).toBeDefined();
@@ -74,96 +68,89 @@ describe('WebSocketConnector', () => {
74
68
  expect(old.readyState).toBe(WebSocket.CLOSING);
75
69
  expect(client.websocket.readyState).toBe(WebSocket.CONNECTING);
76
70
  });
77
-
78
- test('call onOpen function', async () => {
71
+ test("call onOpen function", async () => {
79
72
  const onOpen = jest.fn();
80
- const client = new Connector();
73
+ const client = new WebSocketAPI();
81
74
  client.subscribe = jest.fn();
82
75
  client.connect(`ws://foo:1234`, onOpen);
83
76
  await server.connected;
84
77
  expect(client.websocket.readyState).toBe(WebSocket.OPEN);
85
78
  expect(onOpen).toHaveBeenCalledTimes(1);
86
79
  });
87
-
88
- test('subscribe previous subscriptions on open (quiet or not)', async () => {
89
- const client = new Connector();
80
+ test("subscribe previous subscriptions on open (quiet or not)", async () => {
81
+ const client = new WebSocketAPI();
90
82
  client.subscribe = jest.fn();
91
83
  client.send = jest.fn();
92
84
  const subsc = {
93
- params: 'foo',
94
- cb: () => {},
95
- errorCb: () => {},
96
- quiet: true,
85
+ params: "foo",
86
+ cb: () => {
87
+ },
88
+ errorCb: () => {
89
+ },
90
+ quiet: true
97
91
  };
98
92
  const subsc2 = {
99
- params: 'foo',
100
- cb: () => {},
101
- errorCb: () => {},
102
- quiet: false,
93
+ params: "foo",
94
+ cb: () => {
95
+ },
96
+ errorCb: () => {
97
+ },
98
+ quiet: false
103
99
  };
104
100
  client.subscriptions = [subsc, subsc2];
105
-
106
101
  client.connect(`ws://foo:1234`);
107
102
  await server.connected;
108
103
  expect(client.websocket.readyState).toBe(WebSocket.OPEN);
109
104
  expect(client.subscribe).toHaveBeenCalledTimes(2);
110
105
  client.subscribe.mockReset();
111
-
112
106
  client.connect(`ws://foo:12345`);
113
107
  await server2.connected;
114
108
  expect(client.websocket.readyState).toBe(WebSocket.OPEN);
115
109
  expect(client.subscribe).toHaveBeenCalledTimes(2);
116
- expect(client.subscribe).toHaveBeenCalledWith(
117
- subsc.params,
118
- subsc.cb,
119
- subsc.errorCb,
120
- subsc.quiet,
121
- );
110
+ expect(client.subscribe).toHaveBeenCalledWith(subsc.params, subsc.cb, subsc.errorCb, subsc.quiet);
122
111
  });
123
-
124
- test('send GET and SUB for not quiet previous subscriptions', async () => {
125
- const client = new Connector();
112
+ test("send GET and SUB for not quiet previous subscriptions", async () => {
113
+ const client = new WebSocketAPI();
126
114
  client.send = jest.fn();
127
115
  const subsc = {
128
- params: { channel: 'foo' },
129
- cb: () => {},
130
- errorCb: () => {},
131
- quiet: false,
116
+ params: { channel: "foo" },
117
+ cb: () => {
118
+ },
119
+ errorCb: () => {
120
+ },
121
+ quiet: false
132
122
  };
133
123
  client.subscriptions = [subsc];
134
-
135
124
  client.connect(`ws://foo:1234`);
136
125
  client.websocket.addEventListener = jest.fn();
137
126
  client.websocket.removeEventListener = jest.fn();
138
127
  await server.connected;
139
128
  expect(client.websocket.readyState).toBe(WebSocket.OPEN);
140
129
  expect(client.send).toHaveBeenCalledTimes(2);
141
- expect(client.send.mock.calls[0]).toEqual(['GET foo']);
142
- expect(client.send.mock.calls[1]).toEqual(['SUB foo']);
130
+ expect(client.send.mock.calls[0]).toEqual(["GET foo"]);
131
+ expect(client.send.mock.calls[1]).toEqual(["SUB foo"]);
143
132
  client.send.mockReset();
144
-
145
133
  client.connect(`ws://foo:12345`);
146
134
  client.websocket.addEventListener = jest.fn();
147
135
  client.websocket.removeEventListener = jest.fn();
148
136
  await server2.connected;
149
137
  expect(client.websocket.readyState).toBe(WebSocket.OPEN);
150
- // not quiet subscriptions will send GET and SUB requests.
151
138
  expect(client.send).toHaveBeenCalledTimes(2);
152
- expect(client.send.mock.calls[0]).toEqual(['GET foo']);
153
- expect(client.send.mock.calls[1]).toEqual(['SUB foo']);
139
+ expect(client.send.mock.calls[0]).toEqual(["GET foo"]);
140
+ expect(client.send.mock.calls[1]).toEqual(["SUB foo"]);
154
141
  });
155
-
156
- test('doesn\t send GET and SUB for quiet previous subscriptions', async () => {
157
- const client = new Connector();
142
+ test("doesn send GET and SUB for quiet previous subscriptions", async () => {
143
+ const client = new WebSocketAPI();
158
144
  client.send = jest.fn();
159
145
  const subsc = {
160
- params: { channel: 'foo' },
161
- cb: () => {},
162
- errorCb: () => {},
163
- quiet: true,
146
+ params: { channel: "foo" },
147
+ cb: () => {
148
+ },
149
+ errorCb: () => {
150
+ },
151
+ quiet: true
164
152
  };
165
153
  client.subscriptions = [subsc];
166
-
167
154
  client.connect(`ws://foo:1234`);
168
155
  client.websocket.addEventListener = jest.fn();
169
156
  client.websocket.removeEventListener = jest.fn();
@@ -171,24 +158,20 @@ describe('WebSocketConnector', () => {
171
158
  expect(client.websocket.readyState).toBe(WebSocket.OPEN);
172
159
  expect(client.send).toHaveBeenCalledTimes(0);
173
160
  client.send.mockReset();
174
-
175
161
  client.connect(`ws://foo:12345`);
176
162
  client.websocket.addEventListener = jest.fn();
177
163
  client.websocket.removeEventListener = jest.fn();
178
164
  await server2.connected;
179
165
  expect(client.websocket.readyState).toBe(WebSocket.OPEN);
180
- // not quiet subscriptions will send GET and SUB requests.
181
166
  expect(client.send).toHaveBeenCalledTimes(0);
182
167
  });
183
168
  });
184
-
185
- describe('#subscribe', () => {
186
- test('adds subscription to subscriptions array', async () => {
187
- // eslint-disable-next-line no-unused-vars
188
- const client = new Connector();
169
+ describe("#subscribe", () => {
170
+ test("adds subscription to subscriptions array", async () => {
171
+ const client = new WebSocketAPI();
189
172
  client.connect(`ws://foo:1234`);
190
173
  await server.connected;
191
- const params = { channel: 'bar', args: ['baz'], id: 'id' };
174
+ const params = { channel: "bar", args: ["baz"], id: "id" };
192
175
  const cb = jest.fn();
193
176
  const errorCb = jest.fn();
194
177
  client.subscribe(params, cb, errorCb);
@@ -197,54 +180,44 @@ describe('WebSocketConnector', () => {
197
180
  expect(client.subscriptions[0].cb).toBe(cb);
198
181
  expect(client.subscriptions[0].errorCb).toBe(errorCb);
199
182
  expect(client.subscriptions[0].quiet).toBe(false);
200
-
201
- const obj = { source: 'bar baz', client_reference: 'id' };
183
+ const obj = { source: "bar baz", client_reference: "id" };
202
184
  server.send(JSON.stringify(obj));
203
-
204
185
  expect(cb).toHaveBeenCalledTimes(1);
205
186
  expect(cb).toHaveBeenCalledWith(obj);
206
187
  });
207
-
208
188
  test("doesn't duplicate subscriptions", async () => {
209
- // eslint-disable-next-line no-unused-vars
210
- const client = new Connector();
189
+ const client = new WebSocketAPI();
211
190
  client.connect(`ws://foo:1234`);
212
191
  await server.connected;
213
- const params = { channel: 'bar', args: ['baz'], id: 'id' };
192
+ const params = { channel: "bar", args: ["baz"], id: "id" };
214
193
  const cb = jest.fn();
215
194
  const errorCb = jest.fn();
216
195
  client.subscribe(params, cb, errorCb, true);
217
196
  client.subscribe(params, cb, errorCb, true);
218
197
  expect(client.subscriptions.length).toBe(1);
219
-
220
- const obj = { source: 'bar baz', client_reference: 'id' };
198
+ const obj = { source: "bar baz", client_reference: "id" };
221
199
  server.send(JSON.stringify(obj));
222
-
223
200
  expect(cb).toHaveBeenCalledTimes(1);
224
201
  expect(cb).toHaveBeenCalledWith(obj);
225
202
  });
226
-
227
- test('send GET and SUB requests.', async () => {
228
- // eslint-disable-next-line no-unused-vars
229
- const client = new Connector();
203
+ test("send GET and SUB requests.", async () => {
204
+ const client = new WebSocketAPI();
230
205
  client.connect(`ws://foo:1234`);
231
206
  client.send = jest.fn();
232
- const params = { channel: 'bar', args: ['baz'], id: 'id' };
207
+ const params = { channel: "bar", args: ["baz"], id: "id" };
233
208
  const cb = jest.fn();
234
209
  const errorCb = jest.fn();
235
210
  client.subscribe(params, cb, errorCb);
236
211
  expect(client.send).toHaveBeenCalledTimes(2);
237
- expect(client.send).toHaveBeenCalledWith('GET bar baz id');
238
- expect(client.send).toHaveBeenCalledWith('SUB bar baz id');
212
+ expect(client.send).toHaveBeenCalledWith("GET bar baz id");
213
+ expect(client.send).toHaveBeenCalledWith("SUB bar baz id");
239
214
  client.send.mockRestore();
240
215
  });
241
-
242
- test('should register callback without sending GET and SUB requests (quiet=true).', async () => {
243
- // eslint-disable-next-line no-unused-vars
244
- const client = new Connector();
216
+ test("should register callback without sending GET and SUB requests (quiet=true).", async () => {
217
+ const client = new WebSocketAPI();
245
218
  client.connect(`ws://foo:1234`);
246
219
  await server.connected;
247
- const params = { channel: 'bar', args: ['baz'], id: 'id' };
220
+ const params = { channel: "bar", args: ["baz"], id: "id" };
248
221
  const cb = jest.fn();
249
222
  const errorCb = jest.fn();
250
223
  client.send = jest.fn();
@@ -258,14 +231,12 @@ describe('WebSocketConnector', () => {
258
231
  client.send.mockRestore();
259
232
  });
260
233
  });
261
-
262
- describe('#unsubscribe', () => {
263
- test('should only unsubscribe the subscription using the good cb', async () => {
264
- // eslint-disable-next-line no-unused-vars
265
- const client = new Connector();
234
+ describe("#unsubscribe", () => {
235
+ test("should only unsubscribe the subscription using the good cb", async () => {
236
+ const client = new WebSocketAPI();
266
237
  client.connect(`ws://foo:1234`);
267
238
  await server.connected;
268
- const params = { channel: 'foo', id: 'id' };
239
+ const params = { channel: "foo", id: "id" };
269
240
  const cb = jest.fn();
270
241
  const cb2 = jest.fn();
271
242
  client.subscribe(params, cb);
@@ -275,27 +246,22 @@ describe('WebSocketConnector', () => {
275
246
  expect(client.subscriptions[0].cb).toBe(cb);
276
247
  expect(client.subscriptions[1].params).toBe(params);
277
248
  expect(client.subscriptions[1].cb).toBe(cb2);
278
-
279
- client.unsubscribe('foo', cb);
249
+ client.unsubscribe("foo", cb);
280
250
  expect(client.subscriptions.length).toBe(1);
281
-
282
251
  expect(cb).toHaveBeenCalledTimes(0);
283
252
  expect(cb2).toHaveBeenCalledTimes(0);
284
- const obj = { source: 'foo', client_reference: 'id' };
253
+ const obj = { source: "foo", client_reference: "id" };
285
254
  server.send(JSON.stringify(obj));
286
-
287
255
  expect(cb2).toHaveBeenCalledTimes(1);
288
256
  expect(cb).toHaveBeenCalledTimes(0);
289
257
  });
290
-
291
- test('should unsubscribe all subscriptions related to a channel', () => {
292
- // eslint-disable-next-line no-unused-vars
293
- const client = new Connector();
258
+ test("should unsubscribe all subscriptions related to a channel", () => {
259
+ const client = new WebSocketAPI();
294
260
  client.connect(`ws://foo:1234`);
295
261
  client.websocket.removeEventListener = jest.fn();
296
262
  client.websocket.addEventListener = jest.fn();
297
- const params = { channel: 'foo' };
298
- const params2 = { channel: 'bar' };
263
+ const params = { channel: "foo" };
264
+ const params2 = { channel: "bar" };
299
265
  const cb = jest.fn();
300
266
  const cb2 = jest.fn();
301
267
  client.subscribe(params, cb);
@@ -305,49 +271,38 @@ describe('WebSocketConnector', () => {
305
271
  client.subscribe(params2, cb2);
306
272
  expect(client.subscriptions.length).toBe(3);
307
273
  expect(client.websocket.removeEventListener).toBeCalledTimes(2);
308
- expect(
309
- client.websocket.addEventListener.mock.calls.filter(
310
- (c) => c[0] === 'message',
311
- ).length,
312
- ).toBe(5);
313
-
314
- client.unsubscribe('foo');
274
+ expect(client.websocket.addEventListener.mock.calls.filter((c) => c[0] === "message").length).toBe(5);
275
+ client.unsubscribe("foo");
315
276
  expect(client.subscriptions.length).toBe(1);
316
277
  expect(client.subscriptions[0].params).toBe(params2);
317
278
  expect(client.subscriptions[0].cb).toBe(cb2);
318
279
  });
319
-
320
- test('send DEL when there is no more unquiet subscriptions on the channel', async () => {
321
- // eslint-disable-next-line no-unused-vars
322
- const client = new Connector();
280
+ test("send DEL when there is no more unquiet subscriptions on the channel", async () => {
281
+ const client = new WebSocketAPI();
323
282
  client.connect(`ws://foo:1234`);
324
283
  await server.connected;
325
284
  client.send = jest.fn();
326
285
  client.websocket.removeEventListener = jest.fn();
327
286
  client.websocket.addEventListener = jest.fn();
328
- const params = { channel: 'foo' };
287
+ const params = { channel: "foo" };
329
288
  const cb = jest.fn();
330
289
  client.subscribe(params, cb);
331
- expect(client.send).toHaveBeenCalledWith('GET foo');
332
- expect(client.send).toHaveBeenCalledWith('SUB foo');
333
-
334
- client.unsubscribe('foo');
335
- expect(client.send).toHaveBeenCalledWith('DEL foo');
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");
336
294
  });
337
-
338
295
  test("doesn't send DEL when we unsubscribe a quiet channel", () => {
339
- // eslint-disable-next-line no-unused-vars
340
- const client = new Connector();
296
+ const client = new WebSocketAPI();
341
297
  client.connect(`ws://foo:1234`);
342
298
  client.send = jest.fn();
343
299
  client.websocket.removeEventListener = jest.fn();
344
300
  client.websocket.addEventListener = jest.fn();
345
- const params = { channel: 'foo' };
301
+ const params = { channel: "foo" };
346
302
  const cb = jest.fn();
347
303
  client.subscribe(params, cb, null, true);
348
304
  expect(cb).toHaveBeenCalledTimes(0);
349
-
350
- client.unsubscribe('foo');
305
+ client.unsubscribe("foo");
351
306
  expect(cb).toHaveBeenCalledTimes(0);
352
307
  client.send.mockRestore();
353
308
  });
@@ -1,146 +1,81 @@
1
- import BaseObject from 'ol/Object';
2
-
3
- /**
4
- * A class representing a control to display on map.
5
- *
6
- * @example
7
- * const control = new Control();
8
- *
9
- * @classproperty {ol/Map~Map|mapboxgl.Map} map - The map which the control refers to.
10
- * @classproperty {boolean} active - Active the control.
11
- * @classproperty {HTMLElement} element - The HTML element used to render the control.
12
- * @classproperty {HTMLElement} target - The HTML element where to render the element property. Default is the map's element. Read only.
13
- */
1
+ import BaseObject from "ol/Object";
14
2
  class Control extends BaseObject {
15
- /**
16
- * Constructor
17
- *
18
- * @param {Object} [options] Control options.
19
- * @param {boolean} [options.active = true] Whether the control is active or not.
20
- * @param {HTMLElement} [options.element] The HTML element used to render the control.
21
- * @param {HTMLElement} [options.target] The HTML element where to render the element property. Default is the map's element.
22
- * @param {function} [options.render] Render function called whenever the control needs to be rerendered.
23
- */
24
3
  constructor(options = {}) {
25
4
  super(options);
26
5
  this.defineProperties(options);
27
-
28
6
  const { active } = {
29
7
  active: options.active !== false,
30
- ...options,
8
+ ...options
31
9
  };
32
-
33
- /**
34
- * @ignore
35
- */
36
10
  this.active = active;
37
11
  }
38
-
39
- /**
40
- * Define control's properties.
41
- *
42
- * @private
43
- */
44
12
  defineProperties(options) {
45
13
  const { target, element, render } = {
46
- ...options,
14
+ ...options
47
15
  };
48
-
49
16
  Object.defineProperties(this, {
50
17
  active: {
51
- get: () => this.get('active'),
18
+ get: () => this.get("active"),
52
19
  set: (newActive) => {
53
- this.set('active', newActive);
20
+ this.set("active", newActive);
54
21
  if (newActive) {
55
22
  this.activate();
56
23
  } else {
57
24
  this.deactivate();
58
25
  }
59
26
  this.render();
60
- },
27
+ }
61
28
  },
62
29
  map: {
63
- get: () => this.get('map'),
30
+ get: () => this.get("map"),
64
31
  set: (map) => {
65
- // Remove previous node.
66
32
  if (this.map && this.element && this.element.parentNode) {
67
33
  this.element.parentNode.removeChild(this.element);
68
34
  }
69
-
70
- // Clean listeners
71
35
  this.deactivate();
72
-
73
- this.set('map', map);
74
-
36
+ this.set("map", map);
75
37
  if (this.map) {
76
- // Add new node
77
- const targett =
78
- this.target ||
79
- (this.map.getTargetElement && this.map.getTargetElement()) ||
80
- (this.map.getContainer && this.map.getContainer());
81
-
38
+ const targett = this.target || this.map.getTargetElement && this.map.getTargetElement() || this.map.getContainer && this.map.getContainer();
82
39
  if (!this.element) {
83
40
  this.createDefaultElement();
84
41
  }
85
-
86
42
  if (this.element) {
87
43
  targett.appendChild(this.element);
88
44
  }
89
-
90
- // Add listeners
91
45
  if (this.active) {
92
46
  this.activate();
93
47
  }
94
48
  }
95
49
  this.render();
96
- },
50
+ }
97
51
  },
98
52
  target: {
99
- value: target,
53
+ value: target
100
54
  },
101
55
  element: {
102
56
  value: element,
103
- writable: true,
57
+ writable: true
104
58
  },
105
59
  render: {
106
- /** @ignore */
107
60
  value: render || this.render,
108
- writable: true,
109
- },
61
+ writable: true
62
+ }
110
63
  });
111
64
  }
112
-
113
- /**
114
- * Add listeners then renders the control.
115
- * To be defined in inherited classes.
116
- */
65
+ attachToMap(map) {
66
+ this.map = map;
67
+ }
68
+ detachFromMap() {
69
+ this.map = null;
70
+ }
117
71
  activate() {
118
72
  this.deactivate();
119
73
  }
120
-
121
- /**
122
- * Remove listeners added by activate() function then renders the control.
123
- * To be defined in inherited classes.
124
- */
125
- // eslint-disable-next-line class-methods-use-this
126
- deactivate() {}
127
-
128
- /**
129
- * The default render function. It renders content in the HTML element.
130
- * To be defined in inherited classes.
131
- *
132
- * @private
133
- */
134
- render() {}
135
-
136
- /**
137
- * The default element to display if this.element is not defined.
138
- * To be defined in inherited classes.
139
- *
140
- * @private
141
- */
142
- // eslint-disable-next-line class-methods-use-this
143
- createDefaultElement() {}
74
+ deactivate() {
75
+ }
76
+ render() {
77
+ }
78
+ createDefaultElement() {
79
+ }
144
80
  }
145
-
146
81
  export default Control;