Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion apps/nativescript-demo-ng/src/tests/ns-location-strategy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// make sure you import mocha-config before @angular/core
import { DefaultUrlSerializer, UrlTree } from '@angular/router';
import { FrameService, LocationState, NavigationOptions, NSLocationStrategy, Outlet } from '@nativescript/angular';
import {
FrameService,
LocationState,
NavigationOptions,
NSLocationStrategy,
Outlet,
RouterExtensions,
} from '@nativescript/angular';
import { BackstackEntry, Frame, NavigationEntry, Page, View } from '@nativescript/core';

export class FakeFrameService extends FrameService {
Expand Down Expand Up @@ -508,4 +515,27 @@ describe('NSLocationStrategy', () => {
assertStatesEqual(outlet.states, expectedStatesTest1);
assertStatesEqual(outlet2.states, expectedStatesTest2);
});

it('RouterExtensions navigates after the Router handled the popstate of a page back', async () => {
const { strategy, frameService } = initStrategy('/');
const currentFrame = frameService.getFrame();
const handled: Array<string> = [];
// The Router handles a popstate in a setTimeout
strategy.onPopState(() => setTimeout(() => handled.push('popstate')));
const router: any = {
navigate: () => {
handled.push('navigate');
return Promise.resolve(true);
},
};
const routerExtensions = new RouterExtensions(router, strategy, frameService);
simulatePageNavigation(strategy, '/page', currentFrame, 'primary');

// Act
simulatePageBack(strategy, currentFrame);
await routerExtensions.navigate(['/other']);

// Assert
expect(handled).toEqual(['popstate', 'navigate']);
});
});
17 changes: 17 additions & 0 deletions packages/angular/src/lib/legacy/router/ns-location-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class NSLocationStrategy extends LocationStrategy implements OnDestroy {
private currentOutlet: Outlet;

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

public _modalNavigationDepth = 0;
Expand Down Expand Up @@ -253,6 +254,22 @@ export class NSLocationStrategy extends LocationStrategy implements OnDestroy {
for (const fn of this.popStateCallbacks) {
fn(change);
}

// The Router handles a popstate in a setTimeout, this timeout runs after it
const handled = new Promise<void>((resolve) => setTimeout(resolve, 0)).then(() => {
if (this.popStateHandled === handled) {
this.popStateHandled = null;
}
});
this.popStateHandled = handled;
}

/**
* Runs the navigation once the Router has handled the last popstate.
* Otherwise the Router would apply that popstate after the navigation and undo it.
*/
public _navigateAfterPopState<T>(navigate: () => Promise<T>): Promise<T> {
return this.popStateHandled ? this.popStateHandled.then(navigate) : navigate();
}

public toString() {
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/src/lib/legacy/router/router-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export class RouterExtensions {
constructor(public router: Router, public locationStrategy: NSLocationStrategy, public frameService: FrameService) {}

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

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

public back(backNavigationOptions?: BackNavigationOptions) {
Expand Down