Skip to content

Instantly share code, notes, and snippets.

@Dorus
Last active November 20, 2015 11:07
Show Gist options
  • Save Dorus/ac95d046c6957af730d7 to your computer and use it in GitHub Desktop.
Save Dorus/ac95d046c6957af730d7 to your computer and use it in GitHub Desktop.
Additional RxJava transformers
/**
* Transformer to concat a new observable based on the last element of the previous observable. Use the default value
* if the first observable is empty.
*
* @param defaultValue
* the default.
* @param nextObservable
* function to create the next observable based on the current observable.
* @return the transformer.
*/
<T> Transformer<T, T> concatOnLast(T defaultValue, Func1<T, Observable<T>> nextObservable) {
return source -> Observable.create(o -> {
ConnectableObservable<T> s1 = source.publish();
Observable<T> s2 = s1.lastOrDefault(defaultValue) // return a default value if source is empty.
.flatMap(nextObservable); // init source2 here based on e.
Observable.merge(s1, s2).subscribe(o);
o.add(s1.connect());
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment