Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxRelay - Enforce onComplete and onError as unnecessary on subscription

I'm looking for an elegant way to hide an Rx Relay as an observable, but enforce that subscribers will only ever have onNext.

The typical process when exposing a relay to an external class is to provide a public method and hide the relay:

public Observable<Boolean> booleanStream() { return myBooleanRelay.hide(); }

Sadly, we lose the fact that this stream never terminates. To an external observer, it is necessary to handle terminal onComplete and onError events. But this is wholly incorrect, as any handling of these events will never be called.

Essentially, I want to implement the inverse of a Completable.

Is there an elegant solution that leverages existing RxJava interfaces? Or would this need to be hand-rolled?

like image 700
Kevin Avatar asked Dec 31 '25 19:12

Kevin


1 Answers

If there is guarantee that no errors or completion happens, you can already use subscribe(Consumer).

Otherwise, there is no RxJava interface that you could use for such API design. You have to roll your own reactive base type and reactive consumer variants. Technically, the java.util.Observable was such infinite, no-error source but it lacks operators and is quite tedious to work with its architecture to develop those operators (see also Google Agera).

Note however, that forcefully ignoring errors and completions can have adverse effect of your flow and design. For example, if errors aren't allowed, the user callbacks can't throw any (runtime)exception. Talking to an API with potential error outcome requires one to drop the error and practically leave the flow hanging indefinitely. Second, never-ending flows may eventually consume available memory with flatMap; the inner flows never terminate but only get more numerous as the operator has to keep track of them indefinitely in case any of them emits more items at some point. concatMap and similar operators would not operate as continuations anymore. toList would never work properly (OOME).

like image 105
akarnokd Avatar answered Jan 06 '26 07:01

akarnokd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!