maplibre / maplibre-gl-directions

A plugin to show routing directions on a MapLibre GL JS map
https://maplibre.org/maplibre-gl-directions/
MIT License
72 stars 16 forks source link

`destroy()`-ing the Directions instance while it fetches the routes leads to multiple runtime errors #189

Closed smellyshovel closed 1 year ago

smellyshovel commented 1 year ago

When the destrtoy() method is called while the Directions is fetching the routes, that leads to multipleruntime errors. Seems like them come from the mousemove handler which is for some reason not detached after the plugin's layers are removed from the map (with destroy()).

smellyshovel commented 1 year ago

As it turned out, the reason was in a race-condition. If there's a previously-ongoing request to fetch directions and the destroy() method is called, it calls the setWaypoints() method, which synchronously calls the asynchronous fetchDirections(). When it's done and its finally clause is invoked to restore the previous interactive state, if it was true, it overwrites the interactive = false from the destroy() mehod and thus re-adds all the event listeners. Since after destroy() and setting the instance to undefined the instance doesn't exist anymore, there are no layers anymore as well. And the move callback tries to address layers that don't exist anymore.

smellyshovel commented 1 year ago

Fixed in #190 by aborting the ongoing request with a special reason and only restoring the prev. interactive state when it's not that special reason.