Skip to content

Commit 9391bce

Browse files
committed
feat: race-condition
1 parent 0d15a0b commit 9391bce

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed
Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,28 @@
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';
73
import { MatDialog } from '@angular/material/dialog';
8-
import { take } from 'rxjs';
4+
import { Subject, take } from 'rxjs';
95
import { TopicModalComponent } from './topic-dialog.component';
10-
import { TopicService, TopicType } from './topic.service';
6+
import { TopicService } from './topic.service';
117

128
@Component({
139
standalone: true,
1410
selector: 'app-root',
1511
template: `
16-
<button (click)="openTopicModal()">Open Topic</button>
12+
<button (click)="openModal$.next()">Open Topic</button>
1713
`,
1814
changeDetection: ChangeDetectionStrategy.OnPush,
1915
})
20-
export class AppComponent implements OnInit {
16+
export class AppComponent {
2117
title = 'rxjs-race-condition';
22-
dialog = inject(MatDialog);
2318
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, {
3523
data: {
36-
topics: this.topics,
24+
topics: this.topics(),
3725
},
3826
});
39-
}
27+
});
4028
}

0 commit comments

Comments
 (0)