https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

Dominaezzz

01/31/2021, 5:03 PM
Is there a better (more correct?) way to write this operator?
Copy code
@ExperimentalCoroutinesApi
internal fun <T> Flow<T?>.coalesce(other: Flow<T>): Flow<T> {
    return distinctUntilChanged { old, new -> old == null && new == null }
        .transformLatest {
            if (it != null) {
                // TODO: Will "Latest" wrongly cancel this branch?
                emit(it)
            } else {
                emitAll(other)
            }
        }
}
e

ephemient

01/31/2021, 8:05 PM
I would have gone for
Copy code
.flatMapLatest {
    flowOf(it ?: return@flatMapLatest other)
}
but it's functionally the same