feat(router): add events tracking activation of individual routes

* Adds `ChildActivationStart` and `ChildActivationEnd`
* Adds test to verify the PreActivation phase of routing
This commit is contained in:
Jason Aden
2017-08-01 10:44:00 -07:00
committed by Alex Rickabaugh
parent 82b067fc40
commit 49cd8513e4
11 changed files with 906 additions and 462 deletions
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -11,7 +11,7 @@ export {Data, LoadChildren, LoadChildrenCallback, ResolveData, Route, Routes, Ru
export {RouterLink, RouterLinkWithHref} from './directives/router_link';
export {RouterLinkActive} from './directives/router_link_active';
export {RouterOutlet} from './directives/router_outlet';
export {Event, GuardsCheckEnd, GuardsCheckStart, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RoutesRecognized} from './events';
export {ChildActivationEnd, ChildActivationStart, Event, GuardsCheckEnd, GuardsCheckStart, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RouteEvent, RoutesRecognized} from './events';
export {CanActivate, CanActivateChild, CanDeactivate, CanLoad, Resolve} from './interfaces';
export {DetachedRouteHandle, RouteReuseStrategy} from './route_reuse_strategy';
export {NavigationExtras, Router} from './router';
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
-1
View File
@@ -6,7 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Route, UrlMatchResult} from './config';
import {UrlSegment, UrlSegmentGroup} from './url_tree';
+13
View File
@@ -41,14 +41,23 @@ export function shallowEqual(a: {[x: string]: any}, b: {[x: string]: any}): bool
return true;
}
/**
* Flattens single-level nested arrays.
*/
export function flatten<T>(arr: T[][]): T[] {
return Array.prototype.concat.apply([], arr);
}
/**
* Return the last element of an array.
*/
export function last<T>(a: T[]): T|null {
return a.length > 0 ? a[a.length - 1] : null;
}
/**
* Verifys all booleans in an array are `true`.
*/
export function and(bools: boolean[]): boolean {
return !bools.some(v => !v);
}
@@ -85,6 +94,10 @@ export function waitForMap<A, B>(
return map.call(last$, () => res);
}
/**
* ANDs Observables by merging all input observables, reducing to an Observable verifying all
* input Observables return `true`.
*/
export function andObservables(observables: Observable<Observable<any>>): Observable<boolean> {
const merged$ = mergeAll.call(observables);
return every.call(merged$, (result: any) => result === true);
+11
View File
@@ -87,4 +87,15 @@ export class TreeNode<T> {
constructor(public value: T, public children: TreeNode<T>[]) {}
toString(): string { return `TreeNode(${this.value})`; }
}
// Return the list of T indexed by outlet name
export function nodeChildrenAsMap<T extends{outlet: string}>(node: TreeNode<T>| null) {
const map: {[outlet: string]: TreeNode<T>} = {};
if (node) {
node.children.forEach(child => map[child.value.outlet] = child);
}
return map;
}
+50
View File
@@ -0,0 +1,50 @@
/**
* @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 {Type} from '@angular/core';
import {Data, ResolveData, Route} from '../src/config';
import {ActivatedRouteSnapshot} from '../src/router_state';
import {PRIMARY_OUTLET, ParamMap, Params, convertToParamMap} from '../src/shared';
import {UrlSegment, UrlSegmentGroup, UrlTree, equalSegments} from '../src/url_tree';
export class Logger {
logs: string[] = [];
add(thing: string) { this.logs.push(thing); }
empty() { this.logs.length = 0; }
}
export function provideTokenLogger(token: string, returnValue = true) {
return {
provide: token,
useFactory: (logger: Logger) => () => (logger.add(token), returnValue),
deps: [Logger]
};
};
export declare type ARSArgs = {
url?: UrlSegment[],
params?: Params,
queryParams?: Params,
fragment?: string,
data?: Data,
outlet?: string,
component: Type<any>| string | null,
routeConfig?: Route | null,
urlSegment?: UrlSegmentGroup,
lastPathIndex?: number,
resolve?: ResolveData
};
export function createActivatedRouteSnapshot(args: ARSArgs): ActivatedRouteSnapshot {
return new ActivatedRouteSnapshot(
args.url || <any>[], args.params || {}, args.queryParams || <any>null,
args.fragment || <any>null, args.data || <any>null, args.outlet || <any>null,
<any>args.component, args.routeConfig || <any>{}, args.urlSegment || <any>null,
args.lastPathIndex || -1, args.resolve || {});
}
+7 -5
View File
@@ -11,7 +11,7 @@ import {ChangeDetectionStrategy, Component, Injectable, NgModule, NgModuleFactor
import {ComponentFixture, TestBed, fakeAsync, inject, tick} from '@angular/core/testing';
import {By} from '@angular/platform-browser/src/dom/debug/by';
import {expect} from '@angular/platform-browser/testing/src/matchers';
import {ActivatedRoute, ActivatedRouteSnapshot, CanActivate, CanDeactivate, DetachedRouteHandle, Event, GuardsCheckEnd, GuardsCheckStart, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, PRIMARY_OUTLET, ParamMap, Params, PreloadAllModules, PreloadingStrategy, Resolve, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RouteReuseStrategy, Router, RouterModule, RouterPreloader, RouterStateSnapshot, RoutesRecognized, RunGuardsAndResolvers, UrlHandlingStrategy, UrlSegmentGroup, UrlTree} from '@angular/router';
import {ActivatedRoute, ActivatedRouteSnapshot, CanActivate, CanDeactivate, ChildActivationEnd, ChildActivationStart, DetachedRouteHandle, Event, GuardsCheckEnd, GuardsCheckStart, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, PRIMARY_OUTLET, ParamMap, Params, PreloadAllModules, PreloadingStrategy, Resolve, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RouteEvent, RouteReuseStrategy, Router, RouterModule, RouterPreloader, RouterStateSnapshot, RoutesRecognized, RunGuardsAndResolvers, UrlHandlingStrategy, UrlSegmentGroup, UrlTree} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {Observer} from 'rxjs/Observer';
import {of } from 'rxjs/observable/of';
@@ -428,7 +428,7 @@ describe('Integration', () => {
}]);
const recordedEvents: any[] = [];
router.events.forEach(e => recordedEvents.push(e));
router.events.forEach(e => e instanceof RouteEvent || recordedEvents.push(e));
router.navigateByUrl('/team/22/user/victor');
advance(fixture);
@@ -986,7 +986,7 @@ describe('Integration', () => {
[{path: 'simple', component: SimpleCmp, resolve: {error: 'resolveError'}}]);
const recordedEvents: any[] = [];
router.events.subscribe(e => recordedEvents.push(e));
router.events.subscribe(e => e instanceof RouteEvent || recordedEvents.push(e));
let e: any = null;
router.navigateByUrl('/simple') !.catch(error => e = error);
@@ -2388,9 +2388,11 @@ describe('Integration', () => {
[RouteConfigLoadEnd],
[RoutesRecognized, '/lazyTrue/loaded'],
[GuardsCheckStart, '/lazyTrue/loaded'],
[ChildActivationStart],
[GuardsCheckEnd, '/lazyTrue/loaded'],
[ResolveStart, '/lazyTrue/loaded'],
[ResolveEnd, '/lazyTrue/loaded'],
[ChildActivationEnd],
[NavigationEnd, '/lazyTrue/loaded'],
]);
})));
@@ -3342,7 +3344,7 @@ describe('Integration', () => {
}]);
const events: any[] = [];
router.events.subscribe(e => events.push(e));
router.events.subscribe(e => e instanceof RouteEvent || events.push(e));
// supported URL
router.navigateByUrl('/include/user/kate');
@@ -3406,7 +3408,7 @@ describe('Integration', () => {
}]);
const events: any[] = [];
router.events.subscribe(e => events.push(e));
router.events.subscribe(e => e instanceof RouteEvent || events.push(e));
location.go('/include/user/kate(aux:excluded)');
advance(fixture);
File diff suppressed because it is too large Load Diff
+29 -37
View File
@@ -59,6 +59,16 @@ export interface CanLoad {
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean;
}
/** @experimental */
export declare class ChildActivationEnd extends RouteEvent {
toString(): string;
}
/** @experimental */
export declare class ChildActivationStart extends RouteEvent {
toString(): string;
}
/** @stable */
export declare class ChildrenOutletContexts {
getContext(childName: string): OutletContext | null;
@@ -87,7 +97,7 @@ export declare class DefaultUrlSerializer implements UrlSerializer {
export declare type DetachedRouteHandle = {};
/** @stable */
export declare type Event = NavigationStart | NavigationEnd | NavigationCancel | NavigationError | RoutesRecognized | RouteConfigLoadStart | RouteConfigLoadEnd | GuardsCheckStart | GuardsCheckEnd | ResolveStart | ResolveEnd;
export declare type Event = RouterEvent | RouteEvent;
/** @stable */
export interface ExtraOptions {
@@ -99,11 +109,9 @@ export interface ExtraOptions {
}
/** @experimental */
export declare class GuardsCheckEnd {
id: number;
export declare class GuardsCheckEnd extends RouterEvent {
shouldActivate: boolean;
state: RouterStateSnapshot;
url: string;
urlAfterRedirects: string;
constructor(
id: number,
@@ -115,10 +123,8 @@ export declare class GuardsCheckEnd {
}
/** @experimental */
export declare class GuardsCheckStart {
id: number;
export declare class GuardsCheckStart extends RouterEvent {
state: RouterStateSnapshot;
url: string;
urlAfterRedirects: string;
constructor(
id: number,
@@ -135,10 +141,8 @@ export declare type LoadChildren = string | LoadChildrenCallback;
export declare type LoadChildrenCallback = () => Type<any> | NgModuleFactory<any> | Promise<Type<any>> | Observable<Type<any>>;
/** @stable */
export declare class NavigationCancel {
id: number;
export declare class NavigationCancel extends RouterEvent {
reason: string;
url: string;
constructor(
id: number,
url: string,
@@ -147,9 +151,7 @@ export declare class NavigationCancel {
}
/** @stable */
export declare class NavigationEnd {
id: number;
url: string;
export declare class NavigationEnd extends RouterEvent {
urlAfterRedirects: string;
constructor(
id: number,
@@ -159,10 +161,8 @@ export declare class NavigationEnd {
}
/** @stable */
export declare class NavigationError {
export declare class NavigationError extends RouterEvent {
error: any;
id: number;
url: string;
constructor(
id: number,
url: string,
@@ -183,12 +183,7 @@ export interface NavigationExtras {
}
/** @stable */
export declare class NavigationStart {
id: number;
url: string;
constructor(
id: number,
url: string);
export declare class NavigationStart extends RouterEvent {
toString(): string;
}
@@ -246,10 +241,8 @@ export declare type ResolveData = {
};
/** @experimental */
export declare class ResolveEnd {
id: number;
export declare class ResolveEnd extends RouterEvent {
state: RouterStateSnapshot;
url: string;
urlAfterRedirects: string;
constructor(
id: number,
@@ -260,10 +253,8 @@ export declare class ResolveEnd {
}
/** @experimental */
export declare class ResolveStart {
id: number;
export declare class ResolveStart extends RouterEvent {
state: RouterStateSnapshot;
url: string;
urlAfterRedirects: string;
constructor(
id: number,
@@ -293,19 +284,22 @@ export interface Route {
}
/** @experimental */
export declare class RouteConfigLoadEnd {
route: Route;
constructor(route: Route);
export declare class RouteConfigLoadEnd extends RouteEvent {
toString(): string;
}
/** @experimental */
export declare class RouteConfigLoadStart {
route: Route;
constructor(route: Route);
export declare class RouteConfigLoadStart extends RouteEvent {
toString(): string;
}
/** @experimental */
export declare class RouteEvent {
route: Route;
constructor(
route: Route);
}
/** @stable */
export declare class Router {
config: Routes;
@@ -453,10 +447,8 @@ export declare type Routes = Route[];
export declare const ROUTES: InjectionToken<Route[][]>;
/** @stable */
export declare class RoutesRecognized {
id: number;
export declare class RoutesRecognized extends RouterEvent {
state: RouterStateSnapshot;
url: string;
urlAfterRedirects: string;
constructor(
id: number,