fix(common): prevent duplicate URL change notifications (#37404)

Prevent duplicate notifications from being emitted when multiple URL change listeners are registered using Location#onUrlChange.

PR Close #37404
This commit is contained in:
Lars Gyrup Brink Nielsen
2020-06-02 23:05:39 +02:00
committed by atscott
parent eb6ba9ac80
commit 3569fdf451
2 changed files with 34 additions and 5 deletions

View File

@ -64,6 +64,7 @@ export class Location {
_platformLocation: PlatformLocation;
/** @internal */
_urlChangeListeners: ((url: string, state: unknown) => void)[] = [];
private _urlChangeSubscription?: SubscriptionLike;
constructor(platformStrategy: LocationStrategy, platformLocation: PlatformLocation) {
this._platformStrategy = platformStrategy;
@ -194,9 +195,12 @@ export class 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 */