mobility-toolbox-js 1.5.0 → 1.6.0-beta.1

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.
@@ -75,12 +75,17 @@ class TralisAPI {
75
75
  /** @ignore */
76
76
  this.prefix = options.prefix || '';
77
77
 
78
+ this.isUpdateBboxOnMoveEnd = options.isUpateBboxOnMoveEnd || false;
79
+
78
80
  /** @ignore */
79
81
  this.conn = new WebSocketConnector(wsUrl);
80
- this.conn.setProjection(options.projection || 'epsg:3857');
81
82
 
82
- if (options.bbox) {
83
- this.conn.setBbox(options.bbox);
83
+ if (!this.isUpdateBboxOnMoveEnd) {
84
+ this.conn.setProjection(options.projection || 'epsg:3857');
85
+
86
+ if (options.bbox) {
87
+ this.conn.setBbox(options.bbox);
88
+ }
84
89
  }
85
90
  }
86
91
 
@@ -377,7 +382,12 @@ class TralisAPI {
377
382
  */
378
383
  subscribeTrajectory(mode, onMessage) {
379
384
  this.unsubscribeTrajectory(onMessage);
380
- this.subscribe(`trajectory${getModeSuffix(mode, TralisModes)}`, onMessage);
385
+ this.subscribe(
386
+ `trajectory${getModeSuffix(mode, TralisModes)}`,
387
+ onMessage,
388
+ null,
389
+ this.isUpdateBboxOnMoveEnd,
390
+ );
381
391
  }
382
392
 
383
393
  /**
@@ -48,17 +48,17 @@ class WebSocketConnector {
48
48
  /** @ignore */
49
49
  this.websocket = new WebSocket(url);
50
50
 
51
- if (this.currentProj) {
52
- this.setProjection(this.currentProj);
53
- }
51
+ // if (this.currentProj) {
52
+ // this.setProjection(this.currentProj);
53
+ // }
54
54
 
55
- if (this.currentBbox) {
56
- this.setBbox(this.currentBbox);
57
- }
55
+ // [...this.subscriptions].forEach((s) => {
56
+ // this.subscribe(s.params, s.cb, s.errorCb, s.quiet);
57
+ // });
58
58
 
59
- [...this.subscriptions].forEach((s) => {
60
- this.subscribe(s.params, s.cb, s.errorCb, true);
61
- });
59
+ // if (this.currentBbox) {
60
+ // this.setBbox(this.currentBbox);
61
+ // }
62
62
 
63
63
  // reconnect on close
64
64
  this.websocket.onclose = () => {
@@ -123,10 +123,11 @@ class WebSocketConnector {
123
123
  * @type {Array<Array<number>>}
124
124
  */
125
125
  this.currentBbox = coordinates;
126
+
126
127
  this.send(`BBOX ${coordinates.join(' ')}`);
127
- this.subscriptions.forEach((s) => {
128
- this.get(s.params, s.cb, s.errorCb);
129
- });
128
+ // this.subscriptions.forEach((s) => {
129
+ // this.get(s.params, s.cb, s.errorCb);
130
+ // });
130
131
  }
131
132
 
132
133
  /**
@@ -184,27 +185,27 @@ class WebSocketConnector {
184
185
  * @param {Object} params Parameters for the websocket get request
185
186
  * @param {function} cb callback on listen
186
187
  * @param {function} errorCb Callback on error
187
- * @param {boolean} quiet if subscribe should be quiet
188
+ * @param {boolean} quiet if false, no GET or SUB requests are send, only the callback is registered.
188
189
  */
189
- subscribe(params, cb, errorCb, quiet) {
190
+ subscribe(params, cb, errorCb, quiet = false) {
190
191
  const { onMessageCb, onErrorCb } = this.listen(params, cb, errorCb);
191
192
  const reqStr = WebSocketConnector.getRequestString('', params);
192
193
 
193
- if (!quiet) {
194
- const index = this.subscriptions.findIndex((subcr) => {
195
- return params.channel === subcr.params.channel && cb === subcr.cb;
196
- });
197
- const newSubscr = { params, cb, errorCb, onMessageCb, onErrorCb };
198
- if (index > -1) {
199
- this.subscriptions[index] = newSubscr;
200
- } else {
201
- this.subscriptions.push(newSubscr);
202
- }
194
+ const index = this.subscriptions.findIndex((subcr) => {
195
+ return params.channel === subcr.params.channel && cb === subcr.cb;
196
+ });
197
+ const newSubscr = { params, cb, errorCb, onMessageCb, onErrorCb, quiet };
198
+ if (index > -1) {
199
+ this.subscriptions[index] = newSubscr;
200
+ } else {
201
+ this.subscriptions.push(newSubscr);
203
202
  }
204
203
 
205
204
  if (!this.subscribed[reqStr]) {
206
- this.send(`GET ${reqStr}`);
207
- this.send(`SUB ${reqStr}`);
205
+ if (!newSubscr.quiet) {
206
+ this.send(`GET ${reqStr}`);
207
+ this.send(`SUB ${reqStr}`);
208
+ }
208
209
  this.subscribed[reqStr] = true;
209
210
  }
210
211
  }
@@ -103,8 +103,8 @@ describe('WebSocketConnector', () => {
103
103
  client.setBbox([0, 0, 0, 0]);
104
104
 
105
105
  expect(client.subscriptions.length).toBe(3);
106
- expect(client.websocket.removeEventListener).toBeCalledTimes(3);
107
- expect(client.websocket.addEventListener).toBeCalledTimes(3);
106
+ expect(client.websocket.removeEventListener).toBeCalledTimes(0);
107
+ expect(client.websocket.addEventListener).toBeCalledTimes(0);
108
108
 
109
109
  client.unsubscribe('foo');
110
110
  expect(client.subscriptions.length).toBe(1);
@@ -44,10 +44,8 @@ export class TrackerLayerInterface {
44
44
  /**
45
45
  * Start the timeout for the next update.
46
46
  * @private
47
- * @param {number} zoom
48
47
  */
49
- // eslint-disable-next-line no-unused-vars
50
- startUpdateTime(zoom) {}
48
+ startUpdateTime() {}
51
49
 
52
50
  /**
53
51
  * Stop the clock.
@@ -426,6 +424,9 @@ const TrackerLayerMixin = (Base) =>
426
424
  }
427
425
  }
428
426
 
427
+ /**
428
+ *
429
+
429
430
  /**
430
431
  * Launch renderTrajectories. it avoids duplicating code in renderTrajectories method.
431
432
  *
@@ -547,9 +548,21 @@ const TrackerLayerMixin = (Base) =>
547
548
  });
548
549
  }
549
550
 
551
+ /**
552
+ * On zoomend we adjust the time interval of the update of vehicles positions.
553
+ *
554
+ * @param evt Event that triggered the function.
555
+ * @private
556
+ */
557
+ // eslint-disable-next-line no-unused-vars
558
+ onZoomEnd(evt) {
559
+ this.startUpdateTime();
560
+ }
561
+
550
562
  /**
551
563
  * Define beahvior when a vehicle is clicked
552
564
  * To be defined in child classes.
565
+ *
553
566
  * @private
554
567
  * @override
555
568
  */
@@ -558,6 +571,7 @@ const TrackerLayerMixin = (Base) =>
558
571
  /**
559
572
  * Define behavior when a vehicle is hovered
560
573
  * To be defined in child classes.
574
+ *
561
575
  * @private
562
576
  * @override
563
577
  */
@@ -565,6 +579,7 @@ const TrackerLayerMixin = (Base) =>
565
579
 
566
580
  /**
567
581
  * Get the duration before the next update depending on zoom level.
582
+ *
568
583
  * @private
569
584
  * @param {number} zoom
570
585
  */
@@ -572,6 +587,7 @@ const TrackerLayerMixin = (Base) =>
572
587
  const roundedZoom = Math.round(zoom);
573
588
  const timeStep = timeSteps[roundedZoom] || 25;
574
589
  const nextTick = Math.max(25, timeStep / this.speed);
590
+ console.log(`Next render in ${nextTick} ms.`);
575
591
  return nextTick;
576
592
  }
577
593
 
@@ -91,11 +91,14 @@ const TralisLayerMixin = (TrackerLayer) =>
91
91
  super({ ...options });
92
92
  this.debug = options.debug;
93
93
  this.mode = options.mode || TralisModes.TOPOGRAPHIC;
94
- this.refreshTimeInMs = 1000 / 30;
95
94
  this.onMessage = this.onMessage.bind(this);
96
95
  this.onDeleteMessage = this.onDeleteMessage.bind(this);
97
96
  this.api = options.api || new TralisAPI(options);
97
+
98
98
  this.format = new GeoJSON();
99
+
100
+ // This property will call api.setBbox on each movend event
101
+ this.isUpdateBboxOnMoveEnd = options.isUpdateBboxOnMoveEnd || false;
99
102
  }
100
103
 
101
104
  start() {
@@ -212,7 +215,7 @@ const TralisLayerMixin = (TrackerLayer) =>
212
215
  }
213
216
  }
214
217
 
215
- this.updateTrajectories();
218
+ this.tracker.setTrajectories(this.trajectories);
216
219
  }
217
220
 
218
221
  removeTrajectory(id) {
@@ -232,15 +235,6 @@ const TralisLayerMixin = (TrackerLayer) =>
232
235
  }
233
236
  }
234
237
  }
235
-
236
- updateTrajectories() {
237
- this.tracker.setTrajectories(this.trajectories);
238
- this.renderTrajectories();
239
- }
240
-
241
- getRefreshTimeInMs() {
242
- return this.refreshTimeInMs;
243
- }
244
238
  };
245
239
 
246
240
  export default TralisLayerMixin;