General RxJava2 Transformers for different Rx sources

I understand that this might fall within the scope of library design and function implementation, but I wanted to ask if there is any way to use Transformers in different sources (like Observable and Completable) as they use quite a few methods, especially side effects. Rather than creating our own operators by extending them, we created transformers to handle the logic we usually use with our observables and off the shelf.

For example, instead of wrapping a one-off that is returned from a subscription call, we created a Transformer function for it:

public static <T> ObservableTransformer<T, T> dispose(CompositeDisposable compositeDisposable) {
    return observable -> observable.doOnSubscribe(compositeDisposable::add);
}

      

Now this won't work on any other source other than Observable, we needed to add another method for the finalized

public static CompletableTransformer disposeCompletable(CompositeDisposable compositeDisposable) {
    return completable -> completable.doOnSubscribe(compositeDisposable::add);
}

      

This has become a model for handling only two different source transformers that have exactly the same techniques

for example

public static <T, V extends Progressive & Erroneous> ObservableTransformer<T, T> progressiveErroneous(V view) {
    return observable -> observable
        .compose(progressive(view))
        .compose(erroneous(view));
}

public static <V extends Progressive & Erroneous> CompletableTransformer progressiveErroneousCompletable(V view) {
    return observable -> observable
        .compose(progressiveCompletable(view))
        .compose(erroneousCompletable(view));
}

      

To do this, we had to implement progressiveCompletable(view)

and erroneousCompletable(view)

, without distinction in the body of the method.

We also want to remove testing from our controllers and just test those transformers by calling the correct methods on the view interfaces. This will drastically reduce redundant tests and will be the reason why we chose such an abstract design. But if we duplicate these methods, the tests will be very repetitive. And if we start using other sources like Flowable, Maybe, etc. it will only get worse

Is it possible to use abstract transformers that work with a range of sources that support common operations such as

  • doOnSubscribe
  • doOnError
  • doFinally
  • etc.
+3


source to share





All Articles