javarx-java2

RX2. How to react on nothing emitted after x seconds?


Despite that my question seems very common I haven't found any nice solution to this. The situation, of course, is solvable, but I want to solve it in more RX way, if you know what I mean.

The situation

I have an Observable that emits some events. What I want is to subscribe for next upcoming event, and do something, if there will be no events emitted for 1 second. So, if something is emitted - call method doSomething(). If nothing was emitted during 1 second, call method doSomething().

Possible solution

One of the possible solutions is to create Completable.timer(1000), subscribe for both, and wait for first to emit / complete. Something like Observable.combineLatest except I don't have to wait both, but just first event to arrive. But I haven't found such functionality in RX2 also.

Please advice

Please advice solution for both situations, if possible, is there a way to set a waiting delay for next emitted item, or to pick first emitted from several observables?

Thanks!


Solution

  • I ended up using Observable.ambArray().

    Thanks to the @akarnokd comment