From a3a4ca0b0097bdf9250f4a12982b266e046574f3 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 13 Dec 2022 08:57:45 -0800 Subject: [PATCH] refactor(router): move internal router properties to transition handler where appropriate (#48475) Working towards removing the backwards dependency on router from the navigation transition handler, this change moves `rootComponentType` and `afterPreactivation` to the transition handler since that is the only location those properties are used. PR Close #48475 --- packages/router/src/navigation_transition.ts | 17 ++++++++++++----- packages/router/src/provide_router.ts | 2 +- packages/router/src/router.ts | 17 +++-------------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/packages/router/src/navigation_transition.ts b/packages/router/src/navigation_transition.ts index 162105034ac..de5ed204526 100644 --- a/packages/router/src/navigation_transition.ts +++ b/packages/router/src/navigation_transition.ts @@ -270,7 +270,6 @@ interface InternalRouterInterface { errorHandler: ErrorHandler; titleStrategy?: TitleStrategy; navigated: boolean; - afterPreactivation: () => Observable; urlHandlingStrategy: UrlHandlingStrategy; routeReuseStrategy: RouteReuseStrategy; onSameUrlNavigation: 'reload'|'ignore'; @@ -278,7 +277,6 @@ interface InternalRouterInterface { urlUpdateStrategy: 'deferred'|'eager'; serializeUrl(url: UrlTree): string; config: Routes; - rootComponentType: Type|null; setBrowserUrl(url: UrlTree, t: NavigationTransition): void; restoreHistory(t: NavigationTransition, restoringFromCaughtError?: boolean): void; scheduleNavigation( @@ -301,6 +299,15 @@ export class NavigationTransitions { return this.navigationId !== 0; } private transitions?: BehaviorSubject; + /** + * Hook that enables you to pause navigation after the preactivation phase. + * Used by `RouterModule`. + * + * @internal + */ + afterPreactivation: () => Observable = () => of(void 0); + /** @internal */ + rootComponentType: Type|null = null; constructor() { const onLoadStart = (r: Route) => this.events.next(new RouteConfigLoadStart(r)); @@ -437,7 +444,7 @@ export class NavigationTransitions { // Recognize recognize( - this.environmentInjector, router.rootComponentType, + this.environmentInjector, this.rootComponentType, router.config, this.urlSerializer, router.paramsInheritanceStrategy), @@ -472,7 +479,7 @@ export class NavigationTransitions { restoredState); this.events.next(navStart); const targetSnapshot = - createEmptyState(extractedUrl, router.rootComponentType).snapshot; + createEmptyState(extractedUrl, this.rootComponentType).snapshot; overallTransitionState = { ...t, @@ -614,7 +621,7 @@ export class NavigationTransitions { .pipe(defaultIfEmpty(), take(1)); }), - switchTap(() => router.afterPreactivation()), + switchTap(() => this.afterPreactivation()), map((t: NavigationTransition) => { const targetRouterState = createRouterState( diff --git a/packages/router/src/provide_router.ts b/packages/router/src/provide_router.ts index d98be8d8fc4..127c2a44f3c 100644 --- a/packages/router/src/provide_router.ts +++ b/packages/router/src/provide_router.ts @@ -358,7 +358,7 @@ export function withEnabledBlockingInitialNavigation(): EnabledBlockingInitialNa resolve(true); }); - router.afterPreactivation = () => { + injector.get(NavigationTransitions).afterPreactivation = () => { // Unblock APP_INITIALIZER once we get to `afterPreactivation`. At this point, we // assume activation will complete successfully (even though this is not // guaranteed). diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index ed2c1f6530a..045326ec93a 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -204,14 +204,6 @@ export class Router { navigated: boolean = false; private lastSuccessfulId: number = -1; - /** - * Hook that enables you to pause navigation after the preactivation phase. - * Used by `RouterModule`. - * - * @internal - */ - afterPreactivation: () => Observable = () => of(void 0); - /** * A strategy for extracting and merging URLs. * Used for AngularJS to Angular migrations. @@ -317,9 +309,6 @@ export class Router { private readonly urlSerializer = inject(UrlSerializer); private readonly location = inject(Location); - /** @internal */ - rootComponentType: Type|null = null; - constructor() { this.isNgZoneEnabled = inject(NgZone) instanceof NgZone && NgZone.isInAngularZone(); @@ -328,7 +317,7 @@ export class Router { this.rawUrlTree = this.currentUrlTree; this.browserUrlTree = this.currentUrlTree; - this.routerState = createEmptyState(this.currentUrlTree, this.rootComponentType); + this.routerState = createEmptyState(this.currentUrlTree, null); this.navigationTransitions.setupNavigations(this).subscribe( t => { @@ -342,10 +331,10 @@ export class Router { /** @internal */ resetRootComponentType(rootComponentType: Type): void { - this.rootComponentType = rootComponentType; // TODO: vsavkin router 4.0 should make the root component set to null // this will simplify the lifecycle of the router. - this.routerState.root.component = this.rootComponentType; + this.routerState.root.component = rootComponentType; + this.navigationTransitions.rootComponentType = rootComponentType; } /**