Skip to content

Commit c733dd4

Browse files
committed
docs: inject services with recommended API and use takeUntilDestroyed
- Updated service injection to use readonly fields. - Replaced manual subscription tracking with `takeUntilDestroyed`, removing the need for `OnDestroy` cleanup.
1 parent b5f1f88 commit c733dd4

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

docs/src/app/shared/theme-picker/theme-picker.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {
1010
ChangeDetectionStrategy,
1111
Component,
12-
OnDestroy,
12+
DestroyRef,
1313
OnInit,
1414
ViewEncapsulation,
1515
inject,
@@ -21,7 +21,7 @@ import {MatIcon} from '@angular/material/icon';
2121
import {MatMenu, MatMenuItem, MatMenuTrigger} from '@angular/material/menu';
2222
import {MatTooltip} from '@angular/material/tooltip';
2323
import {ActivatedRoute, ParamMap} from '@angular/router';
24-
import {Subscription} from 'rxjs';
24+
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
2525
import {map} from 'rxjs/operators';
2626
import {LiveAnnouncer} from '@angular/cdk/a11y';
2727

@@ -33,13 +33,13 @@ import {LiveAnnouncer} from '@angular/cdk/a11y';
3333
encapsulation: ViewEncapsulation.None,
3434
imports: [MatIconButton, MatTooltip, MatMenu, MatMenuItem, MatMenuTrigger, MatIcon],
3535
})
36-
export class ThemePicker implements OnInit, OnDestroy {
37-
styleManager = inject(StyleManager);
38-
private _themeStorage = inject(ThemeStorage);
39-
private _activatedRoute = inject(ActivatedRoute);
40-
private _liveAnnouncer = inject(LiveAnnouncer);
36+
export class ThemePicker implements OnInit {
37+
readonly styleManager = inject(StyleManager);
38+
private readonly _themeStorage = inject(ThemeStorage);
39+
private readonly _activatedRoute = inject(ActivatedRoute);
40+
private readonly _liveAnnouncer = inject(LiveAnnouncer);
41+
private readonly _destroyRef = inject(DestroyRef);
4142

42-
private _queryParamSubscription = Subscription.EMPTY;
4343
currentTheme: DocsSiteTheme | undefined;
4444

4545
// The below colors need to align with the themes defined in theme-picker.scss
@@ -85,19 +85,18 @@ export class ThemePicker implements OnInit, OnDestroy {
8585
}
8686

8787
ngOnInit() {
88-
this._queryParamSubscription = this._activatedRoute.queryParamMap
89-
.pipe(map((params: ParamMap) => params.get('theme')))
88+
this._activatedRoute.queryParamMap
89+
.pipe(
90+
map((params: ParamMap) => params.get('theme')),
91+
takeUntilDestroyed(this._destroyRef),
92+
)
9093
.subscribe((themeName: string | null) => {
9194
if (themeName) {
9295
this.selectTheme(themeName);
9396
}
9497
});
9598
}
9699

97-
ngOnDestroy() {
98-
this._queryParamSubscription.unsubscribe();
99-
}
100-
101100
selectTheme(themeName: string) {
102101
const theme =
103102
this.themes.find(currentTheme => currentTheme.name === themeName) ||

0 commit comments

Comments
 (0)