Right now I\'m implementing some polling logic with RxJava. I\'m supposed to poll an endpoint a number of times until it tells me to stop. Additionally, each response come
As @JohnWowUs mentioned, you need out-of-band communication, but if you subscribe to the sequence more than once, you can use defer
to have per-subscriber state:
Observable.defer(() -> {
int[] pollDelay = { 0 };
return service.pollEndpoint()
.doOnNext(response -> pollDelay[0] = response.getDelay())
.repeatWhen(o -> o.flatMap(v -> Observable.timer(pollDelay[0], MILLISECONDS)))
.takeUntil(Blah::shouldStopPolling);
});