I have something like this in an Angular 4 app (for the sake of the example, I have removed code)
@Injectable()
export class SomeService {
constructor(
private http: Http
) {
}
get(id: number) {
return this.http.get('http://somedomain/somemodel/${id}.json');
}
}
this is used by some components to make API calls.
constructor(private someService: SomeService) {}
...
someMethod() {
// code here...
this.someService.get(2).subscribe( someHandlerFunction );
}
someOtherMethod() {
// more code here...
this.someService.get(2).subscribe( someHandlerFunction );
}
the problem is that I don't know when someMethod() and someOtherMethod() will be called. Sometimes both of them may be called and then, my API will be called twice. What I am trying to find is if there any way to change my Service to do this request only after an X amount of time. I tried to use debounce:
get(id: number) {
return this.http.get(`http://somedomain/somemodel/${id}.json`).debounceTime(10000);
}
expecting that (in this case) this HTTP get request will only be repeated after 10 seconds, if the request is made within this 10 seconds, the request won't repeat but the observable emits the last value. But this didn't work. Any tips?
PS: I know I could control this using some kind of flag, but I can't do this as long I this does not scale very good. I have multiple services with a lot of HTTP requests.
Any thoughts about this?