Tuesday, June 11, 2019

Learning RxJava: Thoughts on Chapter 9

compose()

You can create custom operators through composing existing operators by implementing the ObservableTransformer interface, and then passing it in to the compose() operator. This is useful for extracting duplicated code.

Often, creating a static method that will return the ObservableTransformer will allow for a clean and easy way to pass it into the compose operator.

This can also be done for Flowables using the FlowableTransformer.

to()

The to() operator simply takes a Function<Observable<T>, R>, which can be used to fluently convert the Observable to something else. This can be quite useful for mapping from an Observable to a language or framework specific construct such as a Future.

lift()

Sometimes there arises the need to build a custom operator from scratch, instead of composing it from existing operators. In such a situation you can implement the ObservableOperator interface and pass the resulting object to the lift() operator.

Note that this should be a rare occurrence, and it can be quite difficult to get right. Try your best to first implement your needs using the ObservableTransformer and the compose() operator, but if you determine that this is insufficient, then take a close look at how the standard operators are implemented in RxJava source code as well as trusted operator libraries such as RxJava2-Extras.