From 25fc6258e79ef1c00c919c0d2f0a849ebf3764f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Fri, 30 Sep 2022 22:47:51 +0200 Subject: [PATCH] Cancel running pathfinding jobs before scheduling a new one --- src/ruler.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/ruler.js b/src/ruler.js index f5d7362..c519fc7 100644 --- a/src/ruler.js +++ b/src/ruler.js @@ -110,19 +110,26 @@ export function extendRuler() { if (d.x === this.destination.x && d.y === this.destination.y) return; this.destination = d; - // TODO Cancel running pathfinding operations // TODO Check if we can reuse the old path this.dragRulerRemovePathfindingWaypoints(); + if (this.pathfindingJob) { + routinglib.cancelPathfinding(this.pathfindingJob); + this.pathfindingJob = undefined; + } if (isToken && isPathfindingEnabled.call(this)) { // TODO Show a busy indicator const from = getGridPositionFromPixelsObj(this.waypoints[this.waypoints.length - 1]); const to = getGridPositionFromPixelsObj(destination); - return routinglib - .calculatePath(from, to, {token: this.draggedEntity}) - .then(result => this.addPathToWaypoints(result?.path)) - .then(() => this.performPostPathfindingActions(options)); + const pathfindingJob = routinglib.calculatePath(from, to, {token: this.draggedEntity}); + this.pathfindingJob = pathfindingJob; + return this.pathfindingJob.then(result => { + if (pathfindingJob === this.pathfindingJob) { + this.addPathToWaypoints(result?.path); + return this.performPostPathfindingActions(options); + } + }); } return this.performPostPathfindingActions(options);