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:

committed by
atscott

parent
eb6ba9ac80
commit
3569fdf451
@ -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 */
|
||||
|
Reference in New Issue
Block a user