|
1 | | -import { |
2 | | - ChangeDetectionStrategy, |
3 | | - Component, |
4 | | - inject, |
5 | | - OnInit, |
6 | | -} from '@angular/core'; |
| 1 | +import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; |
| 2 | +import { toSignal } from '@angular/core/rxjs-interop'; |
7 | 3 | import { MatDialog } from '@angular/material/dialog'; |
| 4 | +import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; |
8 | 5 | import { take } from 'rxjs'; |
9 | 6 | import { TopicModalComponent } from './topic-dialog.component'; |
10 | | -import { TopicService, TopicType } from './topic.service'; |
| 7 | +import { TopicService } from './topic.service'; |
11 | 8 |
|
12 | 9 | @Component({ |
13 | 10 | standalone: true, |
14 | 11 | selector: 'app-root', |
| 12 | + imports: [MatProgressSpinnerModule], |
15 | 13 | template: ` |
16 | | - <button (click)="openTopicModal()">Open Topic</button> |
| 14 | + @if (topics()) { |
| 15 | + <button (click)="openModal()">Open Topic</button> |
| 16 | + } |
17 | 17 | `, |
18 | 18 | changeDetection: ChangeDetectionStrategy.OnPush, |
19 | 19 | }) |
20 | | -export class AppComponent implements OnInit { |
| 20 | +export class AppComponent { |
21 | 21 | title = 'rxjs-race-condition'; |
22 | | - dialog = inject(MatDialog); |
23 | 22 | topicService = inject(TopicService); |
24 | | - topics: TopicType[] = []; |
| 23 | + matDialog = inject(MatDialog); |
| 24 | + topics = toSignal(this.topicService.fakeGetHttpTopic().pipe(take(1))); |
25 | 25 |
|
26 | | - ngOnInit(): void { |
27 | | - this.topicService |
28 | | - .fakeGetHttpTopic() |
29 | | - .pipe(take(1)) |
30 | | - .subscribe((topics) => (this.topics = topics)); |
31 | | - } |
32 | | - |
33 | | - openTopicModal() { |
34 | | - this.dialog.open(TopicModalComponent, { |
| 26 | + openModal(): void { |
| 27 | + this.matDialog.open(TopicModalComponent, { |
35 | 28 | data: { |
36 | | - topics: this.topics, |
| 29 | + topics: this.topics(), |
37 | 30 | }, |
38 | 31 | }); |
39 | 32 | } |
|
0 commit comments