bbaldino
12/02/2020, 8:34 PMbezrukov
12/02/2020, 9:38 PMbbaldino
12/02/2020, 9:40 PMfun <T> Flow<T>.withDefaultAfterTimeout(default: T, timeoutMs: Long): Flow<T> = flow {
coroutineScope { // delimit a scope in which flow is running
val channel = produceIn(this) // run flow in the scope
while (isActive) {
val result = withTimeoutOrNull(timeoutMs) {
select<T> {
channel.onReceive { it }
}
} ?: default
emit(result)
}
}
}
flow.onStart { emit(null) }
needed?bezrukov
12/02/2020, 9:49 PMbbaldino
12/02/2020, 9:49 PM