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