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

PR Close #25740
This commit is contained in:
Jason Aden 2018-08-14 10:12:55 -07:00 committed by Alex Rickabaugh
parent 9c1c945489
commit 68f2e0c391
2 changed files with 46 additions and 15 deletions

View File

@ -0,0 +1,27 @@
/**
* @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, Type} from '@angular/core';
import {Observable, OperatorFunction} from 'rxjs';
import {mergeMap} from 'rxjs/operators';
import {Route} from '../config';
import {PreActivation} from '../pre_activation';
import {recognize as recognizeFn} from '../recognize';
import {ChildrenOutletContexts} from '../router_outlet_context';
import {RouterStateSnapshot} from '../router_state';
import {UrlTree} from '../url_tree';
export function checkGuards(
rootContexts: ChildrenOutletContexts, currentSnapshot: RouterStateSnapshot,
moduleInjector: Injector, preActivation: PreActivation): OperatorFunction<UrlTree, boolean> {
return function(source: Observable<UrlTree>) {
return source.pipe(
mergeMap((appliedUrl): Observable<boolean> => { return preActivation.checkGuards(); }));
};
}

View File

@ -17,6 +17,7 @@ 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 {applyRedirects} from './operators/apply_redirects';
import {beforePreactivation} from './operators/before_preactivation'; import {beforePreactivation} from './operators/before_preactivation';
import {checkGuards} from './operators/check_guards';
import {recognize} from './operators/recognize'; import {recognize} from './operators/recognize';
import {setupPreactivation} from './operators/setup_preactivation'; import {setupPreactivation} from './operators/setup_preactivation';
import {PreActivation} from './pre_activation'; import {PreActivation} from './pre_activation';
@ -735,21 +736,24 @@ export class Router {
map(preActivation => ({...p, preActivation})))), map(preActivation => ({...p, preActivation})))),
tap(p => preActivation = p.preActivation)); tap(p => preActivation = p.preActivation));
const preactivationCheckGuards$ = const preactivationCheckGuards$: Observable<NavStreamValue> =
preactivationSetup$.pipe(mergeMap((p): Observable<NavStreamValue> => { preactivationSetup$.pipe(mergeMap(
if (typeof p === 'boolean' || this.navigationId !== id) return of (false); p => this.navigationId !== id ? of (false) : of (p.appliedUrl)
const {appliedUrl, snapshot} = p; .pipe(
tap(_ => this.triggerEvent(new GuardsCheckStart(
this.triggerEvent(new GuardsCheckStart( id, this.serializeUrl(url), this.serializeUrl(p.appliedUrl),
id, this.serializeUrl(url), this.serializeUrl(appliedUrl), snapshot)); p.snapshot))),
checkGuards(
return preActivation.checkGuards().pipe(map((shouldActivate: boolean) => { this.rootContexts, this.routerState.snapshot, this.ngModule.injector,
this.triggerEvent(new GuardsCheckEnd( preActivation),
id, this.serializeUrl(url), this.serializeUrl(appliedUrl), snapshot, tap(shouldActivate => this.triggerEvent(new GuardsCheckEnd(
shouldActivate)); id, this.serializeUrl(url), this.serializeUrl(p.appliedUrl),
return {appliedUrl: appliedUrl, snapshot: snapshot, shouldActivate: shouldActivate}; p.snapshot, shouldActivate))),
})); map(shouldActivate => ({
})); appliedUrl: p.appliedUrl,
snapshot: p.snapshot,
shouldActivate: shouldActivate
})))));
const preactivationResolveData$ = const preactivationResolveData$ =
preactivationCheckGuards$.pipe(mergeMap((p): Observable<NavStreamValue> => { preactivationCheckGuards$.pipe(mergeMap((p): Observable<NavStreamValue> => {