zak.taccardi
09/06/2019, 12:56 AMFlow<T>
into a ReceiveChannel<T>
?
I’ve tried the following so far, but it’s deadlocking
@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated("You should be using Flows, not channels.")
fun <T> Flow<T>.collectAsReceiveChannel(scope: CoroutineScope): ReceiveChannel<T> {
val flow = this
val channel = Channel<T>()
val flowJob = scope.launch {
flow
.onEach { item -> channel.send(item) }
.collect()
}
channel.invokeOnClose {
flowJob.cancel()
}
return channel
}
louiscad
09/06/2019, 5:56 AMproduceIn
Zach Klippenstein (he/him) [MOD]
09/06/2019, 5:31 PM