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

myanmarking

03/17/2020, 1:07 PM
Copy code
public fun <T> Flow<T>.onEmpty(action: suspend FlowCollector<T>.() -> Unit): Flow<T> {
    var valueCount: Int = 0
    return flow {
        collect { valueCount += 1; emit(it) }
        if(valueCount == 0) action()
    }
}
does this makes sense in terms of implementation ?
👍 1
e

elizarov

03/17/2020, 1:48 PM
Looks good to me
m

myanmarking

03/17/2020, 1:49 PM
will this work: flowOf<Int>().onEmpty{ emit(0) }. From my test it works, but someone mentioned it wont
e

elizarov

03/17/2020, 1:51 PM
It should work.
23 Views