Skip to content

Commit

Permalink
refactor(router): avoid circular dependency
Browse files Browse the repository at this point in the history
Move const into shared file to avoid circular dependency
  • Loading branch information
EmmanuelRoux committed Jul 16, 2022
1 parent ad7edac commit aa9153e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@
"name": "RouteExampleModule"
},
{
"name": "RouteTitle"
"name": "RouteTitleKey"
},
{
"name": "Router"
Expand Down
6 changes: 3 additions & 3 deletions packages/router/src/operators/resolve_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {EMPTY, EmptyError, from, MonoTypeOperatorFunction, Observable, of, throw
import {catchError, concatMap, first, map, mapTo, mergeMap, takeLast, tap} from 'rxjs/operators';

import {ResolveData, Route} from '../models';
import {RouteTitle} from '../page_title_strategy';
import {NavigationTransition} from '../router';
import {ActivatedRouteSnapshot, inheritedParamsDataResolve, RouterStateSnapshot} from '../router_state';
import {RouteTitleKey} from '../shared';
import {wrapIntoObservable} from '../utils/collection';
import {getToken} from '../utils/preactivation';

Expand Down Expand Up @@ -45,14 +45,14 @@ function runResolve(
const config = futureARS.routeConfig;
const resolve = futureARS._resolve;
if (config?.title !== undefined && !hasStaticTitle(config)) {
resolve[RouteTitle] = config.title;
resolve[RouteTitleKey] = config.title;
}
return resolveNode(resolve, futureARS, futureRSS, moduleInjector)
.pipe(map((resolvedData: any) => {
futureARS._resolvedData = resolvedData;
futureARS.data = inheritedParamsDataResolve(futureARS, paramsInheritanceStrategy).resolve;
if (config && hasStaticTitle(config)) {
futureARS.data[RouteTitle] = config.title;
futureARS.data[RouteTitleKey] = config.title;
}
return null;
}));
Expand Down
11 changes: 2 additions & 9 deletions packages/router/src/page_title_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ import {Injectable} from '@angular/core';
import {Title} from '@angular/platform-browser';

import {ActivatedRouteSnapshot, RouterStateSnapshot} from './router_state';
import {PRIMARY_OUTLET} from './shared';

/**
* A private symbol used to store the value of `Route.title` inside the `Route.data` if it is a
* static string or `Route.resolve` if anything else. This allows us to reuse the existing route
* data/resolvers to support the title feature without new instrumentation in the `Router` pipeline.
*/
export const RouteTitle = Symbol('RouteTitle');
import {PRIMARY_OUTLET, RouteTitleKey} from './shared';

/**
* Provides a strategy for setting the page title after a router navigation.
Expand Down Expand Up @@ -64,7 +57,7 @@ export abstract class TitleStrategy {
* `Route.title` property, which can either be a static string or a resolved value.
*/
getResolvedTitleForRoute(snapshot: ActivatedRouteSnapshot) {
return snapshot.data[RouteTitle];
return snapshot.data[RouteTitleKey];
}
}

Expand Down
7 changes: 3 additions & 4 deletions packages/router/src/router_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {BehaviorSubject, Observable} from 'rxjs';
import {map} from 'rxjs/operators';

import {Data, ResolveData, Route} from './models';
import {RouteTitle as TitleKey} from './page_title_strategy';
import {convertToParamMap, ParamMap, Params, PRIMARY_OUTLET} from './shared';
import {convertToParamMap, ParamMap, Params, PRIMARY_OUTLET, RouteTitleKey} from './shared';
import {equalSegments, UrlSegment, UrlSegmentGroup, UrlTree} from './url_tree';
import {shallowEqual, shallowEqualArrays} from './utils/collection';
import {Tree, TreeNode} from './utils/tree';
Expand Down Expand Up @@ -123,7 +122,7 @@ export class ActivatedRoute {
_queryParamMap!: Observable<ParamMap>;

/** An Observable of the resolved route title */
readonly title: Observable<string|undefined> = this.data.pipe(map((d: Data) => d[TitleKey]));
readonly title: Observable<string|undefined> = this.data.pipe(map((d: Data) => d[RouteTitleKey]));

/** @internal */
constructor(
Expand Down Expand Up @@ -310,7 +309,7 @@ export class ActivatedRouteSnapshot {
_queryParamMap!: ParamMap;

/** The resolved route title */
readonly title: string|undefined = this.data?.[TitleKey];
readonly title: string|undefined = this.data?.[RouteTitleKey];

/** @internal */
constructor(
Expand Down
7 changes: 7 additions & 0 deletions packages/router/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import {UrlSegment, UrlSegmentGroup} from './url_tree';
*/
export const PRIMARY_OUTLET = 'primary';

/**
* A private symbol used to store the value of `Route.title` inside the `Route.data` if it is a
* static string or `Route.resolve` if anything else. This allows us to reuse the existing route
* data/resolvers to support the title feature without new instrumentation in the `Router` pipeline.
*/
export const RouteTitleKey = Symbol('RouteTitle');

/**
* A collection of matrix and query URL parameters.
* @see `convertToParamMap()`
Expand Down
5 changes: 2 additions & 3 deletions packages/router/test/router_state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

import {BehaviorSubject} from 'rxjs';

import {RouteTitle as TitleKey} from '../src/page_title_strategy';
import {ActivatedRoute, ActivatedRouteSnapshot, advanceActivatedRoute, equalParamsAndUrlSegments, RouterState, RouterStateSnapshot} from '../src/router_state';
import {Params} from '../src/shared';
import {Params, RouteTitleKey} from '../src/shared';
import {UrlSegment} from '../src/url_tree';
import {TreeNode} from '../src/utils/tree';

Expand Down Expand Up @@ -206,7 +205,7 @@ describe('RouterState & Snapshot', () => {

describe('ActivatedRoute', () => {
it('should get resolved route title', () => {
const data = {[TitleKey]: 'resolved title'};
const data = {[RouteTitleKey]: 'resolved title'};
const route = createActivatedRoute('a');
const snapshot = new (ActivatedRouteSnapshot as any)(
<any>[], <any>null, <any>null, <any>null, data, <any>null, 'test', <any>null, <any>null,
Expand Down

0 comments on commit aa9153e

Please sign in to comment.