Skip to content

Commit

Permalink
refactor(router): create pipeable afterPreactivation function (#25740)
Browse files Browse the repository at this point in the history
PR Close #25740
  • Loading branch information
jasonaden authored and alxhub committed Sep 27, 2018
1 parent 5b3c08b commit 4c0d4fc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
29 changes: 29 additions & 0 deletions packages/router/src/operators/after_preactivation.ts
@@ -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 afterPreactivation(
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))));
};
}
19 changes: 8 additions & 11 deletions packages/router/src/router.ts
Expand Up @@ -31,6 +31,7 @@ import {DefaultUrlHandlingStrategy, UrlHandlingStrategy} from './url_handling_st
import {UrlSerializer, UrlTree, containsTree, createEmptyUrlTree} from './url_tree';
import {forEach} from './utils/collection';
import {TreeNode, nodeChildrenAsMap} from './utils/tree';
import { afterPreactivation } from './operators/after_preactivation';



Expand Down Expand Up @@ -777,17 +778,13 @@ export class Router {
of (p)
));

const preactivationDone$ =
preactivationResolveData$.pipe(mergeMap((p): Observable<NavStreamValue> => {
if (typeof p === 'boolean' || this.navigationId !== id) return of (false);
return this.hooks
.afterPreactivation(p.snapshot, {
navigationId: id,
appliedUrlTree: url,
rawUrlTree: rawUrl, skipLocationChange, replaceUrl,
})
.pipe(map(() => p));
}));
const preactivationDone$: Observable<NavStreamValue> =
preactivationResolveData$.pipe(mergeMap(p =>
typeof p === 'boolean' || this.navigationId !== id ? of (false) :
of (p).pipe(
afterPreactivation(this.hooks.afterPreactivation, id, url, rawUrl, skipLocationChange, replaceUrl),
map(() => p))
));


// create router state
Expand Down

0 comments on commit 4c0d4fc

Please sign in to comment.