Skip to content

Commit

Permalink
perf: ignore non-essential router events (#306)
Browse files Browse the repository at this point in the history
# Performance optimizations
- Ignore non-essential router events when serializing the router state.
Only `NavigationStart`, `RoutesRecognized`, `NavigationEnd`,
`NavigationCancel`, and `NavigationError` events are essential.
  • Loading branch information
LayZeeDK authored Jan 3, 2023
1 parent 33606a6 commit a0216cc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { inject, Injectable, Type } from '@angular/core';
import { Data, Event as RouterEvent, Params, Router } from '@angular/router';
import {
Data,
Event as RouterEvent,
NavigationCancel,
NavigationEnd,
NavigationError,
NavigationStart,
Params,
Router,
RoutesRecognized,
} from '@angular/router';
import { ComponentStore } from '@ngrx/component-store';
import { map, Observable } from 'rxjs';
import { MinimalActivatedRouteSnapshot } from '../@ngrx/router-store/minimal-activated-route-state-snapshot';
Expand Down Expand Up @@ -72,7 +82,13 @@ export class GlobalRouterStore
});

this.#updateRouterState(
this.#router.events.pipe(
this.selectRouterEvents(
NavigationStart,
RoutesRecognized,
NavigationEnd,
NavigationCancel,
NavigationError
).pipe(
map(() => this.#serializer.serialize(this.#router.routerState.snapshot))
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import {
ActivatedRoute,
Data,
Event as RouterEvent,
NavigationCancel,
NavigationEnd,
NavigationError,
NavigationStart,
Params,
Router,
RoutesRecognized,
} from '@angular/router';
import { ComponentStore } from '@ngrx/component-store';
import { map, Observable } from 'rxjs';
Expand Down Expand Up @@ -71,7 +76,13 @@ export class LocalRouterStore
});

this.#updateRouterState(
this.#router.events.pipe(
this.selectRouterEvents(
NavigationStart,
RoutesRecognized,
NavigationEnd,
NavigationCancel,
NavigationError
).pipe(
map(() => this.#serializer.serialize(this.#router.routerState.snapshot))
)
);
Expand Down

0 comments on commit a0216cc

Please sign in to comment.