Dynamic delay value with repeatWhen()

后端 未结 5 1746

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

5条回答
  •  一整个雨季
    2020-12-19 03:46

    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);
    });
    

提交回复
热议问题