refactor(router): remove RouteEvent base class (#19043)

* Introduced with #18407, `RouteEvents` don't actually have a common constructor. Reverting here to be able to add new functionality to ChildActivation events.

PR Close #19043
This commit is contained in:
Jason Aden 2017-09-05 11:54:33 -07:00 committed by Miško Hevery
parent 1a9d382da9
commit dce36751f5
6 changed files with 57 additions and 59 deletions

View File

@ -35,35 +35,6 @@ export class RouterEvent {
public url: string) {}
}
/**
* @whatItDoes Base for events tied to a specific `Route`, as opposed to events for the Router
* lifecycle. `RouteEvent`s may be fired multiple times during a single navigation and will
* always receive the `Route` they pertain to.
*
* Example:
*
* ```
* class MyService {
* constructor(public router: Router, spinner: Spinner) {
* router.events.filter(e => e instanceof RouteEvent).subscribe(e => {
* if (e instanceof ChildActivationStart) {
* spinner.start(e.route);
* } else if (e instanceof ChildActivationEnd) {
* spinner.end(e.route);
* }
* });
* }
* }
* ```
*
* @experimental
*/
export class RouteEvent {
constructor(
/** @docsNotRequired */
public route: Route) {}
}
/**
* @whatItDoes Represents an event triggered when a navigation starts.
*
@ -265,7 +236,10 @@ export class ResolveEnd extends RouterEvent {
*
* @experimental
*/
export class RouteConfigLoadStart extends RouteEvent {
export class RouteConfigLoadStart {
constructor(
/** @docsNotRequired */
public route: Route) {}
toString(): string { return `RouteConfigLoadStart(path: ${this.route.path})`; }
}
@ -274,7 +248,10 @@ export class RouteConfigLoadStart extends RouteEvent {
*
* @experimental
*/
export class RouteConfigLoadEnd extends RouteEvent {
export class RouteConfigLoadEnd {
constructor(
/** @docsNotRequired */
public route: Route) {}
toString(): string { return `RouteConfigLoadEnd(path: ${this.route.path})`; }
}
@ -284,7 +261,10 @@ export class RouteConfigLoadEnd extends RouteEvent {
*
* @experimental
*/
export class ChildActivationStart extends RouteEvent {
export class ChildActivationStart {
constructor(
/** @docsNotRequired */
public route: Route) {}
toString(): string { return `ChildActivationStart(path: '${this.route.path}')`; }
}
@ -294,7 +274,10 @@ export class ChildActivationStart extends RouteEvent {
*
* @experimental
*/
export class ChildActivationEnd extends RouteEvent {
export class ChildActivationEnd {
constructor(
/** @docsNotRequired */
public route: Route) {}
toString(): string { return `ChildActivationEnd(path: '${this.route.path}')`; }
}
@ -319,4 +302,5 @@ export class ChildActivationEnd extends RouteEvent {
*
* @stable
*/
export type Event = RouterEvent | RouteEvent;
export type Event = RouterEvent | RouteConfigLoadStart | RouteConfigLoadEnd | ChildActivationStart |
ChildActivationEnd;

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 {ChildActivationEnd, ChildActivationStart, Event, GuardsCheckEnd, GuardsCheckStart, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RouteEvent, RoutesRecognized} from './events';
export {ChildActivationEnd, ChildActivationStart, Event, GuardsCheckEnd, GuardsCheckStart, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RouterEvent, RoutesRecognized} from './events';
export {CanActivate, CanActivateChild, CanDeactivate, CanLoad, Resolve} from './interfaces';
export {DetachedRouteHandle, RouteReuseStrategy} from './route_reuse_strategy';
export {NavigationExtras, Router} from './router';

View File

@ -19,7 +19,7 @@ import {mergeMap} from 'rxjs/operator/mergeMap';
import {reduce} from 'rxjs/operator/reduce';
import {LoadedRouterConfig, ResolveData, RunGuardsAndResolvers} from './config';
import {ChildActivationStart, RouteEvent} from './events';
import {ChildActivationStart, Event} from './events';
import {ChildrenOutletContexts, OutletContext} from './router_outlet_context';
import {ActivatedRouteSnapshot, RouterStateSnapshot, equalParamsAndUrlSegments, inheritedParamsDataResolve} from './router_state';
import {andObservables, forEach, shallowEqual, wrapIntoObservable} from './utils/collection';
@ -43,7 +43,7 @@ export class PreActivation {
constructor(
private future: RouterStateSnapshot, private curr: RouterStateSnapshot,
private moduleInjector: Injector, private forwardEvent?: (evt: RouteEvent) => void) {}
private moduleInjector: Injector, private forwardEvent?: (evt: Event) => void) {}
initalize(parentContexts: ChildrenOutletContexts): void {
const futureRoot = this.future._root;

View File

@ -21,7 +21,7 @@ import {applyRedirects} from './apply_redirects';
import {LoadedRouterConfig, QueryParamsHandling, Route, Routes, validateConfig} from './config';
import {createRouterState} from './create_router_state';
import {createUrlTree} from './create_url_tree';
import {ChildActivationEnd, Event, GuardsCheckEnd, GuardsCheckStart, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RouteEvent, RoutesRecognized} from './events';
import {ChildActivationEnd, Event, GuardsCheckEnd, GuardsCheckStart, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RouterEvent, RoutesRecognized} from './events';
import {PreActivation} from './pre_activation';
import {recognize} from './recognize';
import {DefaultRouteReuseStrategy, DetachedRouteHandleInternal, RouteReuseStrategy} from './route_reuse_strategy';
@ -628,7 +628,7 @@ export class Router {
const moduleInjector = this.ngModule.injector;
preActivation = new PreActivation(
snapshot, this.currentRouterState.snapshot, moduleInjector,
(evt: RouteEvent) => this.triggerEvent(evt));
(evt: Event) => this.triggerEvent(evt));
preActivation.initalize(this.rootContexts);
return {appliedUrl, snapshot};
});
@ -764,7 +764,7 @@ export class Router {
class ActivateRoutes {
constructor(
private routeReuseStrategy: RouteReuseStrategy, private futureState: RouterState,
private currState: RouterState, private forwardEvent: (evt: RouteEvent) => void) {}
private currState: RouterState, private forwardEvent: (evt: Event) => void) {}
activate(parentContexts: ChildrenOutletContexts): void {
const futureRoot = this.futureState._root;

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, 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 {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, RouteReuseStrategy, Router, RouterEvent, 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 => e instanceof RouteEvent || recordedEvents.push(e));
router.events.forEach(e => e instanceof RouterEvent && 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 => e instanceof RouteEvent || recordedEvents.push(e));
router.events.subscribe(e => e instanceof RouterEvent && recordedEvents.push(e));
let e: any = null;
router.navigateByUrl('/simple') !.catch(error => e = error);
@ -3344,7 +3344,7 @@ describe('Integration', () => {
}]);
const events: any[] = [];
router.events.subscribe(e => e instanceof RouteEvent || events.push(e));
router.events.subscribe(e => e instanceof RouterEvent && events.push(e));
// supported URL
router.navigateByUrl('/include/user/kate');
@ -3408,7 +3408,7 @@ describe('Integration', () => {
}]);
const events: any[] = [];
router.events.subscribe(e => e instanceof RouteEvent || events.push(e));
router.events.subscribe(e => e instanceof RouterEvent && events.push(e));
location.go('/include/user/kate(aux:excluded)');
advance(fixture);

View File

@ -60,12 +60,18 @@ export interface CanLoad {
}
/** @experimental */
export declare class ChildActivationEnd extends RouteEvent {
export declare class ChildActivationEnd {
route: Route;
constructor(
route: Route);
toString(): string;
}
/** @experimental */
export declare class ChildActivationStart extends RouteEvent {
export declare class ChildActivationStart {
route: Route;
constructor(
route: Route);
toString(): string;
}
@ -97,7 +103,7 @@ export declare class DefaultUrlSerializer implements UrlSerializer {
export declare type DetachedRouteHandle = {};
/** @stable */
export declare type Event = RouterEvent | RouteEvent;
export declare type Event = RouterEvent | RouteConfigLoadStart | RouteConfigLoadEnd | ChildActivationStart | ChildActivationEnd;
/** @stable */
export interface ExtraOptions {
@ -284,20 +290,19 @@ export interface Route {
}
/** @experimental */
export declare class RouteConfigLoadEnd extends RouteEvent {
toString(): string;
}
/** @experimental */
export declare class RouteConfigLoadStart extends RouteEvent {
toString(): string;
}
/** @experimental */
export declare class RouteEvent {
export declare class RouteConfigLoadEnd {
route: Route;
constructor(
route: Route);
toString(): string;
}
/** @experimental */
export declare class RouteConfigLoadStart {
route: Route;
constructor(
route: Route);
toString(): string;
}
/** @stable */
@ -339,6 +344,15 @@ export declare abstract class RouteReuseStrategy {
abstract store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle | null): void;
}
/** @experimental */
export declare class RouterEvent {
id: number;
url: string;
constructor(
id: number,
url: string);
}
/** @stable */
export declare class RouterLink {
fragment: string;