Avoiding a shared state with transformers

When you start creating your own transformers and custom operators, an easy way to shoot yourself in the foot is to share states between more than one subscription. This can quickly create unwanted side effects and buggy applications and is one of the reasons you have to tread carefully as you create your own operators.

Say you want to create an ObservableTransformer<T, IndexedValue<T>>, which pairs each emission with its consecutive index starting at 0. First, you create an IndexedValue<T> class to simply pair each T value with an int index, as shown in the following code example:

 static final class IndexedValue<T> {     final int index;     final T value;     IndexedValue(int index, T value) { this.index = ...

Get Learning RxJava - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.