Skip to content

Commit c2de5f6

Browse files
arturovtmmalerba
authored andcommitted
fix(common): clean up onUrlChange listener when root scope is destroyed (#60004)
This cleans up the `onUrlChange` listener once the root scope is destroyed. PR Close #60004
1 parent dd70675 commit c2de5f6

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

packages/common/upgrade/src/location_shim.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export class $locationShim {
5959

6060
private urlChanges = new ReplaySubject<{newUrl: string; newState: unknown}>(1);
6161

62+
private readonly removeOnUrlChangeFn: VoidFunction;
63+
6264
constructor(
6365
$injector: any,
6466
private location: Location,
@@ -82,7 +84,7 @@ export class $locationShim {
8284
this.cacheState();
8385
this.$$state = this.browserState();
8486

85-
this.location.onUrlChange((newUrl, newState) => {
87+
this.removeOnUrlChangeFn = this.location.onUrlChange((newUrl, newState) => {
8688
this.urlChanges.next({newUrl, newState});
8789
});
8890

@@ -249,6 +251,7 @@ export class $locationShim {
249251
});
250252

251253
$rootScope.$on('$destroy', () => {
254+
this.removeOnUrlChangeFn();
252255
// Complete the subject to release all active observers when the root
253256
// scope is destroyed. Before this change, we subscribed to the `urlChanges`
254257
// subject, and the subscriber captured `this`, leading to a memory leak

0 commit comments

Comments
 (0)