Skip to content

Commit 0864726

Browse files
LayZeeDKatscott
authored andcommitted
fix(common): prevent duplicate URL change notifications (#37459)
Prevent duplicate notifications from being emitted when multiple URL change listeners are registered using SpyLocation#onUrlChange. Use `@internal` annotation for the `_urlChangeSubscription` properties instead of the `private` access modifier. Otherwise, we get in trouble because of `SpyLocation implements Location`. PR Close #37459
1 parent 215d50d commit 0864726

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

packages/common/src/location/location.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export class Location {
6464
_platformLocation: PlatformLocation;
6565
/** @internal */
6666
_urlChangeListeners: ((url: string, state: unknown) => void)[] = [];
67-
private _urlChangeSubscription?: SubscriptionLike;
67+
/** @internal */
68+
_urlChangeSubscription?: SubscriptionLike;
6869

6970
constructor(platformStrategy: LocationStrategy, platformLocation: PlatformLocation) {
7071
this._platformStrategy = platformStrategy;

packages/common/testing/src/location_mock.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export class SpyLocation implements Location {
3030
_platformLocation: PlatformLocation = null!;
3131
/** @internal */
3232
_urlChangeListeners: ((url: string, state: unknown) => void)[] = [];
33+
/** @internal */
34+
_urlChangeSubscription?: SubscriptionLike;
3335

3436
setInitialPath(url: string) {
3537
this._history[this._historyIndex].path = url;
@@ -123,9 +125,12 @@ export class SpyLocation implements Location {
123125
}
124126
onUrlChange(fn: (url: string, state: unknown) => void) {
125127
this._urlChangeListeners.push(fn);
126-
this.subscribe(v => {
127-
this._notifyUrlChangeListeners(v.url, v.state);
128-
});
128+
129+
if (!this._urlChangeSubscription) {
130+
this._urlChangeSubscription = this.subscribe(v => {
131+
this._notifyUrlChangeListeners(v.url, v.state);
132+
});
133+
}
129134
}
130135

131136
/** @internal */

0 commit comments

Comments
 (0)