Skip to content

Commit

Permalink
Fix issue where multiple nav clicks with loaders resolving out of ord…
Browse files Browse the repository at this point in the history
…er wouldn't end up on the last clicked route
  • Loading branch information
jhsware committed Jan 3, 2023
1 parent 28df0d9 commit 93ded92
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/inferno-router/src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type RouterContext = { router: TContextRouter };
export class Router extends Component<IRouterProps, any> {
public unlisten;
private _loaderFetchControllers: AbortController[] = [];
private _loaderIteration = 0;

constructor(props: IRouterProps, context: { router: TContextRouter }) {
super(props, context);
Expand Down Expand Up @@ -105,6 +106,13 @@ export class Router extends Component<IRouterProps, any> {
}

private _matchAndResolveLoaders(match?: Match<any>) {
// Keep track of invokation order
// Bumping the counter needs to be done first because calling abort
// triggers promise to resolve with "aborted"
this._loaderIteration = (this._loaderIteration + 1) % 10000;
const currentIteration = this._loaderIteration;


for (const controller of this._loaderFetchControllers) {
controller.abort();
}
Expand All @@ -120,13 +128,16 @@ export class Router extends Component<IRouterProps, any> {
// Store AbortController instances for each matched loader
this._loaderFetchControllers = loaderEntries.map(e => e.controller);

// First execution of loaders
resolveLoaders(loaderEntries)
.then((initialData) => {
this.setState({
match,
initialData,
});
// On multiple pending navigations, only update interface with last
// in case they resolve out of order
if (currentIteration === this._loaderIteration) {
this.setState({
match,
initialData,
});
}
});
}

Expand Down

0 comments on commit 93ded92

Please sign in to comment.