Skip to content

Commit

Permalink
Fix #192, #232 and #233. Refactor some comments. Don't disable intera…
Browse files Browse the repository at this point in the history
…ctivity for when there's an ongoing routing request. Properly cancel prev. requests (#235)

* chore(deps-dev): bump svelte-spa-router from 3.3.0 to 4.0.1

Bumps [svelte-spa-router](https://github.com/ItalyPaleAle/svelte-spa-router) from 3.3.0 to 4.0.1.
- [Release notes](https://github.com/ItalyPaleAle/svelte-spa-router/releases)
- [Changelog](https://github.com/ItalyPaleAle/svelte-spa-router/blob/main/CHANGELOG.md)
- [Commits](ItalyPaleAle/svelte-spa-router@v3.3.0...v4.0.1)

---
updated-dependencies:
- dependency-name: svelte-spa-router
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps-dev): bump eslint from 8.56.0 to 8.57.0

Bumps [eslint](https://github.com/eslint/eslint) from 8.56.0 to 8.57.0.
- [Release notes](https://github.com/eslint/eslint/releases)
- [Changelog](https://github.com/eslint/eslint/blob/main/CHANGELOG.md)
- [Commits](eslint/eslint@v8.56.0...v8.57.0)

---
updated-dependencies:
- dependency-name: eslint
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps): bump nanoid from 5.0.4 to 5.0.6

Bumps [nanoid](https://github.com/ai/nanoid) from 5.0.4 to 5.0.6.
- [Release notes](https://github.com/ai/nanoid/releases)
- [Changelog](https://github.com/ai/nanoid/blob/main/CHANGELOG.md)
- [Commits](ai/nanoid@5.0.4...5.0.6)

---
updated-dependencies:
- dependency-name: nanoid
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps-dev): bump postcss-load-config from 4.0.2 to 5.0.3

Bumps [postcss-load-config](https://github.com/postcss/postcss-load-config) from 4.0.2 to 5.0.3.
- [Release notes](https://github.com/postcss/postcss-load-config/releases)
- [Changelog](https://github.com/postcss/postcss-load-config/blob/main/CHANGELOG.md)
- [Commits](postcss/postcss-load-config@v4.0.2...v5.0.3)

---
updated-dependencies:
- dependency-name: postcss-load-config
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps-dev): bump prettier-plugin-svelte from 3.1.2 to 3.2.2

Bumps [prettier-plugin-svelte](https://github.com/sveltejs/prettier-plugin-svelte) from 3.1.2 to 3.2.2.
- [Changelog](https://github.com/sveltejs/prettier-plugin-svelte/blob/master/CHANGELOG.md)
- [Commits](sveltejs/prettier-plugin-svelte@v3.1.2...v3.2.2)

---
updated-dependencies:
- dependency-name: prettier-plugin-svelte
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps-dev): bump lint-staged from 15.2.0 to 15.2.2

Bumps [lint-staged](https://github.com/okonet/lint-staged) from 15.2.0 to 15.2.2.
- [Release notes](https://github.com/okonet/lint-staged/releases)
- [Changelog](https://github.com/lint-staged/lint-staged/blob/master/CHANGELOG.md)
- [Commits](lint-staged/lint-staged@v15.2.0...v15.2.2)

---
updated-dependencies:
- dependency-name: lint-staged
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps-dev): bump svelte from 4.2.8 to 4.2.12

Bumps [svelte](https://github.com/sveltejs/svelte/tree/HEAD/packages/svelte) from 4.2.8 to 4.2.12.
- [Release notes](https://github.com/sveltejs/svelte/releases)
- [Changelog](https://github.com/sveltejs/svelte/blob/[email protected]/packages/svelte/CHANGELOG.md)
- [Commits](https://github.com/sveltejs/svelte/commits/[email protected]/packages/svelte)

---
updated-dependencies:
- dependency-name: svelte
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps-dev): bump prettier from 3.1.1 to 3.2.5

Bumps [prettier](https://github.com/prettier/prettier) from 3.1.1 to 3.2.5.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
- [Commits](prettier/prettier@3.1.1...3.2.5)

---
updated-dependencies:
- dependency-name: prettier
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps-dev): bump husky from 8.0.3 to 9.0.11

Bumps [husky](https://github.com/typicode/husky) from 8.0.3 to 9.0.11.
- [Release notes](https://github.com/typicode/husky/releases)
- [Commits](typicode/husky@v8.0.3...v9.0.11)

---
updated-dependencies:
- dependency-name: husky
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps-dev): bump svelte-check from 3.6.2 to 3.6.8

Bumps [svelte-check](https://github.com/sveltejs/language-tools) from 3.6.2 to 3.6.8.
- [Release notes](https://github.com/sveltejs/language-tools/releases)
- [Commits](sveltejs/language-tools@svelte-check-3.6.2...svelte-check-3.6.8)

---
updated-dependencies:
- dependency-name: svelte-check
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps-dev): bump postcss from 8.4.33 to 8.4.38

Bumps [postcss](https://github.com/postcss/postcss) from 8.4.33 to 8.4.38.
- [Release notes](https://github.com/postcss/postcss/releases)
- [Changelog](https://github.com/postcss/postcss/blob/main/CHANGELOG.md)
- [Commits](postcss/postcss@8.4.33...8.4.38)

---
updated-dependencies:
- dependency-name: postcss
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps-dev): bump @tsconfig/svelte from 5.0.2 to 5.0.4

Bumps [@tsconfig/svelte](https://github.com/tsconfig/bases/tree/HEAD/bases) from 5.0.2 to 5.0.4.
- [Commits](https://github.com/tsconfig/bases/commits/HEAD/bases)

---
updated-dependencies:
- dependency-name: "@tsconfig/svelte"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps-dev): bump @types/lodash from 4.14.202 to 4.17.0

Bumps [@types/lodash](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/lodash) from 4.14.202 to 4.17.0.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/lodash)

---
updated-dependencies:
- dependency-name: "@types/lodash"
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps-dev): bump autoprefixer from 10.4.16 to 10.4.19

Bumps [autoprefixer](https://github.com/postcss/autoprefixer) from 10.4.16 to 10.4.19.
- [Release notes](https://github.com/postcss/autoprefixer/releases)
- [Changelog](https://github.com/postcss/autoprefixer/blob/main/CHANGELOG.md)
- [Commits](postcss/autoprefixer@10.4.16...10.4.19)

---
updated-dependencies:
- dependency-name: autoprefixer
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps-dev): bump @typescript-eslint/parser from 6.18.1 to 7.7.0

Bumps [@typescript-eslint/parser](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/parser) from 6.18.1 to 7.7.0.
- [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases)
- [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/parser/CHANGELOG.md)
- [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v7.7.0/packages/parser)

---
updated-dependencies:
- dependency-name: "@typescript-eslint/parser"
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore(deps-dev): bump tailwindcss from 3.4.1 to 3.4.3

Bumps [tailwindcss](https://github.com/tailwindlabs/tailwindcss) from 3.4.1 to 3.4.3.
- [Release notes](https://github.com/tailwindlabs/tailwindcss/releases)
- [Changelog](https://github.com/tailwindlabs/tailwindcss/blob/v3.4.3/CHANGELOG.md)
- [Commits](tailwindlabs/tailwindcss@v3.4.1...v3.4.3)

---
updated-dependencies:
- dependency-name: tailwindcss
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

* Update deps and reformat the code with updated rules

* Update MapLibre to v4
Use MapLibre v4's version of handling custom attributions
Use of updated loadImage API

* Update Vite to 4.5.3 (to fix a vulnerability)

* Fix #232 waypoints are not in draggable state after creating them and after ending a previous drag

* small refactoring (for the comments). Only rely on the public interface to check the current interactivity state.

* Fix #233 by not awaiting for fetchDirections to finish before detaching drag-related event listeners

* Fix #233 and #234 by not disabling the interactivity for when there's an ongoing request

* Fix #192

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
smellyshovel and dependabot[bot] authored Aug 2, 2024
1 parent 05d4c1e commit 0a12345
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions src/directions/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,15 @@ export default class MapLibreGlDirections extends MapLibreGlDirectionsEvented {

protected async fetchDirections(originalEvent: MapLibreGlDirectionsWaypointEvent) {
/*
* If a request from a previous fetchDirections is already running,
* we abort it as we don't need the previous value anymore
* If a request from a previous fetchDirections is already running (there's no such a check really, but this is
* implied be calling this method), we abort it as we don't need the previous value anymore.
*/
this.abortController?.abort();
const prevInteractive = this.interactive;

if (this._waypoints.length >= 2) {
this.fire(new MapLibreGlDirectionsRoutingEvent("fetchroutesstart", originalEvent));

this.abortController = new AbortController();
const signal = this.abortController.signal;
signal.onabort = () => {
this.interactive = prevInteractive;
};

this.interactive = false;

let timer;
if (this.configuration.requestTimeout !== null) {
Expand Down Expand Up @@ -221,11 +214,6 @@ export default class MapLibreGlDirections extends MapLibreGlDirectionsEvented {
}),
);
} finally {
// see #189 (https://github.com/maplibre/maplibre-gl-directions/issues/189)
if (this.abortController?.signal.reason !== "DESTROY") this.interactive = prevInteractive;

this.abortController = undefined;

clearTimeout(timer);
}

Expand Down Expand Up @@ -600,8 +588,8 @@ export default class MapLibreGlDirections extends MapLibreGlDirectionsEvented {

protected onDragMove(e: MapMouseEvent | MapTouchEvent) {
/*
* when updateOnMove is active, if this timeout ever triggers it means we are dragging,
* but not moving the mouse.
* When the `updateOnMove` option is on, if this timeout ever triggers, it means we are dragging, but not moving the
* mouse.
*/
if (this.configuration.refreshOnMove) {
clearTimeout(this.noMouseMovementTimer);
Expand All @@ -627,12 +615,13 @@ export default class MapLibreGlDirections extends MapLibreGlDirectionsEvented {

this.hoverpoint.geometry.coordinates = [e.lngLat.lng, e.lngLat.lat];
}

this.currentMousePosition = e.point;
this.draw();

/*
* If the user selected a waypoint or a routeline and routes should update while dragging,
* we initiate the live updating process.
* If the user selected a waypoint or a routeline and routes should update while dragging, we initiate the live
* updating process.
*/
if (this.configuration.refreshOnMove && !this.refreshOnMoveIsRefreshing) {
this.liveRefreshHandler(e);
Expand All @@ -641,9 +630,9 @@ export default class MapLibreGlDirections extends MapLibreGlDirectionsEvented {

protected noMouseMovementTimer?: ReturnType<typeof setTimeout>;

protected async onDragUp(e: MapMouseEvent | MapTouchEvent) {
protected onDragUp(e: MapMouseEvent | MapTouchEvent) {
/*
* if routes should update while dragging, there's some cleanup to do when releasing the mouse
* If the routes should update while dragging, there's some cleanup to do when releasing the mouse.
*/
if (this.configuration.refreshOnMove) {
clearTimeout(this.noMouseMovementTimer);
Expand All @@ -655,7 +644,7 @@ export default class MapLibreGlDirections extends MapLibreGlDirectionsEvented {

/*
* Only add a new waypoint or change the dragged one's position if the mouse has been dragged for more than the
* specified threshold. If the specified threshold's value is less than zero then treat it as if it was zero.
* configured threshold. If the specified threshold's value is less than zero then treat it as if it was zero.
*/
if (
Math.abs(e.point.x - this.dragDownPosition?.x) >
Expand All @@ -679,21 +668,20 @@ export default class MapLibreGlDirections extends MapLibreGlDirectionsEvented {
/*
* If the routing request has failed for some reason, restore the waypoint's original position.
*/
try {
await this.fetchDirections(waypointEvent);
} catch (err) {

this.fetchDirections(waypointEvent).catch((err) => {
if (!(err instanceof DOMException && err.name == "AbortError")) {
if (this.waypointBeingDraggedInitialCoordinates) {
if (this.waypointBeingDragged && this.waypointBeingDraggedInitialCoordinates) {
this.waypointBeingDragged.geometry.coordinates = this.waypointBeingDraggedInitialCoordinates;
}
}
}
});

this.waypointBeingDragged = undefined;
this.waypointBeingDraggedInitialCoordinates = undefined;
} else if (this.hoverpoint) {
/*
* If the selected route line has been dragged then add a waypoint at the previously saved index and remove the
* If the selected routeline has been dragged then add a waypoint at the previously saved index and remove the
* hoverpoint.
*/

Expand Down Expand Up @@ -726,7 +714,7 @@ export default class MapLibreGlDirections extends MapLibreGlDirectionsEvented {
this.deHighlight();

/*
* Remove the specifically assigned for the drag-related events listeners.
* Remove the listeners assigned specifically for the drag-related events.
*/
if (e.type === "touchend") {
this.map.off("touchmove", this.onDragMoveHandler);
Expand All @@ -747,6 +735,12 @@ export default class MapLibreGlDirections extends MapLibreGlDirectionsEvented {
this.map.dragPan.enable();

this.draw();

// After the moved waypoint was rendered, imitate hovering the mouse over it (because it actually keeps being
// hovered right after the drug-up event ends).
this.map.once("idle", () => {
this.onMove(e);
});
}

protected lastRequestMousePosition = {
Expand Down Expand Up @@ -798,7 +792,7 @@ export default class MapLibreGlDirections extends MapLibreGlDirectionsEvented {
],
})[0];

if (this._interactive && this.configuration.sensitiveWaypointLayers.includes(feature?.layer.id ?? "")) {
if (this.interactive && this.configuration.sensitiveWaypointLayers.includes(feature?.layer.id ?? "")) {
/*
* If a waypoint is clicked, remove it.
*/
Expand All @@ -810,7 +804,7 @@ export default class MapLibreGlDirections extends MapLibreGlDirectionsEvented {
if (~respectiveWaypointIndex) {
this._removeWaypoint(respectiveWaypointIndex, e);
}
} else if (this._interactive && this.configuration.sensitiveSnappointLayers.includes(feature?.layer.id ?? "")) {
} else if (this.interactive && this.configuration.sensitiveSnappointLayers.includes(feature?.layer.id ?? "")) {
/*
* If a snappoint is clicked, find its respective waypoint and remove it.
*/
Expand All @@ -827,24 +821,30 @@ export default class MapLibreGlDirections extends MapLibreGlDirectionsEvented {
this.configuration.sensitiveAltRoutelineLayers.includes(feature?.layer.id ?? "")
) {
/*
* If an alternative route line is clicked, set its index as the selected route's one.
* If an alternative routeline is clicked, set its index as the selected route's one.
*/

this.selectedRouteIndex = this.routelines.findIndex((routeline) => {
return !!routeline.find((segment) => {
return segment.properties?.id === feature?.properties?.id;
});
});
} else if (this._interactive && !this.configuration.sensitiveRoutelineLayers.includes(feature?.layer.id ?? "")) {
} else if (this.interactive && !this.configuration.sensitiveRoutelineLayers.includes(feature?.layer.id ?? "")) {
/*
* If the selected route line is clicked, don't add a new waypoint. Else do.
* If the selected routeline is clicked, don't add a new waypoint. Else do.
*/

this._addWaypoint([e.lngLat.lng, e.lngLat.lat], undefined, e);
}

// the selected route might have changed, so it's important not to skip its redraw
// The selected route might have changed, so it's important not to skip its redraw.
this.draw(false);

// After the added waypoint was rendered, imitate hovering the mouse over it (because it actually keeps being
// hovered right after the click event ends).
this.map.once("idle", () => {
this.onMove(e);
});
}

protected assignWaypointsCategories() {
Expand Down Expand Up @@ -952,6 +952,12 @@ export default class MapLibreGlDirections extends MapLibreGlDirectionsEvented {
this.map.off("mousemove", this.onMoveHandler);
this.map.off("click", this.onClickHandler);
}

// see #192 (https://github.com/maplibre/maplibre-gl-directions/issues/192)
// There may be cases when the interactivity gets disabled while fetching the routes. In this case it's important
// to manually restore the map's drag-pan functionality, because after the map becomes non-interactive, the event
// listeners responsible for doing that won't be fired anymore.
this.map.dragPan.enable();
}
}

Expand Down Expand Up @@ -1136,8 +1142,7 @@ export default class MapLibreGlDirections extends MapLibreGlDirectionsEvented {
* de-initializing the instance.
*/
destroy() {
// see #189 (https://github.com/maplibre/maplibre-gl-directions/issues/189)
this.abortController?.abort("DESTROY");
this.abortController?.abort();

this.clear();
this.hoverable = false;
Expand Down

0 comments on commit 0a12345

Please sign in to comment.