Observable#repeatWhen
See original GitHub issueI’m trying to leverage the repeatWhen
operator, but its behavior doesn’t seem to be following the documentation.
I’m probably not using it correctly, but I can’t see exactly what I’m doing wrong, and I also noticed that this operator is lacking test coverage.
final AtomicInteger i = new AtomicInteger(0);
final Observable<?> timer = Observable.timer(100, TimeUnit.MILLISECONDS)
.take(6)
.cache();
final Observable<Integer> result = Observable.defer(() -> Observable.just(i.getAndIncrement()))
.repeatWhen(observable -> timer)
.cache();
result
.subscribe(System.out::println);
// wait for result to complete.
result
.toList()
.toBlocking()
.first();
I would expect this to print 0, 1, 2, 3, 4, 5
and then complete, but instead result
is only emitting 0 and completing.
Note that what I’m trying to accomplish is more complex than this (this example in particular could be implemented with timer()
+ map()
).
In my example the observable returned from repeatWhen
is a subject
to which I send values to make the resulting Observable
repeat itself, but I simplified this for illustration purposes.
Could somebody point to what I’m doing wrong, or whether there’s a better way to implement what I described?
Thank you.
Issue Analytics
- State:
- Created 8 years ago
- Comments:10 (3 by maintainers)
@akarnokd The case you point out seems like a bug to me. The final subscriber after the repeatWhen should always receive the onCompleted. Emitting an empty observable from the notificationHandler in a
repeatWhen
will propagate the onCompleted event to the child subscriber effectively terminating the observable chain. Its interesting that when I change the flatMap to a takeWhile the subscriber does execute it’s onCompleted.output
I’ve looked into repeatWhen and it is unclear to me how the returned observable from the user function should affect the resubscription. It seems you can’t dismiss the incoming
observable
and return something independent because the returned observabletimer
will be subscribed to immediately. My guess is that you need to map/flatMap over theobservable
to return a possible delay for the resubscription:But this never prints “Done”.
/cc @benjchristensen @stealthcode