Skip to content

Commit 4448bb7

Browse files
authored
docs: use takeUntilDestroyed in component viewer (#33876)
Clean up the subscriptions in `ComponentViewer` and `ComponentBaseView` with `takeUntilDestroyed` instead of a manual `Subject` and `ngOnDestroy`. Also remove the unused `Subscription` from `NavBar`.
1 parent ef29b7e commit 4448bb7

2 files changed

Lines changed: 20 additions & 33 deletions

File tree

‎docs/src/app/pages/component-viewer/component-viewer.ts‎

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ import {
1212
ChangeDetectorRef,
1313
Component,
1414
Directive,
15-
OnDestroy,
1615
OnInit,
1716
ViewEncapsulation,
1817
viewChild,
1918
viewChildren,
2019
inject,
20+
DestroyRef,
2121
} from '@angular/core';
22+
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
2223
import {ActivatedRoute, Router, RouterLinkActive, RouterLink, RouterOutlet} from '@angular/router';
23-
import {combineLatest, Observable, ReplaySubject, Subject} from 'rxjs';
24-
import {map, skip, switchMap, takeUntil} from 'rxjs/operators';
24+
import {combineLatest, Observable, ReplaySubject} from 'rxjs';
25+
import {map, skip, switchMap} from 'rxjs/operators';
2526
import {DocItem, DocumentationItems} from '../../shared/documentation-items/documentation-items';
2627
import {TableOfContents} from '../../shared/table-of-contents/table-of-contents';
2728

@@ -46,14 +47,14 @@ import {MatTabLink, MatTabNav, MatTabNavPanel} from '@angular/material/tabs';
4647
RouterOutlet,
4748
],
4849
})
49-
export class ComponentViewer implements OnDestroy {
50+
export class ComponentViewer {
5051
private _router = inject(Router);
5152
componentPageTitle = inject(ComponentPageTitle);
5253
readonly docItems = inject(DocumentationItems);
54+
private readonly _destroyRef = inject(DestroyRef);
5355

5456
componentDocItem = new ReplaySubject<DocItem>(1);
5557
sections: Set<string> = new Set(['overview', 'api']);
56-
private _destroyed = new Subject<void>();
5758

5859
constructor() {
5960
const route = inject(ActivatedRoute);
@@ -74,7 +75,7 @@ export class ComponentViewer implements OnDestroy {
7475
const doc = await docItems.getItemById(id, section);
7576
return {doc, section};
7677
}),
77-
takeUntil(this._destroyed),
78+
takeUntilDestroyed(this._destroyRef),
7879
)
7980
.subscribe(({doc, section}) => {
8081
if (!doc) {
@@ -98,11 +99,6 @@ export class ComponentViewer implements OnDestroy {
9899
}
99100
});
100101
}
101-
102-
ngOnDestroy(): void {
103-
this._destroyed.next();
104-
this._destroyed.complete();
105-
}
106102
}
107103

108104
/**
@@ -111,15 +107,15 @@ export class ComponentViewer implements OnDestroy {
111107
* the table of contents headers.
112108
*/
113109
@Directive()
114-
export class ComponentBaseView implements OnInit, OnDestroy {
110+
export class ComponentBaseView implements OnInit {
115111
componentViewer = inject(ComponentViewer);
116112
private _changeDetectorRef = inject(ChangeDetectorRef);
113+
private readonly _destroyRef = inject(DestroyRef);
117114

118115
readonly tableOfContents = viewChild<TableOfContents>('toc');
119116
readonly viewers = viewChildren(DocViewer);
120117

121118
showToc: Observable<boolean>;
122-
private _destroyed = new Subject<void>();
123119

124120
constructor() {
125121
const breakpointObserver = inject(BreakpointObserver);
@@ -133,14 +129,16 @@ export class ComponentBaseView implements OnInit, OnDestroy {
133129
}
134130

135131
ngOnInit() {
136-
this.componentViewer.componentDocItem.pipe(takeUntil(this._destroyed)).subscribe(() => {
137-
const tableOfContents = this.tableOfContents();
138-
if (tableOfContents) {
139-
tableOfContents.resetHeaders();
140-
}
141-
});
132+
this.componentViewer.componentDocItem
133+
.pipe(takeUntilDestroyed(this._destroyRef))
134+
.subscribe(() => {
135+
const tableOfContents = this.tableOfContents();
136+
if (tableOfContents) {
137+
tableOfContents.resetHeaders();
138+
}
139+
});
142140

143-
this.showToc.pipe(skip(1), takeUntil(this._destroyed)).subscribe(() => {
141+
this.showToc.pipe(skip(1), takeUntilDestroyed(this._destroyRef)).subscribe(() => {
144142
if (this.tableOfContents()) {
145143
this.viewers().forEach(viewer => {
146144
viewer.contentRendered.emit(viewer._elementRef.nativeElement);
@@ -149,11 +147,6 @@ export class ComponentBaseView implements OnInit, OnDestroy {
149147
});
150148
}
151149

152-
ngOnDestroy() {
153-
this._destroyed.next();
154-
this._destroyed.complete();
155-
}
156-
157150
updateTableOfContents(sectionName: string, docViewerContent: HTMLElement, sectionIndex = 0) {
158151
const tableOfContents = this.tableOfContents();
159152
if (tableOfContents) {

‎docs/src/app/shared/navbar/navbar.ts‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {ChangeDetectionStrategy, Component, OnDestroy, inject} from '@angular/core';
9+
import {ChangeDetectionStrategy, Component, inject} from '@angular/core';
1010
import {NgTemplateOutlet} from '@angular/common';
1111
import {MatButton, MatIconButton} from '@angular/material/button';
1212
import {RouterLink, RouterLinkActive} from '@angular/router';
1313

1414
import {SECTIONS} from '../documentation-items/documentation-items';
15-
import {Subscription} from 'rxjs';
1615
import {NavigationFocusService} from '../navigation-focus/navigation-focus.service';
1716
import {ThemePicker} from '../theme-picker/theme-picker';
1817
import {VersionPicker} from '../version-picker/version-picker';
@@ -36,10 +35,9 @@ const SECTIONS_KEYS = Object.keys(SECTIONS);
3635
NgTemplateOutlet,
3736
],
3837
})
39-
export class NavBar implements OnDestroy {
38+
export class NavBar {
4039
private _navigationFocusService = inject(NavigationFocusService);
4140

42-
private _subscriptions = new Subscription();
4341
isNextVersion = location.hostname === 'next.material.angular.dev';
4442
skipLinkHref: string | null | undefined;
4543
skipLinkHidden = true;
@@ -55,8 +53,4 @@ export class NavBar implements OnDestroy {
5553
get sectionKeys() {
5654
return SECTIONS_KEYS;
5755
}
58-
59-
ngOnDestroy() {
60-
this._subscriptions.unsubscribe();
61-
}
6256
}

0 commit comments

Comments
 (0)