From 7edb026619eb0ae78559065e7c10b7acfedc1f82 Mon Sep 17 00:00:00 2001 From: Lars Gyrup Brink Nielsen Date: Fri, 5 Jun 2020 23:24:35 +0200 Subject: [PATCH] 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 --- packages/common/src/location/location.ts | 3 ++- packages/common/testing/src/location_mock.ts | 11 ++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/common/src/location/location.ts b/packages/common/src/location/location.ts index fbf0675cc8..55af1e738d 100644 --- a/packages/common/src/location/location.ts +++ b/packages/common/src/location/location.ts @@ -64,7 +64,8 @@ export class Location { _platformLocation: PlatformLocation; /** @internal */ _urlChangeListeners: ((url: string, state: unknown) => void)[] = []; - private _urlChangeSubscription?: SubscriptionLike; + /** @internal */ + _urlChangeSubscription?: SubscriptionLike; constructor(platformStrategy: LocationStrategy, platformLocation: PlatformLocation) { this._platformStrategy = platformStrategy; diff --git a/packages/common/testing/src/location_mock.ts b/packages/common/testing/src/location_mock.ts index 28f57c26a5..e7f5595ec9 100644 --- a/packages/common/testing/src/location_mock.ts +++ b/packages/common/testing/src/location_mock.ts @@ -30,6 +30,8 @@ export class SpyLocation implements Location { _platformLocation: PlatformLocation = null!; /** @internal */ _urlChangeListeners: ((url: string, state: unknown) => void)[] = []; + /** @internal */ + _urlChangeSubscription?: SubscriptionLike; setInitialPath(url: string) { this._history[this._historyIndex].path = url; @@ -123,9 +125,12 @@ export class SpyLocation implements Location { } onUrlChange(fn: (url: string, state: unknown) => void) { this._urlChangeListeners.push(fn); - this.subscribe(v => { - this._notifyUrlChangeListeners(v.url, v.state); - }); + + if (!this._urlChangeSubscription) { + this._urlChangeSubscription = this.subscribe(v => { + this._notifyUrlChangeListeners(v.url, v.state); + }); + } } /** @internal */