Convert `ReceiveChannel<T>` to `Flow<T>` without consuming
See original GitHub issueMy ViewModel
exposes a stream of events as a ReceiveChannel<T>
// in `ViewModel`
private val events = Channel(capacity = Channel.UNLIMITED) // these are "side effects"
fun events(): ReceiveChannel<T> = events
My activity/fragment observes this stream of events, without consuming on cancellation.
// in an Activity's onCreate
launch {
for (event in viewModel.events()) {
ui.handleEvent(event)
}
}
By not consuming on cancellation (unsubscription), this allows my UI to re-use the same ReceiveChannel<T>
instance across configuration changes.
// this exists already
fun <T> ReceiveChannel<T>.consumeAsFlow(): Flow<T>
// this does not
fun <T> ReceiveChannel<T>.asFlowWithoutConsuming: Flow<T>
I think the latter function would be useful. Can it be added?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:9 (3 by maintainers)
Top Results From Across the Web
ReceiveChannel - Kotlin
Iteration completes normally when the channel is closed for receive without a cause and throws the original close cause exception if the channel...
Read more >can.interfaces.canalystii - python-can 4.1.0 documentation
__init__(channel=channel, can_filters=can_filters, **kwargs) if not (bitrate ... convert to seconds as # float timestamp = raw_msg.timestamp * 100e-6 return ...
Read more >Kotlin casting int to float - Stack Overflow
A cast does not convert a value into another type; a cast promises the compiler that the value already is the new type....
Read more >LTC2497 | 16-Bit 8-/16-Channel - Analog Devices
No Latency DSTM ADC with Easy Drive technology and ... selected and the first conversion, after a new channel is ... Address Bit...
Read more >tms320c6713b floating point digital signal processor
D Highest-Performance Floating-Point Digital ... Production processing does not necessarily include ... McBSPx receive channel enable register. 018C 0020.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
There should not be any cut-and-paste in this method implementation, as we are concerned about the overall size of the library. I’ve sketched the right-looking implementation in this commit: https://github.com/Kotlin/kotlinx.coroutines/commit/b16beb00b88a16a992e0d9f84184ae68a39d8226 It needs tests, though. There are also open design issue with respect to the consistency of operator naming and its interaction with
emitAll
. We’ll get back to it when time permits.Thanks. That’s clear. How about
receiveAsFlow
name for this conversion?