Skip to content

Commit d996cae

Browse files
committed
fix: race condition by using async pipe
1 parent 8ce5402 commit d996cae

File tree

4 files changed

+859
-824
lines changed

4 files changed

+859
-824
lines changed

apps/rxjs/14-race-condition/project.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@
7878
"coverage": true
7979
}
8080
}
81+
},
82+
"component-test": {
83+
"executor": "@nx/cypress:cypress",
84+
"options": {
85+
"cypressConfig": "cypress.config.ts",
86+
"testingType": "component",
87+
"skipServe:": true,
88+
"devServerTarget": "rxjs-race-condition:build"
89+
}
8190
}
8291
}
8392
}
Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
import {
2-
ChangeDetectionStrategy,
3-
Component,
4-
inject,
5-
OnInit,
6-
} from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
72
import { MatDialog } from '@angular/material/dialog';
8-
import { take } from 'rxjs';
93
import { TopicModalComponent } from './topic-dialog.component';
10-
import { TopicService, TopicType } from './topic.service';
114

125
@Component({
136
standalone: true,
@@ -17,24 +10,11 @@ import { TopicService, TopicType } from './topic.service';
1710
`,
1811
changeDetection: ChangeDetectionStrategy.OnPush,
1912
})
20-
export class AppComponent implements OnInit {
13+
export class AppComponent {
2114
title = 'rxjs-race-condition';
2215
dialog = inject(MatDialog);
23-
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-
}
3216

3317
openTopicModal() {
34-
this.dialog.open(TopicModalComponent, {
35-
data: {
36-
topics: this.topics,
37-
},
38-
});
18+
this.dialog.open(TopicModalComponent);
3919
}
4020
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { NgFor } from '@angular/common';
1+
import { AsyncPipe, NgFor } from '@angular/common';
22
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
33
import { MatButtonModule } from '@angular/material/button';
4-
import { MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
4+
import { MatDialogModule } from '@angular/material/dialog';
5+
import { take } from 'rxjs';
6+
import { TopicService } from './topic.service';
57

68
@Component({
79
template: `
810
<h1 mat-dialog-title>Show all Topics</h1>
911
<div mat-dialog-content>
1012
<ul>
11-
<li *ngFor="let topic of data.topics">
13+
<li *ngFor="let topic of topics$ | async">
1214
{{ topic }}
1315
</li>
1416
</ul>
@@ -17,9 +19,10 @@ import { MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
1719
<button mat-button mat-dialog-close>Close</button>
1820
</div>
1921
`,
20-
imports: [MatDialogModule, MatButtonModule, NgFor],
22+
imports: [MatDialogModule, MatButtonModule, NgFor, AsyncPipe],
2123
changeDetection: ChangeDetectionStrategy.OnPush,
2224
})
2325
export class TopicModalComponent {
24-
data = inject(MAT_DIALOG_DATA);
26+
topicService = inject(TopicService);
27+
topics$ = this.topicService.fakeGetHttpTopic().pipe(take(1));
2528
}

0 commit comments

Comments
 (0)