I have a button witch makes a request to the server. And i'm using the switchMap operator with subject for preventing multiple pending requests.
//service
public getPhotos(): Observable<any> {
return this.http.get<any>(`url`)
}
//component
private $filterSubject: Subject<void> = new Subject();
public onFetchNewImage(): void {
this.$filterSubject
.pipe(
switchMap(event => {
return this.photoFetchService.getPhotos();
})
)
.subscribe((photo) => {
console.log('url', photo);
})
this.$filterSubject.next();
}
But, more i click, more i make multiple requests, because my subject.observables array gets bigger each time i make a request
here is my code: you can see console.log, stackblitz