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
This commit is contained in:
Andrew Scott
2022-12-13 08:57:45 -08:00
committed by Alex Rickabaugh
parent edd7982d51
commit a3a4ca0b00
3 changed files with 16 additions and 20 deletions
+12 -5
View File
@@ -270,7 +270,6 @@ interface InternalRouterInterface {
errorHandler: ErrorHandler;
titleStrategy?: TitleStrategy;
navigated: boolean;
afterPreactivation: () => Observable<void>;
urlHandlingStrategy: UrlHandlingStrategy;
routeReuseStrategy: RouteReuseStrategy;
onSameUrlNavigation: 'reload'|'ignore';
@@ -278,7 +277,6 @@ interface InternalRouterInterface {
urlUpdateStrategy: 'deferred'|'eager';
serializeUrl(url: UrlTree): string;
config: Routes;
rootComponentType: Type<any>|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<NavigationTransition>;
/**
* Hook that enables you to pause navigation after the preactivation phase.
* Used by `RouterModule`.
*
* @internal
*/
afterPreactivation: () => Observable<void> = () => of(void 0);
/** @internal */
rootComponentType: Type<any>|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(
+1 -1
View File
@@ -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).
+3 -14
View File
@@ -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<void> = () => 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<any>|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<any>): 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;
}
/**