feat(router): add navigation execution context info to activation hooks (#24204)

This change adds to internal API hooks (undocumented API) for
`before/afterPreactivation`. The immediate need for this API is to
allow applications to build support for marshalling navigation between
a web worker and the main application.

Fixes #24202

PR Close #24204
This commit is contained in:
Jason Aden 2018-05-30 11:20:48 -07:00 committed by Victor Berchet
parent 57eacf4b5a
commit 20c463e97c
1 changed files with 28 additions and 4 deletions

View File

@ -177,12 +177,24 @@ type NavigationParams = {
/**
* @internal
*/
export type RouterHook = (snapshot: RouterStateSnapshot) => Observable<void>;
export type RouterHook = (snapshot: RouterStateSnapshot, runExtras: {
appliedUrlTree: UrlTree,
rawUrlTree: UrlTree,
skipLocationChange: boolean,
replaceUrl: boolean,
navigationId: number
}) => Observable<void>;
/**
* @internal
*/
function defaultRouterHook(snapshot: RouterStateSnapshot): Observable<void> {
function defaultRouterHook(snapshot: RouterStateSnapshot, runExtras: {
appliedUrlTree: UrlTree,
rawUrlTree: UrlTree,
skipLocationChange: boolean,
replaceUrl: boolean,
navigationId: number
}): Observable<void> {
return of (null) as any;
}
@ -645,7 +657,13 @@ export class Router {
const beforePreactivationDone$ =
urlAndSnapshot$.pipe(mergeMap((p): Observable<NavStreamValue> => {
if (typeof p === 'boolean') return of (p);
return this.hooks.beforePreactivation(p.snapshot).pipe(map(() => p));
return this.hooks
.beforePreactivation(p.snapshot, {
navigationId: id,
appliedUrlTree: url,
rawUrlTree: rawUrl, skipLocationChange, replaceUrl,
})
.pipe(map(() => p));
}));
// run preactivation: guards and data resolvers
@ -698,7 +716,13 @@ export class Router {
const preactivationDone$ =
preactivationResolveData$.pipe(mergeMap((p): Observable<NavStreamValue> => {
if (typeof p === 'boolean' || this.navigationId !== id) return of (false);
return this.hooks.afterPreactivation(p.snapshot).pipe(map(() => p));
return this.hooks
.afterPreactivation(p.snapshot, {
navigationId: id,
appliedUrlTree: url,
rawUrlTree: rawUrl, skipLocationChange, replaceUrl,
})
.pipe(map(() => p));
}));