In my data service, I have a mechanism that initiates http calls periodically and on demand by other services. The challenge is that these services operate asynchronously, creating the possibility of overlapping http calls where a new call is made before the previous one completes.
I am exploring the use of rxjs to determine if there is an ongoing call when a new http call needs to be made.
Data Service:
constructor(private http: HttpClient){}
// If a request is made while another is in progress, wait for it to finish before triggering the next call
public request(method, url, options): Observable<any>{
return this.http.request(method, url, options);
}
Service A:
public syncA(){
setTimeout(() => {
this.dataService.request('GET', 'someUrl', someOptions).subscribe((response) => {
console.log('periodic call returns ', response});
}, 45000);
}
Service B:
public doB(): Observable<any>{
return this.dataService.request('GET', 'someUrl', someOptions)
}
The potential issue arises when service B calls doB while syncA has initiated a request that is still pending.