Skip to content

Commit 52a6710

Browse files
arturovtthePunderWoman
authored andcommitted
fix(router): complete router events on dispose (angular#59327)
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
1 parent 5685b4c commit 52a6710

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

packages/router/src/router.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@ export class Router {
377377

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

0 commit comments

Comments
 (0)