RxKotlin 2.3.0です。
RxKotlin + ラムダで華麗にストリームを結合していたんですが、可変個のFlowableをcombineLatestしようとした際にFlowablesに定義されてないことに気づきました。
固定個だと9個までは用意してくれている
RxKotlin/src/main/kotlin/io/reactivex/rxkotlin/Flowables.kt
@CheckReturnValue
@BackpressureSupport(BackpressureKind.FULL)
@SchedulerSupport(SchedulerSupport.NONE)
inline fun <T1,T2,T3,T4,T5,T6,T7,T8,T9,R> combineLatest(source1: Flowable<T1>, source2: Flowable<T2>,
source3: Flowable<T3>, source4: Flowable<T4>,
source5: Flowable<T5>, source6: Flowable<T6>,
source7: Flowable<T7>, source8: Flowable<T8>,
source9: Flowable<T9>, crossinline combineFunction: (T1,T2, T3, T4, T5, T6, T7, T8, T9) -> R) =
Flowable.combineLatest(source1, source2,source3, source4, source5, source6, source7, source8, source9,
Function9 { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8: T8, t9: T9 -> combineFunction(t1,t2, t3, t4, t5, t6, t7, t8, t9) })!!
んですが、10個以上や一覧取得で返ってきた可変個のIDで詳細APIを呼び出したいといった際に困ってしまいます。
結論
Iterable<Flowable<T>>.combineLatest()として存在しました。
RxKotlin/src/main/kotlin/io/reactivex/rxkotlin/flowable.kt
/**
* Flowable.combineLatest(List<? extends Flowable<? extends T>> sources, FuncN<? extends R> combineFunction)
*/
@Suppress("UNCHECKED_CAST")
@SchedulerSupport(SchedulerSupport.NONE)
@CheckReturnValue
@BackpressureSupport(BackpressureKind.FULL)
inline fun <T : Any, R : Any> Iterable<Flowable<T>>.combineLatest(crossinline combineFunction: (args: List<T>) -> R): Flowable<R>
= Flowable.combineLatest(this) { combineFunction(it.asList().map { it as T }) }
探してます
同様にSigle.zip(Iterable<Single<T>>)も見当たりません。
Iterable<Flowable<T>>.zip()はあるので、toFlowable()で変換してしまう。
もしくは、自分でExtension書くなりすればいい話ではあるんですが。。。