From 68b7847b4cfc486209236f86182dd081a33002de Mon Sep 17 00:00:00 2001 From: Jason Aden Date: Wed, 25 Jul 2018 17:33:47 -0700 Subject: [PATCH] refactor(router): create pipeable applyRedirects function (#25740) PR Close #25740 --- .../router/src/operators/apply_redirects.ts | 31 +++++++++++++++++++ packages/router/src/router.ts | 7 +++-- 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 packages/router/src/operators/apply_redirects.ts diff --git a/packages/router/src/operators/apply_redirects.ts b/packages/router/src/operators/apply_redirects.ts new file mode 100644 index 0000000000..f79ff17d50 --- /dev/null +++ b/packages/router/src/operators/apply_redirects.ts @@ -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 { + return function(source: Observable) { + return source.pipe(flatMap( + urlTree => applyRedirectsFn(moduleInjector, configLoader, urlSerializer, urlTree, config))); + }; +} diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index 544f46cd2f..7ca8a033cc 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -11,11 +11,11 @@ import {Compiler, Injector, NgModuleFactoryLoader, NgModuleRef, NgZone, Optional import {BehaviorSubject, Observable, Subject, Subscription, of } from 'rxjs'; 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 {applyRedirects} from './operators/apply_redirects'; import {recognize} from './operators/recognize'; import {PreActivation} from './pre_activation'; import {DefaultRouteReuseStrategy, DetachedRouteHandleInternal, RouteReuseStrategy} from './route_reuse_strategy'; @@ -29,6 +29,7 @@ import {forEach} from './utils/collection'; import {TreeNode, nodeChildrenAsMap} from './utils/tree'; + /** * @description * @@ -688,8 +689,8 @@ export class Router { let urlAndSnapshot$: Observable; if (!precreatedState) { const moduleInjector = this.ngModule.injector; - const redirectsApplied$ = - applyRedirects(moduleInjector, this.configLoader, this.urlSerializer, url, this.config); + const redirectsApplied$ = applyRedirects( + moduleInjector, this.configLoader, this.urlSerializer, this.config)(of (url)); urlAndSnapshot$ = redirectsApplied$.pipe(mergeMap( (appliedUrl: UrlTree) =>