From 9c1c9454892c7b19b7774bcd324a64f89bf5c7c7 Mon Sep 17 00:00:00 2001 From: Jason Aden Date: Thu, 26 Jul 2018 11:29:03 -0700 Subject: [PATCH] refactor(router): create pipeable setupPreactivation function (#25740) PR Close #25740 --- .../src/operators/setup_preactivation.ts | 30 +++++++++++++++++++ packages/router/src/router.ts | 20 ++++++------- 2 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 packages/router/src/operators/setup_preactivation.ts diff --git a/packages/router/src/operators/setup_preactivation.ts b/packages/router/src/operators/setup_preactivation.ts new file mode 100644 index 0000000000..9ad6106332 --- /dev/null +++ b/packages/router/src/operators/setup_preactivation.ts @@ -0,0 +1,30 @@ +/** + * @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 {map} from 'rxjs/operators'; + +import {Event} from '../events'; +import {PreActivation} from '../pre_activation'; +import {ChildrenOutletContexts} from '../router_outlet_context'; +import {RouterStateSnapshot} from '../router_state'; + +export function setupPreactivation( + rootContexts: ChildrenOutletContexts, currentSnapshot: RouterStateSnapshot, + moduleInjector: Injector, + forwardEvent?: (evt: Event) => void): OperatorFunction { + return function(source: Observable) { + return source.pipe(map(snapshot => { + const preActivation = + new PreActivation(snapshot, currentSnapshot, moduleInjector, forwardEvent); + preActivation.initialize(rootContexts); + return preActivation; + })); + }; +} diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index 6438800940..83f474efc5 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -18,6 +18,7 @@ import {ActivationEnd, ChildActivationEnd, Event, GuardsCheckEnd, GuardsCheckSta import {applyRedirects} from './operators/apply_redirects'; import {beforePreactivation} from './operators/before_preactivation'; import {recognize} from './operators/recognize'; +import {setupPreactivation} from './operators/setup_preactivation'; import {PreActivation} from './pre_activation'; import {DefaultRouteReuseStrategy, DetachedRouteHandleInternal, RouteReuseStrategy} from './route_reuse_strategy'; import {RouterConfigLoader} from './router_config_loader'; @@ -724,16 +725,15 @@ export class Router { // run preactivation: guards and data resolvers let preActivation: PreActivation; - const preactivationSetup$ = beforePreactivationDone$.pipe(map((p): NavStreamValue => { - if (typeof p === 'boolean') return p; - const {appliedUrl, snapshot} = p; - const moduleInjector = this.ngModule.injector; - preActivation = new PreActivation( - snapshot, this.routerState.snapshot, moduleInjector, - (evt: Event) => this.triggerEvent(evt)); - preActivation.initialize(this.rootContexts); - return {appliedUrl, snapshot}; - })); + const preactivationSetup$ = beforePreactivationDone$.pipe( + mergeMap( + p => of (p.snapshot) + .pipe( + setupPreactivation( + this.rootContexts, this.routerState.snapshot, this.ngModule.injector, + (evt: Event) => this.triggerEvent(evt)), + map(preActivation => ({...p, preActivation})))), + tap(p => preActivation = p.preActivation)); const preactivationCheckGuards$ = preactivationSetup$.pipe(mergeMap((p): Observable => {