question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Observable#repeatWhen

See original GitHub issue

I’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:closed
  • Created 8 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
stealthcodecommented, Jun 5, 2015

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

AtomicInteger c = new AtomicInteger();
Observable.just(1)
    .repeatWhen(o -> o.takeWhile((v -> c.getAndIncrement() == 0)))
    .subscribe(System.out::println, Throwable::printStackTrace, () -> System.out.println("Done"));

output

1
1
Done
1reaction
akarnokdcommented, Apr 20, 2015

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 observable timer will be subscribed to immediately. My guess is that you need to map/flatMap over the observable to return a possible delay for the resubscription:

AtomicInteger c = new AtomicInteger();
Observable.just(1)
.repeatWhen(o -> o.flatMap(v -> {
    if (c.getAndIncrement() == 0) {
        return Observable.just(1);
    }
    return Observable.empty();
}))
.subscribe(System.out::println, Throwable::printStackTrace, () -> System.out.println("Done"));

But this never prints “Done”.

/cc @benjchristensen @stealthcode

Read more comments on GitHub >

github_iconTop Results From Across the Web

repeatWhen - RxJS
Returns an Observable that mirrors the source Observable with the exception of a complete . If the source Observable calls complete , this...
Read more >
repeatWhen · rxjs - xngiser
Returns an Observable that emits the same values as the source Observable with the exception of an onCompleted. An onCompleted notification from the...
Read more >
rx.Observable.repeatWhen java code examples - Tabnine
public Observable call(Observable upstream) { return upstream.repeatWhen(events -> events.switchMap(aVoid -> source));
Read more >
RxJava's repeatWhen and retryWhen, explained
repeatWhen is identical to retryWhen , only it responds to onCompleted instead of onError . The input is Observable<Void> , since onCompleted ...
Read more >
Utilizing rxjs repeatWhen in http request pipe in order to ...
Let's break down the description of the repeatWhen operator: Returns an Observable that mirrors the source Observable with the exception of a complete....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found