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

PR Close #25740
This commit is contained in:
Jason Aden 2018-07-25 17:33:47 -07:00 committed by Alex Rickabaugh
parent 4decc8521d
commit 380b3d7653
2 changed files with 35 additions and 3 deletions

View File

@ -0,0 +1,31 @@
/**
* @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 {Injector} from '@angular/core';
import {Observable, OperatorFunction} from 'rxjs';
import {flatMap} from 'rxjs/operators';
import {applyRedirects as applyRedirectsFn} from '../apply_redirects';
import {Routes} from '../config';
import {RouterConfigLoader} from '../router_config_loader';
import {UrlSerializer, UrlTree} from '../url_tree';
/**
* Returns the `UrlTree` with the redirection applied.
*
* Lazy modules are loaded along the way.
*/
export function applyRedirects(
moduleInjector: Injector, configLoader: RouterConfigLoader, urlSerializer: UrlSerializer,
config: Routes): OperatorFunction<UrlTree, UrlTree> {
return function(source: Observable<UrlTree>) {
return source.pipe(flatMap(
urlTree => applyRedirectsFn(moduleInjector, configLoader, urlSerializer, urlTree, config)));
};
}

View File

@ -11,11 +11,11 @@ import {Compiler, Injector, NgModuleFactoryLoader, NgModuleRef, NgZone, Optional
import {BehaviorSubject, Observable, Subject, Subscription, of } from 'rxjs'; import {BehaviorSubject, Observable, Subject, Subscription, of } from 'rxjs';
import {concatMap, map, mergeMap, tap} 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 {LoadedRouterConfig, QueryParamsHandling, Route, Routes, standardizeConfig, validateConfig} from './config';
import {createRouterState} from './create_router_state'; import {createRouterState} from './create_router_state';
import {createUrlTree} from './create_url_tree'; 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 {ActivationEnd, ChildActivationEnd, Event, GuardsCheckEnd, GuardsCheckStart, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, NavigationTrigger, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RoutesRecognized} from './events';
import {applyRedirects} from './operators/apply_redirects';
import {recognize} from './operators/recognize'; import {recognize} from './operators/recognize';
import {PreActivation} from './pre_activation'; import {PreActivation} from './pre_activation';
import {DefaultRouteReuseStrategy, DetachedRouteHandleInternal, RouteReuseStrategy} from './route_reuse_strategy'; import {DefaultRouteReuseStrategy, DetachedRouteHandleInternal, RouteReuseStrategy} from './route_reuse_strategy';
@ -29,6 +29,7 @@ import {forEach} from './utils/collection';
import {TreeNode, nodeChildrenAsMap} from './utils/tree'; import {TreeNode, nodeChildrenAsMap} from './utils/tree';
/** /**
* @description * @description
* *
@ -696,8 +697,8 @@ export class Router {
let urlAndSnapshot$: Observable<NavStreamValue>; let urlAndSnapshot$: Observable<NavStreamValue>;
if (!precreatedState) { if (!precreatedState) {
const moduleInjector = this.ngModule.injector; const moduleInjector = this.ngModule.injector;
const redirectsApplied$ = const redirectsApplied$ = applyRedirects(
applyRedirects(moduleInjector, this.configLoader, this.urlSerializer, url, this.config); moduleInjector, this.configLoader, this.urlSerializer, this.config)(of (url));
urlAndSnapshot$ = redirectsApplied$.pipe(mergeMap( urlAndSnapshot$ = redirectsApplied$.pipe(mergeMap(
(appliedUrl: UrlTree) => (appliedUrl: UrlTree) =>