If I have a `ReceiveChannel`, is there a way for m...
# coroutines
s
If I have a
ReceiveChannel
, is there a way for me to turn it into a
Flow
?
o
flow(block)
can probably still work:
Copy code
val flowFromChan = flow { 
  recvChan.consumeEach(this::emit)
}
hmm, though I guess
consumeEach
is deprecated
not sure if they're still planning to drop it
b
Isn’t there a simple
ReceiveChannel<T>.asFlow(): Flow<T>
extension?
Ahh it appears to be for
BroadcastChannel<T>
instead of simply
ReceiveChannel<T>
(probably because flow can’t manage the lifecycle of any sort of subscription unless it uses broadcast subscriptions) https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/as-flow.html
o
yes, that's why I didn't suggest it
v
Copy code
flow { 
  recvChan.consumeEach(this::emit)
}
is an option, yes. We do not provide such extension on
ReceiveChannel
because it is not cold (e.g. second
collect
call to such flow will emit zero items) and does not play well with flow semantics. We are currently thinking of alternatives (e.g. some kind of subjects), so feel free to create an issue with your use-case and desired solution
d
Receive can be broadcast and then the broadcast can be flowed.
Not sure if it's a good idea but it's a thing