* 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)
| 224 | * @returns A function that, when executed, unregisters a URL change listener. |
| 225 | */ |
| 226 | onUrlChange(fn: (url: string, state: unknown) => void): VoidFunction { |
| 227 | this._urlChangeListeners.push(fn); |
| 228 | |
| 229 | this._urlChangeSubscription ??= this.subscribe((v) => { |
| 230 | this._notifyUrlChangeListeners(v.url, v.state); |
| 231 | }); |
| 232 | |
| 233 | return () => { |
| 234 | const fnIndex = this._urlChangeListeners.indexOf(fn); |
| 235 | this._urlChangeListeners.splice(fnIndex, 1); |
| 236 | |
| 237 | if (this._urlChangeListeners.length === 0) { |
| 238 | this._urlChangeSubscription?.unsubscribe(); |
| 239 | this._urlChangeSubscription = null; |
| 240 | } |
| 241 | }; |
| 242 | } |
| 243 | |
| 244 | /** @internal */ |
| 245 | _notifyUrlChangeListeners(url: string = '', state: unknown) { |