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

PR Close #25740
This commit is contained in:
Jason Aden 2018-07-26 11:29:03 -07:00 committed by Kara Erickson
parent 44eef5c343
commit 33101359c6
2 changed files with 40 additions and 10 deletions

View File

@ -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<RouterStateSnapshot, PreActivation> {
return function(source: Observable<RouterStateSnapshot>) {
return source.pipe(map(snapshot => {
const preActivation =
new PreActivation(snapshot, currentSnapshot, moduleInjector, forwardEvent);
preActivation.initialize(rootContexts);
return preActivation;
}));
};
}

View File

@ -18,6 +18,7 @@ import {ActivationEnd, ChildActivationEnd, Event, GuardsCheckEnd, GuardsCheckSta
import {applyRedirects} from './operators/apply_redirects'; import {applyRedirects} from './operators/apply_redirects';
import {beforePreactivation} from './operators/before_preactivation'; import {beforePreactivation} from './operators/before_preactivation';
import {recognize} from './operators/recognize'; import {recognize} from './operators/recognize';
import {setupPreactivation} from './operators/setup_preactivation';
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';
import {RouterConfigLoader} from './router_config_loader'; import {RouterConfigLoader} from './router_config_loader';
@ -716,16 +717,15 @@ export class Router {
// run preactivation: guards and data resolvers // run preactivation: guards and data resolvers
let preActivation: PreActivation; let preActivation: PreActivation;
const preactivationSetup$ = beforePreactivationDone$.pipe(map((p): NavStreamValue => { const preactivationSetup$ = beforePreactivationDone$.pipe(
if (typeof p === 'boolean') return p; mergeMap(
const {appliedUrl, snapshot} = p; p => of (p.snapshot)
const moduleInjector = this.ngModule.injector; .pipe(
preActivation = new PreActivation( setupPreactivation(
snapshot, this.routerState.snapshot, moduleInjector, this.rootContexts, this.routerState.snapshot, this.ngModule.injector,
(evt: Event) => this.triggerEvent(evt)); (evt: Event) => this.triggerEvent(evt)),
preActivation.initialize(this.rootContexts); map(preActivation => ({...p, preActivation})))),
return {appliedUrl, snapshot}; tap(p => preActivation = p.preActivation));
}));
const preactivationCheckGuards$ = const preactivationCheckGuards$ =
preactivationSetup$.pipe(mergeMap((p): Observable<NavStreamValue> => { preactivationSetup$.pipe(mergeMap((p): Observable<NavStreamValue> => {