问题
I am trying to schedule a long polling mechanism. And I was wondering if I could leverage Schedulers for that.
Here's what I have been thinking so far.
Schedule via timer, but only enqueue next iteration if previous iteration has already finished.
Enqueue next iteration as previous iteration is finishing.
I have been looking at existing schedulers, but I am not really sure which one to pick and what to overload.
And last but not least - as I am a novice in Rx world - what are the advantages that the use of Scheduler would offer vis-a-vis "roll your own" approach.
回答1:
Something like this:
Observable.interval(500)
.exhaustMap(() => this.load()) //previously flatMapFirst, creates new observable only if previous has ended
.map(r => resource.json)
.distinctUntilChanged() //optional tracking changes
.startWith(0);
来源:https://stackoverflow.com/questions/37926371/how-do-i-long-polling-and-schedulers