Skip to content

Commit 7fe1e26

Browse files
committed
docs: use takeUntilDestroyed for cleanup instead of manual subscription handling
1 parent 4564a4d commit 7fe1e26

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

docs/src/app/shared/navigation-focus/navigation-focus.service.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {Injectable, OnDestroy, inject} from '@angular/core';
9+
import {Injectable, inject} from '@angular/core';
1010
import {Event, NavigationEnd, Router} from '@angular/router';
1111
import {filter, skip} from 'rxjs/operators';
12-
import {Subscription} from 'rxjs';
12+
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
1313

1414
@Injectable({
1515
providedIn: 'root',
1616
})
17-
export class NavigationFocusService implements OnDestroy {
17+
export class NavigationFocusService {
1818
private _router = inject(Router);
19-
private _subscriptions = new Subscription();
2019
private _navigationFocusRequests: HTMLElement[] = [];
2120
private _skipLinkFocusRequests: HTMLElement[] = [];
2221
private _skipLinkHref: string | null | undefined;
@@ -27,24 +26,18 @@ export class NavigationFocusService implements OnDestroy {
2726
readonly softNavigations = this.navigationEndEvents.pipe(skip(1));
2827

2928
constructor() {
30-
this._subscriptions.add(
31-
this.softNavigations.subscribe(() => {
32-
// focus if url does not have fragment
33-
if (!this._router.url.split('#')[1]) {
34-
setTimeout(() => {
35-
if (this._navigationFocusRequests.length) {
36-
this._navigationFocusRequests[this._navigationFocusRequests.length - 1].focus({
37-
preventScroll: true,
38-
});
39-
}
40-
}, 100);
41-
}
42-
}),
43-
);
44-
}
45-
46-
ngOnDestroy() {
47-
this._subscriptions.unsubscribe();
29+
this.softNavigations.pipe(takeUntilDestroyed()).subscribe(() => {
30+
// focus if url does not have fragment
31+
if (!this._router.url.split('#')[1]) {
32+
setTimeout(() => {
33+
if (this._navigationFocusRequests.length) {
34+
this._navigationFocusRequests[this._navigationFocusRequests.length - 1].focus({
35+
preventScroll: true,
36+
});
37+
}
38+
}, 100);
39+
}
40+
});
4841
}
4942

5043
requestFocusOnNavigation(el: HTMLElement) {

0 commit comments

Comments
 (0)