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

PR Close #25740
This commit is contained in:
Jason Aden 2018-07-26 09:27:43 -07:00 committed by Alex Rickabaugh
parent 380b3d7653
commit ef5338663d
2 changed files with 54 additions and 34 deletions

View File

@ -0,0 +1,29 @@
/**
* @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 {MonoTypeOperatorFunction} from 'rxjs';
import {map, mergeMap} from 'rxjs/operators';
import {RouterHook} from '../router';
import {RouterStateSnapshot} from '../router_state';
import {UrlTree} from '../url_tree';
export function beforePreactivation(
hook: RouterHook, navigationId: number, appliedUrlTree: UrlTree, rawUrlTree: UrlTree,
skipLocationChange: boolean, replaceUrl: boolean):
MonoTypeOperatorFunction<{appliedUrl: UrlTree, snapshot: RouterStateSnapshot}> {
return function(source) {
return source.pipe(mergeMap(
p => hook(
p.snapshot,
{
navigationId, appliedUrlTree, rawUrlTree, skipLocationChange, replaceUrl,
})
.pipe(map(() => p))));
};
}

View File

@ -16,6 +16,7 @@ import {createRouterState} from './create_router_state';
import {createUrlTree} from './create_url_tree'; 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 {recognize} from './operators/recognize'; import {recognize} from './operators/recognize';
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';
@ -694,42 +695,32 @@ export class Router {
return new Promise((resolvePromise, rejectPromise) => { return new Promise((resolvePromise, rejectPromise) => {
// create an observable of the url and route state snapshot // create an observable of the url and route state snapshot
// this operation do not result in any side effects // this operation do not result in any side effects
let urlAndSnapshot$: Observable<NavStreamValue>; let urlAndSnapshot$ = of (url).pipe(mergeMap(url => {
if (!precreatedState) { if (precreatedState) {
const moduleInjector = this.ngModule.injector; return of ({appliedUrl: url, snapshot: precreatedState});
const redirectsApplied$ = applyRedirects( } else {
moduleInjector, this.configLoader, this.urlSerializer, this.config)(of (url)); return applyRedirects(
this.ngModule.injector, this.configLoader, this.urlSerializer,
urlAndSnapshot$ = redirectsApplied$.pipe(mergeMap( this.config)(of (url))
(appliedUrl: UrlTree) => .pipe(mergeMap(
appliedUrl =>
recognize( recognize(
this.rootComponentType, this.config, (url) => this.serializeUrl(url), this.rootComponentType, this.config, (url) => this.serializeUrl(url),
this.paramsInheritanceStrategy)(of (appliedUrl)) this.paramsInheritanceStrategy)(of (appliedUrl))
.pipe( .pipe(
map((snapshot: RouterStateSnapshot) => ({appliedUrl, snapshot})), map((snapshot: RouterStateSnapshot) => ({appliedUrl, snapshot})),
tap(({appliedUrl, tap(({appliedUrl, snapshot}:
snapshot}: {appliedUrl: UrlTree, snapshot: RouterStateSnapshot}) => {appliedUrl: UrlTree, snapshot: RouterStateSnapshot}) =>
(this.events as Subject<Event>) (this.events as Subject<Event>)
.next(new RoutesRecognized( .next(new RoutesRecognized(
id, this.serializeUrl(url), this.serializeUrl(appliedUrl), id, this.serializeUrl(url),
snapshot)))))); this.serializeUrl(appliedUrl), snapshot))))));
} else {
urlAndSnapshot$ = of ({appliedUrl: url, snapshot: precreatedState});
} }
const beforePreactivationDone$ =
urlAndSnapshot$.pipe(mergeMap((p): Observable<NavStreamValue> => {
if (typeof p === 'boolean') return of (p);
return this.hooks
.beforePreactivation(p.snapshot, {
navigationId: id,
appliedUrlTree: url,
rawUrlTree: rawUrl, skipLocationChange, replaceUrl,
})
.pipe(map(() => p));
})); }));
const beforePreactivationDone$ = urlAndSnapshot$.pipe(beforePreactivation(
this.hooks.beforePreactivation, id, url, rawUrl, skipLocationChange, replaceUrl));
// run preactivation: guards and data resolvers // run preactivation: guards and data resolvers
let preActivation: PreActivation; let preActivation: PreActivation;