-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ajout d'un composant de manipulation de subject pour essayer de compr…
…endre les différences
- Loading branch information
Showing
5 changed files
with
15,290 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<input type="text" [(ngModel)]="message" /> | ||
<button (click)="sendMessage()" type="submit">Ajout d'un message</button> | ||
|
||
<h2>Liste de tous les messages</h2> | ||
<table> | ||
@for (message of messages(); track $index) { | ||
<tr> | ||
<td>{{ message }}</td> | ||
</tr> | ||
} | ||
</table> | ||
|
||
<button (click)="reset()">Reset</button> | ||
|
||
<div class="card"> | ||
<h1>Subject</h1> | ||
<button (click)="subscribeToSubject()" type="button">S'abonner</button> | ||
</div> | ||
|
||
<div class="card"> | ||
<h1>ReplaySubject</h1> | ||
<button (click)="subscribeToReplaySubject()" type="button">S'abonner</button> | ||
</div> | ||
<div class="card"> | ||
<h1>BehaviorSubject</h1> | ||
<button (click)="subscribeToBehaviorSubject()" type="button"> | ||
S'abonner | ||
</button> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { SubjectComponent } from './subject.component'; | ||
|
||
describe('SubjectComponent', () => { | ||
let component: SubjectComponent; | ||
let fixture: ComponentFixture<SubjectComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [SubjectComponent], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(SubjectComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Component, model, signal } from '@angular/core'; | ||
import { AsyncPipe } from '@angular/common'; | ||
import { Subject, ReplaySubject, BehaviorSubject, takeUntil } from 'rxjs'; | ||
import { FormsModule } from '@angular/forms'; | ||
|
||
@Component({ | ||
selector: 'app-subject', | ||
standalone: true, | ||
imports: [FormsModule, AsyncPipe], | ||
templateUrl: './subject.component.html', | ||
}) | ||
export class SubjectComponent { | ||
subject = new Subject<string>(); | ||
replaySubject = new ReplaySubject<string>(); | ||
behaviorSubject = new BehaviorSubject<string>(''); | ||
|
||
resetSubject = new Subject<void>(); | ||
|
||
message = model(''); | ||
messages = signal<string[]>([]); | ||
|
||
sendMessage(): void { | ||
this.subject.next(this.message()); | ||
this.replaySubject.next(this.message()); | ||
this.behaviorSubject.next(this.message()); | ||
this.messages.update((messages) => [...messages, this.message()]); | ||
this.message.set(''); | ||
} | ||
|
||
reset(): void { | ||
this.messages.set([]); | ||
this.resetSubject.next(); | ||
this.resetSubject.complete(); | ||
this.resetSubject = new Subject<void>(); | ||
} | ||
|
||
subscribeToSubject(): void { | ||
this.subject | ||
.pipe(takeUntil(this.resetSubject)) | ||
.subscribe((message) => console.log('Subject:', message)); | ||
} | ||
|
||
subscribeToReplaySubject(): void { | ||
this.replaySubject | ||
.pipe(takeUntil(this.resetSubject)) | ||
.subscribe((message) => console.log('ReplaySubject:', message)); | ||
} | ||
|
||
subscribeToBehaviorSubject(): void { | ||
this.behaviorSubject | ||
.pipe(takeUntil(this.resetSubject)) | ||
.subscribe((message) => console.log('BehaviorSubject:', message)); | ||
} | ||
} |
Oops, something went wrong.