I'm calling method loadParticipant two times in two separated components, it's going almost in the same time, despite saving the Observable in the variable, api is called multiple times. This solution worked in AngularJs and Promises calls, but of course now I want use Observables :)
I tried to add debouncetime and share parameters after map
return (this.loadParticipantObservable = this.sharedEndpoints.getParticipant().map((response) => {
this.loadParticipantObservable = null;
return (this.participant = response);
})).share().debounceTime(1000);
actual code of loadParticipant method
public loadParticipant(): Observable<DTO<PersonModel>> {
if (this.participant) {
return new BehaviorSubject<DTO<PersonModel>>(this.participant);
}
if (this.loadParticipantObservable) {
return this.loadParticipantObservable;
}
return (this.loadParticipantObservable =
this.sharedEndpoints.getParticipant().map((response) => {
this.loadParticipantObservable = null;
return (this.participant = response);
}));
}
example How I call loadParticipant Method
this.sharedService.loadParticipant().subscribe((response: DTO<PersonModel>) => {
...
});
In the meantime, the Observable isn't null, code is coming into this condition and executes
return this.loadParticipantObservable;
I checked it into debugger.
This is return me only observable but unfortunately this code calls the code lower this return what calls real endpoint from backend