Skip to content

Commit

Permalink
fix(router): complete router events on dispose (angular#59327)
Browse files Browse the repository at this point in the history
In this commit, we manually complete the `events` subject to remove all active observers and enable
granular garbage collection, as users may forget to unsubscribe manually when subscribing to `router.events`.

PR Close angular#59327
  • Loading branch information
arturovt authored and thePunderWoman committed Jan 7, 2025
1 parent 5685b4c commit 52a6710
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ export class Router {

/** Disposes of the router. */
dispose(): void {
// We call `unsubscribe()` to release observers, as users may forget to
// unsubscribe manually when subscribing to `router.events`. We do not call
// `complete()` because it is unsafe; if someone subscribes using the `first`
// operator and the observable completes before emitting a value,
// RxJS will throw an error.
this._events.unsubscribe();
this.navigationTransitions.complete();
if (this.nonRouterCurrentEntryChangeSubscription) {
this.nonRouterCurrentEntryChangeSubscription.unsubscribe();
Expand Down

0 comments on commit 52a6710

Please sign in to comment.