From 4decc8521da342c1a2f7edc143b03a2cbc2d14ea Mon Sep 17 00:00:00 2001 From: Jason Aden Date: Wed, 25 Jul 2018 17:19:58 -0700 Subject: [PATCH] refactor(router): create pipeable recognize function (#25740) PR Close #25740 --- packages/router/src/operators/recognize.ts | 28 ++++++++++++++++++++++ packages/router/src/router.ts | 28 ++++++++++++---------- 2 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 packages/router/src/operators/recognize.ts diff --git a/packages/router/src/operators/recognize.ts b/packages/router/src/operators/recognize.ts new file mode 100644 index 0000000000..4754bf0b4e --- /dev/null +++ b/packages/router/src/operators/recognize.ts @@ -0,0 +1,28 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {Type} from '@angular/core'; +import {Observable, OperatorFunction} from 'rxjs'; +import {mergeMap} from 'rxjs/operators'; + +import {Route} from '../config'; +import {recognize as recognizeFn} from '../recognize'; +import {RouterStateSnapshot} from '../router_state'; +import {UrlTree} from '../url_tree'; + +export function recognize( + rootComponentType: Type| null, config: Route[], serializer: (url: UrlTree) => string, + paramsInheritanceStrategy: 'emptyOnly' | + 'always'): OperatorFunction { + return function(source: Observable) { + return source.pipe(mergeMap( + (appliedUrl: UrlTree) => recognizeFn( + rootComponentType, config, appliedUrl, serializer(appliedUrl), + paramsInheritanceStrategy))); + }; +} \ No newline at end of file diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index 30b657dc4d..297bb9d623 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -9,15 +9,15 @@ import {Location} from '@angular/common'; import {Compiler, Injector, NgModuleFactoryLoader, NgModuleRef, NgZone, Optional, Type, isDevMode, ɵConsole as Console} from '@angular/core'; import {BehaviorSubject, Observable, Subject, Subscription, of } from 'rxjs'; -import {concatMap, map, mergeMap} from 'rxjs/operators'; +import {concatMap, map, mergeMap, tap} from 'rxjs/operators'; import {applyRedirects} from './apply_redirects'; import {LoadedRouterConfig, QueryParamsHandling, Route, Routes, standardizeConfig, validateConfig} from './config'; import {createRouterState} from './create_router_state'; import {createUrlTree} from './create_url_tree'; import {ActivationEnd, ChildActivationEnd, Event, GuardsCheckEnd, GuardsCheckStart, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, NavigationTrigger, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RoutesRecognized} from './events'; +import {recognize} from './operators/recognize'; import {PreActivation} from './pre_activation'; -import {recognize} from './recognize'; import {DefaultRouteReuseStrategy, DetachedRouteHandleInternal, RouteReuseStrategy} from './route_reuse_strategy'; import {RouterConfigLoader} from './router_config_loader'; import {ChildrenOutletContexts} from './router_outlet_context'; @@ -699,18 +699,20 @@ export class Router { const redirectsApplied$ = applyRedirects(moduleInjector, this.configLoader, this.urlSerializer, url, this.config); - urlAndSnapshot$ = redirectsApplied$.pipe(mergeMap((appliedUrl: UrlTree) => { - return recognize( - this.rootComponentType, this.config, appliedUrl, this.serializeUrl(appliedUrl), - this.paramsInheritanceStrategy, this.relativeLinkResolution) - .pipe(map((snapshot: any) => { - (this.events as Subject) - .next(new RoutesRecognized( - id, this.serializeUrl(url), this.serializeUrl(appliedUrl), snapshot)); + urlAndSnapshot$ = redirectsApplied$.pipe(mergeMap( + (appliedUrl: UrlTree) => + recognize( + this.rootComponentType, this.config, (url) => this.serializeUrl(url), + this.paramsInheritanceStrategy)(of (appliedUrl)) + .pipe( + map((snapshot: RouterStateSnapshot) => ({appliedUrl, snapshot})), + tap(({appliedUrl, + snapshot}: {appliedUrl: UrlTree, snapshot: RouterStateSnapshot}) => + (this.events as Subject) + .next(new RoutesRecognized( + id, this.serializeUrl(url), this.serializeUrl(appliedUrl), + snapshot)))))); - return {appliedUrl, snapshot}; - })); - })); } else { urlAndSnapshot$ = of ({appliedUrl: url, snapshot: precreatedState}); }