Skip to content

Commit 41e2a68

Browse files
clydindylhunn
authored andcommitted
feat(router): add type properties to all router events (#44189)
All router events now have a `type` property with a string name identifying the specific type of the event. The `type` property can be used to perform TypeScript type narrowing using an `if` statement, `switch` statement, or similar. This removes the need to perform `instanceof` checks but still allows them if preferred. An `instanceof` check requires the use of the event class value which may not be available or preferred in certain situations. The Router's `enableTracing` option is also now guarded with the `ngDevMode` flag which combined with the use of a new `stringifyEvent` function allows the tree-shaking of the string generation code for each router event class. However, the original `toString` class methods are still present to prevent a potential breaking change. As a result, full benefits of the tree-shaking potential will not be realized until they are removed. PR Close #44189
1 parent ea2b3a9 commit 41e2a68

File tree

4 files changed

+190
-11
lines changed

4 files changed

+190
-11
lines changed

goldens/public-api/router/index.md

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ export class ActivationEnd {
8686
snapshot: ActivatedRouteSnapshot;
8787
// (undocumented)
8888
toString(): string;
89+
// (undocumented)
90+
readonly type = EventType.ActivationEnd;
8991
}
9092

9193
// @public
@@ -96,6 +98,8 @@ export class ActivationStart {
9698
snapshot: ActivatedRouteSnapshot;
9799
// (undocumented)
98100
toString(): string;
101+
// (undocumented)
102+
readonly type = EventType.ActivationStart;
99103
}
100104

101105
// @public
@@ -139,6 +143,8 @@ export class ChildActivationEnd {
139143
snapshot: ActivatedRouteSnapshot;
140144
// (undocumented)
141145
toString(): string;
146+
// (undocumented)
147+
readonly type = EventType.ChildActivationEnd;
142148
}
143149

144150
// @public
@@ -149,6 +155,8 @@ export class ChildActivationStart {
149155
snapshot: ActivatedRouteSnapshot;
150156
// (undocumented)
151157
toString(): string;
158+
// (undocumented)
159+
readonly type = EventType.ChildActivationStart;
152160
}
153161

154162
// @public
@@ -194,9 +202,45 @@ export class DefaultUrlSerializer implements UrlSerializer {
194202
export type DetachedRouteHandle = {};
195203

196204
// @public
197-
type Event_2 = RouterEvent | RouteConfigLoadStart | RouteConfigLoadEnd | ChildActivationStart | ChildActivationEnd | ActivationStart | ActivationEnd | Scroll;
205+
type Event_2 = RouterEvent | NavigationStart | NavigationEnd | NavigationCancel | NavigationError | RoutesRecognized | GuardsCheckStart | GuardsCheckEnd | RouteConfigLoadStart | RouteConfigLoadEnd | ChildActivationStart | ChildActivationEnd | ActivationStart | ActivationEnd | Scroll | ResolveStart | ResolveEnd;
198206
export { Event_2 as Event }
199207

208+
// @public
209+
export const enum EventType {
210+
// (undocumented)
211+
ActivationEnd = 14,
212+
// (undocumented)
213+
ActivationStart = 13,
214+
// (undocumented)
215+
ChildActivationEnd = 12,
216+
// (undocumented)
217+
ChildActivationStart = 11,
218+
// (undocumented)
219+
GuardsCheckEnd = 8,
220+
// (undocumented)
221+
GuardsCheckStart = 7,
222+
// (undocumented)
223+
NavigationCancel = 2,
224+
// (undocumented)
225+
NavigationEnd = 1,
226+
// (undocumented)
227+
NavigationError = 3,
228+
// (undocumented)
229+
NavigationStart = 0,
230+
// (undocumented)
231+
ResolveEnd = 6,
232+
// (undocumented)
233+
ResolveStart = 5,
234+
// (undocumented)
235+
RouteConfigLoadEnd = 10,
236+
// (undocumented)
237+
RouteConfigLoadStart = 9,
238+
// (undocumented)
239+
RoutesRecognized = 4,
240+
// (undocumented)
241+
Scroll = 15
242+
}
243+
200244
// @public
201245
export interface ExtraOptions {
202246
anchorScrolling?: 'disabled' | 'enabled';
@@ -231,6 +275,8 @@ export class GuardsCheckEnd extends RouterEvent {
231275
// (undocumented)
232276
toString(): string;
233277
// (undocumented)
278+
readonly type = EventType.GuardsCheckEnd;
279+
// (undocumented)
234280
urlAfterRedirects: string;
235281
}
236282

@@ -246,6 +292,8 @@ export class GuardsCheckStart extends RouterEvent {
246292
// (undocumented)
247293
toString(): string;
248294
// (undocumented)
295+
readonly type = EventType.GuardsCheckStart;
296+
// (undocumented)
249297
urlAfterRedirects: string;
250298
}
251299

@@ -296,6 +344,8 @@ export class NavigationCancel extends RouterEvent {
296344
reason: string;
297345
// (undocumented)
298346
toString(): string;
347+
// (undocumented)
348+
readonly type = EventType.NavigationCancel;
299349
}
300350

301351
// @public
@@ -307,6 +357,8 @@ export class NavigationEnd extends RouterEvent {
307357
// (undocumented)
308358
toString(): string;
309359
// (undocumented)
360+
readonly type = EventType.NavigationEnd;
361+
// (undocumented)
310362
urlAfterRedirects: string;
311363
}
312364

@@ -320,6 +372,8 @@ export class NavigationError extends RouterEvent {
320372
error: any;
321373
// (undocumented)
322374
toString(): string;
375+
// (undocumented)
376+
readonly type = EventType.NavigationError;
323377
}
324378

325379
// @public
@@ -331,18 +385,20 @@ export class NavigationStart extends RouterEvent {
331385
constructor(
332386
id: number,
333387
url: string,
334-
navigationTrigger?: 'imperative' | 'popstate' | 'hashchange',
388+
navigationTrigger?: NavigationTrigger,
335389
restoredState?: {
336390
[k: string]: any;
337391
navigationId: number;
338392
} | null);
339-
navigationTrigger?: 'imperative' | 'popstate' | 'hashchange';
393+
navigationTrigger?: NavigationTrigger;
340394
restoredState?: {
341395
[k: string]: any;
342396
navigationId: number;
343397
} | null;
344398
// (undocumented)
345399
toString(): string;
400+
// (undocumented)
401+
readonly type = EventType.NavigationStart;
346402
}
347403

348404
// @public
@@ -424,6 +480,8 @@ export class ResolveEnd extends RouterEvent {
424480
// (undocumented)
425481
toString(): string;
426482
// (undocumented)
483+
readonly type = EventType.ResolveEnd;
484+
// (undocumented)
427485
urlAfterRedirects: string;
428486
}
429487

@@ -439,6 +497,8 @@ export class ResolveStart extends RouterEvent {
439497
// (undocumented)
440498
toString(): string;
441499
// (undocumented)
500+
readonly type = EventType.ResolveStart;
501+
// (undocumented)
442502
urlAfterRedirects: string;
443503
}
444504

@@ -472,6 +532,8 @@ export class RouteConfigLoadEnd {
472532
route: Route;
473533
// (undocumented)
474534
toString(): string;
535+
// (undocumented)
536+
readonly type = EventType.RouteConfigLoadEnd;
475537
}
476538

477539
// @public
@@ -482,6 +544,8 @@ export class RouteConfigLoadStart {
482544
route: Route;
483545
// (undocumented)
484546
toString(): string;
547+
// (undocumented)
548+
readonly type = EventType.RouteConfigLoadStart;
485549
}
486550

487551
// @public
@@ -746,6 +810,8 @@ export class RoutesRecognized extends RouterEvent {
746810
// (undocumented)
747811
toString(): string;
748812
// (undocumented)
813+
readonly type = EventType.RoutesRecognized;
814+
// (undocumented)
749815
urlAfterRedirects: string;
750816
}
751817

@@ -766,6 +832,8 @@ export class Scroll {
766832
readonly routerEvent: NavigationEnd;
767833
// (undocumented)
768834
toString(): string;
835+
// (undocumented)
836+
readonly type = EventType.Scroll;
769837
}
770838

771839
// @public

0 commit comments

Comments
 (0)