Repository navigation
Description
Take this route for example. Move a marker and then click the back button. Multiple /route requests are fired and we can also notice that the camera is moving in a weird way and there are rapid updates of the heightgraph (both because of the many route requests).
I noticed this when I was trying to find a solution for #162. Let's take a look at the code:
graphhopper-maps/src/NavBar.ts
Lines 89 to 113 in fbe18e1
| parseUrlAndReplaceQuery() { | |
| this.isIgnoreQueryStoreUpdates = true | |
| //const parseResult = NavBar.parseUrl(this.appContext.location.href) | |
| const parseResult = this.parseUrl(window.location.href) | |
| // remove old query points | |
| this.queryStore.state.queryPoints.forEach(point => Dispatcher.dispatch(new RemovePoint(point))) | |
| // add parsed points | |
| parseResult.points.forEach((point, i) => Dispatcher.dispatch(new AddPoint(i, point.coordinate, true))) | |
| // assuming that at least two points should be present add un-initialized points if necessary | |
| for (let i = this.queryStore.state.queryPoints.length; i < 2; i++) { | |
| Dispatcher.dispatch(new AddPoint(i, { lat: 0, lng: 0 }, false)) | |
| } | |
| // add routing profile | |
| Dispatcher.dispatch(new SetVehicleProfile(parseResult.profile)) | |
| // add map style | |
| Dispatcher.dispatch(new SelectMapStyle(parseResult.styleOption)) | |
| this.isIgnoreQueryStoreUpdates = false | |
| } |
This code runs when we hit the 'back' button of the browser, because we add an event listener for the popstate event in the NavBar constructor. At first sight this.isIgnoreQueryStoreUpdates = true looks like we can manipulate the state without triggering any routing requests. But this flag only blocks the code that is run in onQueryStateChanged, which updates the url. The standard receivers of the query state change are still registered and will fire the routing requests. Because the request was valid before, it will remain valid when we remove each of the points and there will be a routing request after every point we remove. Similarly, there will be even more routing requests when add points in the next line, and even another one when we set the profile.
parseUrlAndReplaceQuery is not only called when the popstate event occurs, but also when the app loads for the first time (see index.tsx). In this case only one route request is fired, but this is rather coincidental it seems. When we refresh the page the QueryStore will be in it's initial state, namely with two (empty) points and an empty profile name:
graphhopper-maps/src/stores/QueryStore.ts
Lines 69 to 84 in fbe18e1
| protected getInitialState(): QueryStoreState { | |
| return { | |
| queryPoints: [ | |
| QueryStore.getEmptyPoint(0, QueryPointType.From), | |
| QueryStore.getEmptyPoint(1, QueryPointType.To), | |
| ], | |
| nextQueryPointId: 2, | |
| currentRequest: { | |
| subRequests: [], | |
| }, | |
| maxAlternativeRoutes: 3, | |
| routingProfile: { | |
| name: '', | |
| }, | |
| } | |
| } |
This means the query store state is invalid initially and this check in routeIfAllPointsSet:
graphhopper-maps/src/stores/QueryStore.ts
Lines 200 to 204 in fbe18e1
| if ( | |
| state.queryPoints.length > 1 && | |
| state.queryPoints.every(point => point.isInitialized) && | |
| state.routingProfile.name | |
| ) { |
will be false so no request will be fired. When we then remove the (empty) points in parseUrlAndReplaceQuery the request will still be invalid. It will be invalid even after we add the points from the url, because the profile will be missing still. Only when we set the profile at the end of parseUrlAndReplaceQuery the request finally becomes valid and a request will be fired. Therefore these single AddPoint/RemovePoint actions seem a bit odd to me. Since we know the query store state that we would like to set after we parsed the url would it not be a lot more intuitive to simply update the store's state once using all the information we got?
This is closely related to #162, because for #162 we also need to tweak the app's behavior following the url parsing and the initial /info and /route requests.
Here is a (preliminary) todo list:
- only trigger one route request when the
popstateevent occurs - maybe just update the query store state once instead of dispatching multiple
Add/RemovePointevents.
Activity