refactor(router): create pipeable recognize function (#25740)

PR Close #25740
This commit is contained in:
Jason Aden 2018-07-25 17:19:58 -07:00 committed by Alex Rickabaugh
parent 4d544bcb46
commit 4decc8521d
2 changed files with 43 additions and 13 deletions

View File

@ -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<any>| null, config: Route[], serializer: (url: UrlTree) => string,
paramsInheritanceStrategy: 'emptyOnly' |
'always'): OperatorFunction<UrlTree, RouterStateSnapshot> {
return function(source: Observable<UrlTree>) {
return source.pipe(mergeMap(
(appliedUrl: UrlTree) => recognizeFn(
rootComponentType, config, appliedUrl, serializer(appliedUrl),
paramsInheritanceStrategy)));
};
}

View File

@ -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<Event>)
.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<Event>)
.next(new RoutesRecognized(
id, this.serializeUrl(url), this.serializeUrl(appliedUrl),
snapshot))))));
return {appliedUrl, snapshot};
}));
}));
} else {
urlAndSnapshot$ = of ({appliedUrl: url, snapshot: precreatedState});
}