Is there a better (more correct?) way to write thi...
# coroutines
d
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
I would have gone for
Copy code
.flatMapLatest {
    flowOf(it ?: return@flatMapLatest other)
}
but it's functionally the same