Skip to content

Commit f7c55f3

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 f7c55f3

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

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

Lines changed: 16 additions & 17 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+
public 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
@@ -72,7 +72,7 @@ export class ThemePicker implements OnInit, OnDestroy {
7272
];
7373

7474
constructor() {
75-
const themeName = this._themeStorage.getStoredThemeName();
75+
const themeName = this.themeStorage.getStoredThemeName();
7676
if (themeName) {
7777
this.selectTheme(themeName);
7878
} else {
@@ -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) ||
@@ -112,8 +111,8 @@ export class ThemePicker implements OnInit, OnDestroy {
112111
}
113112

114113
if (this.currentTheme) {
115-
this._liveAnnouncer.announce(`${theme.displayName} theme selected.`, 'polite', 3000);
116-
this._themeStorage.storeTheme(this.currentTheme);
114+
this.liveAnnouncer.announce(`${theme.displayName} theme selected.`, 'polite', 3000);
115+
this.themeStorage.storeTheme(this.currentTheme);
117116
}
118117
}
119118
}

0 commit comments

Comments
 (0)