The RxRecipes series generally refers to Android and RxJava2, but the same recipes can apply to any Rx implementation and platform (RxJs, RxSwift, a backend with RxJava, etc).

Rx Composition with External Code

Imagine you have a wonderful reactive data layer for your newest application. The API/Disk/Cache setup is beautiful, so fresh so clean, truly an oasis of joy and wonder.

A new requirement comes in from product: you need to initialize the application with some static data to have a wait-free/no-internet-required first run.

So you decide to throw a JSON document in your /assets folder.

Except you soon realize this new data source (and associated AssetManager API) doesn’t fit with your compos-able, schedul-able, chain-able, error-terminating-able, (and all the other-ables) reactive data layer.

Ugh. InputStream and array literals, IOException to cover multiple cases (file doesn’t exist, some issue with the disk), synchronous-blocking close. No thanks.

What we really want is a reactive concise API like this:

Optional since the file may not exist. Completeable for the no-return-value operation. and an Observable stream of locales. Sweet! See discussion in comments regarding Maybe type for the getFileAsString() method.

But how do we get there?