1

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.

  1. Schedule via timer, but only enqueue next iteration if previous iteration has already finished.

  2. 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.

JanezStupar
  • 258
  • 4
  • 11
  • I have no time for a proper answer right now, but that's a pretty simple example and hopefully easy to understand - it doesn't honor the state of the last iteration though and just fires every x milliseconds: http://plnkr.co/edit/9OspJxMcqqE01S50GK8T?p=preview – Maximilian Riegler Jun 20 '16 at 15:56
  • I figured out that much and indeed it was pretty simple. But I would really like to ensure that only a single request is going on at a time, without resorting to external state. – JanezStupar Jun 21 '16 at 07:49

1 Answers1

4

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); 
kemsky
  • 14,727
  • 3
  • 32
  • 51
  • Yes! This is precisely it. exhaustMap it is then. I failed to deduce it from the operator docs, despite reading them repeatedly. Thanks a lot for this one. Also distinctUntilChanged bit also looks interesting. Thanks again, this is going to help my understanding of Rx immensely. – JanezStupar Jun 21 '16 at 07:51