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

PR Close #25740
This commit is contained in:
Jason Aden 2018-08-14 11:36:52 -07:00 committed by Kara Erickson
parent 29d3f3f6dd
commit 855ad8804e
2 changed files with 65 additions and 32 deletions

View File

@ -0,0 +1,28 @@
/**
* @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 resolveData(
preActivation: PreActivation,
paramsInheritanceStrategy: 'emptyOnly' | 'always'): OperatorFunction<UrlTree, boolean> {
return function(source: Observable<UrlTree>) {
return source.pipe(mergeMap((appliedUrl): Observable<boolean> => {
return preActivation.resolveData(paramsInheritanceStrategy);
}));
};
}

View File

@ -19,6 +19,7 @@ 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 {checkGuards} from './operators/check_guards';
import {recognize} from './operators/recognize'; import {recognize} from './operators/recognize';
import {resolveData} from './operators/resolve_data';
import {setupPreactivation} from './operators/setup_preactivation'; 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';
@ -730,39 +731,43 @@ export class Router {
const preactivationCheckGuards$: Observable<NavStreamValue> = const preactivationCheckGuards$: Observable<NavStreamValue> =
preactivationSetup$.pipe(mergeMap( preactivationSetup$.pipe(mergeMap(
p => this.navigationId !== id ? of (false) : of (p.appliedUrl) p => this.navigationId !== id ?
.pipe( of (false) :
tap(_ => this.triggerEvent(new GuardsCheckStart( of (p.appliedUrl)
id, this.serializeUrl(url), this.serializeUrl(p.appliedUrl), .pipe(
p.snapshot))), tap(_ => this.triggerEvent(new GuardsCheckStart(
checkGuards( id, this.serializeUrl(url), this.serializeUrl(p.appliedUrl),
this.rootContexts, this.routerState.snapshot, this.ngModule.injector, p.snapshot))),
preActivation), checkGuards(
tap(shouldActivate => this.triggerEvent(new GuardsCheckEnd( this.rootContexts, this.routerState.snapshot, this.ngModule.injector,
id, this.serializeUrl(url), this.serializeUrl(p.appliedUrl), preActivation),
p.snapshot, shouldActivate))), tap(shouldActivate => this.triggerEvent(new GuardsCheckEnd(
map(shouldActivate => ({ id, this.serializeUrl(url), this.serializeUrl(p.appliedUrl),
appliedUrl: p.appliedUrl, p.snapshot, shouldActivate))),
snapshot: p.snapshot, map(shouldActivate => ({
shouldActivate: shouldActivate appliedUrl: p.appliedUrl,
}))))); snapshot: p.snapshot,
shouldActivate: shouldActivate
})))));
const preactivationResolveData$ = const preactivationResolveData$: Observable<NavStreamValue> = preactivationCheckGuards$.pipe(mergeMap(p =>
preactivationCheckGuards$.pipe(mergeMap((p): Observable<NavStreamValue> => { // TODO(jasonaden): This should be simplified so there's one route to cancelling navigation, which would
if (typeof p === 'boolean' || this.navigationId !== id) return of (false); // unravel the stream. This would get rid of all these imperative checks in the middle of navigation.
typeof p === 'boolean' || this.navigationId !== id ?
if (p.shouldActivate && preActivation.isActivating()) { of (false) :
this.triggerEvent(new ResolveStart( p.shouldActivate && preActivation.isActivating() ?
id, this.serializeUrl(url), this.serializeUrl(p.appliedUrl), p.snapshot)); of (p.appliedUrl)
return preActivation.resolveData(this.paramsInheritanceStrategy).pipe(map(() => { .pipe(
this.triggerEvent(new ResolveEnd( tap(_ => this.triggerEvent(new ResolveStart(
id, this.serializeUrl(url), this.serializeUrl(p.appliedUrl), p.snapshot)); id, this.serializeUrl(url), this.serializeUrl(p.appliedUrl),
return p; p.snapshot))),
})); resolveData(preActivation, this.paramsInheritanceStrategy),
} else { tap(_ => this.triggerEvent(new ResolveEnd(
return of (p); id, this.serializeUrl(url), this.serializeUrl(p.appliedUrl),
} p.snapshot))),
})); map(_ => p)) :
of (p)
));
const preactivationDone$ = const preactivationDone$ =
preactivationResolveData$.pipe(mergeMap((p): Observable<NavStreamValue> => { preactivationResolveData$.pipe(mergeMap((p): Observable<NavStreamValue> => {