https://kotlinlang.org logo
Title
t

trevjones

08/04/2020, 8:27 PM
I am finding myself doing roughly:
combine(one, two, three) { 1, 2, 3 -> Triple(1,2,3) }.flatMapLatest { (1, 2, 3) -> ... }
is there a cleaner way built in to deal with that?
l

louiscad

08/05/2020, 5:42 AM
@tseisel It doesn't cancel running transform on new value like flatMapLatest does.
t

trevjones

08/06/2020, 7:24 PM
good catch Louis! I am going to go poke at it a bit, probably just a matter of holding a job ref in the containing object. even if a custom operator for a bit I can live with it
was going to do some custom impl but decided to just keep it simple for fear of buggy mess.
inline fun <T1, T2, T3, R> combineFlatLatest(
    one: Flow<T1>,
    two: Flow<T2>,
    three: Flow<T3>,
    crossinline combiner: (T1, T2, T3) -> Flow<R>
): Flow<R> {
    return combine(one, two, three) { _one, _two, _three ->
        arrayOf(_one, _two, _three)
    }.flatMapLatest { elements ->
        @Suppress("UNCHECKED_CAST")
        combiner(
            elements[0] as T1, 
            elements[1] as T2, 
            elements[2] as T3
        )
    }
}