Last active
November 20, 2015 11:07
-
-
Save Dorus/ac95d046c6957af730d7 to your computer and use it in GitHub Desktop.
Additional RxJava transformers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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