Skip to content

Commit a5456ab

Browse files
MeAkibmmalerba
authored andcommitted
docs: inject services with recommended API and use takeUntilDestroyed (#31915)
- Updated service injection to use readonly fields. - Replaced manual subscription tracking with `takeUntilDestroyed`, removing the need for `OnDestroy` cleanup. (cherry picked from commit faf1ef0)
1 parent 6270efe commit a5456ab

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

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

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

9-
import {
10-
ChangeDetectionStrategy,
11-
Component,
12-
OnDestroy,
13-
OnInit,
14-
ViewEncapsulation,
15-
inject,
16-
} from '@angular/core';
9+
import {ChangeDetectionStrategy, Component, ViewEncapsulation, inject} from '@angular/core';
1710
import {StyleManager} from '../style-manager';
1811
import {DocsSiteTheme, ThemeStorage} from './theme-storage/theme-storage';
1912
import {MatIconButton} from '@angular/material/button';
2013
import {MatIcon} from '@angular/material/icon';
2114
import {MatMenu, MatMenuItem, MatMenuTrigger} from '@angular/material/menu';
2215
import {MatTooltip} from '@angular/material/tooltip';
2316
import {ActivatedRoute, ParamMap} from '@angular/router';
24-
import {Subscription} from 'rxjs';
17+
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
2518
import {map} from 'rxjs/operators';
2619
import {LiveAnnouncer} from '@angular/cdk/a11y';
2720

@@ -33,13 +26,12 @@ import {LiveAnnouncer} from '@angular/cdk/a11y';
3326
encapsulation: ViewEncapsulation.None,
3427
imports: [MatIconButton, MatTooltip, MatMenu, MatMenuItem, MatMenuTrigger, MatIcon],
3528
})
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);
29+
export class ThemePicker {
30+
readonly styleManager = inject(StyleManager);
31+
private readonly _themeStorage = inject(ThemeStorage);
32+
private readonly _activatedRoute = inject(ActivatedRoute);
33+
private readonly _liveAnnouncer = inject(LiveAnnouncer);
4134

42-
private _queryParamSubscription = Subscription.EMPTY;
4335
currentTheme: DocsSiteTheme | undefined;
4436

4537
// The below colors need to align with the themes defined in theme-picker.scss
@@ -72,6 +64,17 @@ export class ThemePicker implements OnInit, OnDestroy {
7264
];
7365

7466
constructor() {
67+
this._activatedRoute.queryParamMap
68+
.pipe(
69+
map((params: ParamMap) => params.get('theme')),
70+
takeUntilDestroyed(),
71+
)
72+
.subscribe((themeName: string | null) => {
73+
if (themeName) {
74+
this.selectTheme(themeName);
75+
}
76+
});
77+
7578
const themeName = this._themeStorage.getStoredThemeName();
7679
if (themeName) {
7780
this.selectTheme(themeName);
@@ -84,20 +87,6 @@ export class ThemePicker implements OnInit, OnDestroy {
8487
}
8588
}
8689

87-
ngOnInit() {
88-
this._queryParamSubscription = this._activatedRoute.queryParamMap
89-
.pipe(map((params: ParamMap) => params.get('theme')))
90-
.subscribe((themeName: string | null) => {
91-
if (themeName) {
92-
this.selectTheme(themeName);
93-
}
94-
});
95-
}
96-
97-
ngOnDestroy() {
98-
this._queryParamSubscription.unsubscribe();
99-
}
100-
10190
selectTheme(themeName: string) {
10291
const theme =
10392
this.themes.find(currentTheme => currentTheme.name === themeName) ||

0 commit comments

Comments
 (0)