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

spierce7

04/17/2019, 5:04 AM
If I have a
ReceiveChannel
, is there a way for me to turn it into a
Flow
?
o

octylFractal

04/17/2019, 5:08 AM
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

bdawg.io

04/17/2019, 9:07 AM
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

octylFractal

04/17/2019, 9:50 AM
yes, that's why I didn't suggest it
v

Vsevolod Tolstopyatov [JB]

04/17/2019, 11:44 AM
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

dewildte

04/18/2019, 8:47 PM
Receive can be broadcast and then the broadcast can be flowed.
Not sure if it's a good idea but it's a thing
4 Views