* Registers a URL change listener. Use to catch updates performed by the Angular * framework that are not detectible through "popstate" or "hashchange" events. * * @param fn The change handler function, which take a URL and a location history state. * @returns A function that, when execu
(fn: (url: string, state: unknown) => void)
| 230 | * @returns A function that, when executed, unregisters a URL change listener. |
| 231 | */ |
| 232 | onUrlChange(fn: (url: string, state: unknown) => void): VoidFunction { |
| 233 | this._urlChangeListeners.push(fn); |
| 234 | |
| 235 | this._urlChangeSubscription ??= this.subscribe((v) => { |
| 236 | this._notifyUrlChangeListeners(v.url, v.state); |
| 237 | }); |
| 238 | |
| 239 | return () => { |
| 240 | const fnIndex = this._urlChangeListeners.indexOf(fn); |
| 241 | this._urlChangeListeners.splice(fnIndex, 1); |
| 242 | |
| 243 | if (this._urlChangeListeners.length === 0) { |
| 244 | this._urlChangeSubscription?.unsubscribe(); |
| 245 | this._urlChangeSubscription = null; |
| 246 | } |
| 247 | }; |
| 248 | } |
| 249 | |
| 250 | /** @internal */ |
| 251 | _notifyUrlChangeListeners(url: string = '', state: unknown) { |