mobility-toolbox-js 1.7.4 → 1.7.5

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.
@@ -31,6 +31,8 @@ const REGEX_VIA_POINT_COORD = /^([\d.]+),([\d.]+)$/;
31
31
  // !8596126$4 a station with id 8596126
32
32
  const REGEX_VIA_POINT_STATION_ID = /^!([^$]*)(\$?([a-zA-Z0-9]{0,2}))$/;
33
33
 
34
+ const STOP_FETCH_ABORT_CONTROLLER_KEY = 'stop-fetch';
35
+
34
36
  const getFlatCoordinatesFromSegments = (segmentArray) => {
35
37
  const coords = [];
36
38
  segmentArray.forEach((seg) => {
@@ -256,6 +258,7 @@ class RoutingControl extends Control {
256
258
  */
257
259
  reset() {
258
260
  // Clear viaPoints and source
261
+ this.abortRequests();
259
262
  this.viaPoints = [];
260
263
  this.routingLayer.olLayer.getSource().clear();
261
264
  this.dispatchEvent({
@@ -264,6 +267,27 @@ class RoutingControl extends Control {
264
267
  });
265
268
  }
266
269
 
270
+ /**
271
+ * Aborts viapoint and route requests
272
+ * @private
273
+ */
274
+ abortRequests() {
275
+ // Abort Routing API requests
276
+ this.graphs.forEach(([graph]) => {
277
+ if (this.abortControllers[graph]) {
278
+ this.abortControllers[graph].abort();
279
+ }
280
+ this.abortControllers[graph] = new AbortController();
281
+ });
282
+
283
+ // Abort Stops API requests
284
+ this.abortControllers[STOP_FETCH_ABORT_CONTROLLER_KEY]?.abort();
285
+ this.abortControllers[STOP_FETCH_ABORT_CONTROLLER_KEY] =
286
+ new AbortController();
287
+
288
+ this.loading = false;
289
+ }
290
+
267
291
  /**
268
292
  * Draws route on map using an array of coordinates:
269
293
  * If a single coordinate is passed a single point feature is added to map.
@@ -273,14 +297,20 @@ class RoutingControl extends Control {
273
297
  */
274
298
  drawRoute() {
275
299
  /* Calls RoutingAPI to draw a route using the viaPoints array */
300
+ this.abortRequests();
301
+ this.routingLayer.olLayer.getSource().clear();
302
+
303
+ if (!this.viaPoints.length) {
304
+ return null;
305
+ }
306
+
276
307
  if (this.viaPoints.length === 1) {
277
- // Clear source
278
- this.routingLayer.olLayer.getSource().clear();
279
308
  // Add point for first node
280
- return this.drawViaPoint(this.viaPoints[0], 0);
281
- }
282
- if (this.viaPoints.length < 2) {
283
- return null;
309
+ return this.drawViaPoint(
310
+ this.viaPoints[0],
311
+ 0,
312
+ this.abortControllers[STOP_FETCH_ABORT_CONTROLLER_KEY],
313
+ );
284
314
  }
285
315
 
286
316
  const formattedViaPoints = this.viaPoints.map((viaPoint) => {
@@ -302,14 +332,16 @@ class RoutingControl extends Control {
302
332
  this.routingLayer.olLayer.getSource().clear();
303
333
 
304
334
  // Create point features for the viaPoints
305
- this.viaPoints.forEach((viaPoint, idx) => this.drawViaPoint(viaPoint, idx));
335
+ this.viaPoints.forEach((viaPoint, idx) =>
336
+ this.drawViaPoint(
337
+ viaPoint,
338
+ idx,
339
+ this.abortControllers[STOP_FETCH_ABORT_CONTROLLER_KEY],
340
+ ),
341
+ );
306
342
 
307
343
  return Promise.all(
308
344
  this.graphs.map(([graph], index) => {
309
- if (this.abortControllers[graph]) {
310
- this.abortControllers[graph].abort();
311
- }
312
- this.abortControllers[graph] = new AbortController();
313
345
  return this.api
314
346
  .route(
315
347
  {
@@ -366,6 +398,7 @@ class RoutingControl extends Control {
366
398
  routeFeature.set('minResolution', this.graphsResolutions[index][0]);
367
399
  routeFeature.set('maxResolution', this.graphsResolutions[index][1]);
368
400
  this.routingLayer.olLayer.getSource().addFeature(routeFeature);
401
+ this.loading = false;
369
402
  })
370
403
  .catch((error) => {
371
404
  if (error.name === 'AbortError') {
@@ -379,6 +412,7 @@ class RoutingControl extends Control {
379
412
  target: this,
380
413
  });
381
414
  this.onRouteError(error, this);
415
+ this.loading = false;
382
416
  });
383
417
  }),
384
418
  );
@@ -389,7 +423,7 @@ class RoutingControl extends Control {
389
423
  *
390
424
  * @private
391
425
  */
392
- drawViaPoint(viaPoint, idx) {
426
+ drawViaPoint(viaPoint, idx, abortController) {
393
427
  const pointFeature = new Feature();
394
428
  pointFeature.set('viaPointIdx', idx);
395
429
 
@@ -415,6 +449,7 @@ class RoutingControl extends Control {
415
449
 
416
450
  return fetch(
417
451
  `${this.stopsApiUrl}lookup/${stationId}?key=${this.stopsApiKey}`,
452
+ { signal: abortController.signal },
418
453
  )
419
454
  .then((res) => res.json())
420
455
  .then((stationData) => {
@@ -426,6 +461,10 @@ class RoutingControl extends Control {
426
461
  return pointFeature;
427
462
  })
428
463
  .catch((error) => {
464
+ if (error.name === 'AbortError') {
465
+ // Ignore abort error
466
+ return;
467
+ }
429
468
  // Dispatch error event and execute error function
430
469
  this.dispatchEvent({
431
470
  type: 'error',
@@ -475,6 +514,7 @@ class RoutingControl extends Control {
475
514
  if (stationName) {
476
515
  return fetch(
477
516
  `${this.stopsApiUrl}?key=${this.stopsApiKey}&q=${stationName}&limit=1`,
517
+ { signal: abortController.signal },
478
518
  )
479
519
  .then((res) => res.json())
480
520
  .then((stationData) => {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "mobility-toolbox-js",
3
3
  "license": "MIT",
4
4
  "description": "Toolbox for JavaScript applications in the domains of mobility and logistics.",
5
- "version": "1.7.4",
5
+ "version": "1.7.5",
6
6
  "main": "index.js",
7
7
  "module": "module.js",
8
8
  "dependencies": {