Skip to content

Commit 0ff5d97

Browse files
committed
refactor(router): Move navigationId handling to the transition manager (angular#48202)
The navigationId is really just a count of how many navigations have been processed through the navigation pipeline. This tracking should be done as part of the navigation transition handler PR Close angular#48202
1 parent 55ae4aa commit 0ff5d97

2 files changed

Lines changed: 29 additions & 19 deletions

File tree

packages/router/src/navigation_transition.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,6 @@ interface InternalRouterInterface {
266266
browserUrlTree: UrlTree;
267267
currentUrlTree: UrlTree;
268268
rawUrlTree: UrlTree;
269-
navigationId: number;
270269
readonly routerState: RouterState;
271270
errorHandler: ErrorHandler;
272271
titleStrategy?: TitleStrategy;
@@ -297,10 +296,11 @@ export class NavigationTransitions {
297296
private readonly environmentInjector = inject(EnvironmentInjector);
298297
private readonly urlSerializer = inject(UrlSerializer);
299298
private readonly rootContexts = inject(ChildrenOutletContexts);
300-
private _transitions?: BehaviorSubject<NavigationTransition>;
301-
get transitions(): BehaviorSubject<NavigationTransition>|undefined {
302-
return this._transitions;
299+
navigationId = 0;
300+
get hasRequestedNavigation() {
301+
return this.navigationId !== 0;
303302
}
303+
private transitions?: BehaviorSubject<NavigationTransition>;
304304

305305
constructor() {
306306
const onLoadStart = (r: Route) => this.events.next(new RouteConfigLoadStart(r));
@@ -309,8 +309,21 @@ export class NavigationTransitions {
309309
this.configLoader.onLoadStartListener = onLoadStart;
310310
}
311311

312+
complete() {
313+
this.transitions?.complete();
314+
}
315+
316+
handleNavigationRequest(
317+
request: Pick<
318+
NavigationTransition,
319+
'targetPageId'|'source'|'restoredState'|'currentUrlTree'|'currentRawUrl'|'rawUrl'|
320+
'extras'|'resolve'|'reject'|'promise'|'currentSnapshot'|'currentRouterState'>) {
321+
const id = ++this.navigationId;
322+
this.transitions?.next({...this.transitions.value, ...request, id});
323+
}
324+
312325
setupNavigations(router: InternalRouterInterface): Observable<NavigationTransition> {
313-
this._transitions = new BehaviorSubject<NavigationTransition>({
326+
this.transitions = new BehaviorSubject<NavigationTransition>({
314327
id: 0,
315328
targetPageId: 0,
316329
currentUrlTree: router.currentUrlTree,
@@ -331,7 +344,7 @@ export class NavigationTransitions {
331344
guards: {canActivateChecks: [], canDeactivateChecks: []},
332345
guardsResult: null,
333346
});
334-
return this._transitions.pipe(
347+
return this.transitions.pipe(
335348
filter(t => t.id !== 0),
336349

337350
// Extract URL
@@ -662,7 +675,7 @@ export class NavigationTransitions {
662675
`Navigation ID ${
663676
overallTransitionState
664677
.id} is not equal to the current navigation id ${
665-
router.navigationId}` :
678+
this.navigationId}` :
666679
'';
667680
this.cancelNavigationTransition(
668681
overallTransitionState, cancelationReason,

packages/router/src/router.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,11 @@ export class Router {
178178
private disposed = false;
179179

180180
private locationSubscription?: SubscriptionLike;
181-
/** @internal */
182-
navigationId: number = 0;
181+
// TODO(b/260747083): This should not exist and navigationId should be private in
182+
// `NavigationTransitions`
183+
private get navigationId() {
184+
return this.navigationTransitions.navigationId;
185+
}
183186

184187
/**
185188
* The id of the currently active page in the router.
@@ -394,17 +397,12 @@ export class Router {
394397
this.routerState.root.component = this.rootComponentType;
395398
}
396399

397-
private setTransition(t: Partial<NavigationTransition>): void {
398-
this.navigationTransitions.transitions?.next(
399-
{...this.navigationTransitions.transitions?.value, ...t});
400-
}
401-
402400
/**
403401
* Sets up the location change listener and performs the initial navigation.
404402
*/
405403
initialNavigation(): void {
406404
this.setUpLocationChangeListener();
407-
if (this.navigationId === 0) {
405+
if (!this.navigationTransitions.hasRequestedNavigation) {
408406
this.navigateByUrl(this.location.path(true), {replaceUrl: true});
409407
}
410408
}
@@ -499,7 +497,7 @@ export class Router {
499497

500498
/** Disposes of the router. */
501499
dispose(): void {
502-
this.navigationTransitions.transitions?.complete();
500+
this.navigationTransitions.complete();
503501
if (this.locationSubscription) {
504502
this.locationSubscription.unsubscribe();
505503
this.locationSubscription = undefined;
@@ -736,7 +734,6 @@ export class Router {
736734
});
737735
}
738736

739-
const id = ++this.navigationId;
740737
let targetPageId: number;
741738
if (this.canceledNavigationResolution === 'computed') {
742739
const isInitialPage = this.currentPageId === 0;
@@ -762,12 +759,12 @@ export class Router {
762759
targetPageId = 0;
763760
}
764761

765-
this.setTransition({
766-
id,
762+
this.navigationTransitions.handleNavigationRequest({
767763
targetPageId,
768764
source,
769765
restoredState,
770766
currentUrlTree: this.currentUrlTree,
767+
currentRawUrl: this.currentUrlTree,
771768
rawUrl,
772769
extras,
773770
resolve,

0 commit comments

Comments
 (0)