Here is a simple extension function for RxKotlin if you have 10 sources for combineLatest
. You can easily create similar functions for more sources or adapt this to work with plain RxJava.
import io.reactivex.Observable
import io.reactivex.rxkotlin.Observables
@Suppress("UNCHECKED_CAST", "unused")
inline fun <T1 : Any, T2 : Any, T3 : Any, T4 : Any, T5 : Any, T6 : Any, T7 : Any, T8 : Any, T9 : Any, T10 : Any, R : Any> Observables.combineLatest(
source1: Observable<T1>, source2: Observable<T2>,
source3: Observable<T3>, source4: Observable<T4>,
source5: Observable<T5>, source6: Observable<T6>,
source7: Observable<T7>, source8: Observable<T8>,
source9: Observable<T9>, source10: Observable<T10>,
crossinline combineFunction: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) -> R
): Observable<R> =
Observable.combineLatest(arrayOf(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10)) {
combineFunction(
it[0] as T1,
it[1] as T2,
it[2] as T3,
it[3] as T4,
it[4] as T5,
it[5] as T6,
it[6] as T7,
it[7] as T8,
it[8] as T9,
it[9] as T10
)
}
Note: I've created this as an extension function to stay consistent with how combineLatest
function calls look like for less than 10 sources (Observables.combineLatest(...)
). That way I don't have to think about which combineLatest
version I need for what number of parameters. Technically there is no need to make it an extension function.