I am trying to create continuous polling using rxjs and angular. Below is an implementation of my requirement.
https://stackblitz.com/edit/angular-sq3ke5
ngOnInit() {
const bitcoin$ = this.http.get('https://blockchain.info/ticker');
this.polledBitcoin$ = timer(0, this.timeInterval).pipe(
merge(this.manualRefresh),
concatMap(_ => bitcoin$),
map((response: {EUR: {last: number}}) => {
console.log(new Date() +" >> " + response.EUR.last)
return response.EUR.last;
}),
);
}
However in this example, I have added polling interval and I want it to be updated based on the value entered by user. However any changes in text input is not getting reflected in polling interval. Can someone help me achieve this result?
Thanks in advance.
Updating the
timeIntervalvariable will not simply update yourintervalObservable, you will have to kill it and start a new Observable.Try this approach:
https://stackblitz.com/edit/angular-4n29cm?file=app%2Fapp.component.ts
Edit
A more performant approach would be to wait for the input to change and then re-create the interval again. I have used a Subject for listening to the changes in the input, wait for some time so that the user has finished typing and then restart the interval.
https://stackblitz.com/edit/angular-c355ij?file=app%2Fapp.component.ts