Skip to content

Commit 20683a5

Browse files
committed
fix(router): navigate after the Router handled the popstate of a native back
1 parent 8144504 commit 20683a5

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎apps/nativescript-demo-ng/src/tests/ns-location-strategy.spec.ts‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
// make sure you import mocha-config before @angular/core
22
import { DefaultUrlSerializer, UrlTree } from '@angular/router';
3-
import { FrameService, LocationState, NavigationOptions, NSLocationStrategy, Outlet } from '@nativescript/angular';
3+
import {
4+
FrameService,
5+
LocationState,
6+
NavigationOptions,
7+
NSLocationStrategy,
8+
Outlet,
9+
RouterExtensions,
10+
} from '@nativescript/angular';
411
import { BackstackEntry, Frame, NavigationEntry, Page, View } from '@nativescript/core';
512

613
export class FakeFrameService extends FrameService {
@@ -508,4 +515,27 @@ describe('NSLocationStrategy', () => {
508515
assertStatesEqual(outlet.states, expectedStatesTest1);
509516
assertStatesEqual(outlet2.states, expectedStatesTest2);
510517
});
518+
519+
it('RouterExtensions navigates after the Router handled the popstate of a page back', async () => {
520+
const { strategy, frameService } = initStrategy('/');
521+
const currentFrame = frameService.getFrame();
522+
const handled: Array<string> = [];
523+
// The Router handles a popstate in a setTimeout
524+
strategy.onPopState(() => setTimeout(() => handled.push('popstate')));
525+
const router: any = {
526+
navigate: () => {
527+
handled.push('navigate');
528+
return Promise.resolve(true);
529+
},
530+
};
531+
const routerExtensions = new RouterExtensions(router, strategy, frameService);
532+
simulatePageNavigation(strategy, '/page', currentFrame, 'primary');
533+
534+
// Act
535+
simulatePageBack(strategy, currentFrame);
536+
await routerExtensions.navigate(['/other']);
537+
538+
// Assert
539+
expect(handled).toEqual(['popstate', 'navigate']);
540+
});
511541
});

‎packages/angular/src/lib/legacy/router/ns-location-strategy.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class NSLocationStrategy extends LocationStrategy implements OnDestroy {
1515
private currentOutlet: Outlet;
1616

1717
private popStateCallbacks = new Array<(_: any) => any>();
18+
private popStateHandled: Promise<void> | null = null;
1819
private currentUrlTree: UrlTree;
1920

2021
public _modalNavigationDepth = 0;
@@ -253,6 +254,22 @@ export class NSLocationStrategy extends LocationStrategy implements OnDestroy {
253254
for (const fn of this.popStateCallbacks) {
254255
fn(change);
255256
}
257+
258+
// The Router handles a popstate in a setTimeout, this timeout runs after it
259+
const handled = new Promise<void>((resolve) => setTimeout(resolve, 0)).then(() => {
260+
if (this.popStateHandled === handled) {
261+
this.popStateHandled = null;
262+
}
263+
});
264+
this.popStateHandled = handled;
265+
}
266+
267+
/**
268+
* Runs the navigation once the Router has handled the last popstate.
269+
* Otherwise the Router would apply that popstate after the navigation and undo it.
270+
*/
271+
public _navigateAfterPopState<T>(navigate: () => Promise<T>): Promise<T> {
272+
return this.popStateHandled ? this.popStateHandled.then(navigate) : navigate();
256273
}
257274

258275
public toString() {

‎packages/angular/src/lib/legacy/router/router-extensions.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ export class RouterExtensions {
2121
constructor(public router: Router, public locationStrategy: NSLocationStrategy, public frameService: FrameService) {}
2222

2323
public navigate(commands: any[], extras?: ExtendedNavigationExtras): Promise<boolean> {
24-
return this.router.navigate(commands, extras);
24+
return this.locationStrategy._navigateAfterPopState(() => this.router.navigate(commands, extras));
2525
}
2626

2727
public navigateByUrl(url: string | UrlTree, options?: ExtendedNavigationBehaviorOptions): Promise<boolean> {
28-
return this.router.navigateByUrl(url, options);
28+
return this.locationStrategy._navigateAfterPopState(() => this.router.navigateByUrl(url, options));
2929
}
3030

3131
public back(backNavigationOptions?: BackNavigationOptions) {

0 commit comments

Comments
 (0)