question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Convert `ReceiveChannel<T>` to `Flow<T>` without consuming

See original GitHub issue

My 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
elizarovcommented, Sep 23, 2019

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.

1reaction
elizarovcommented, Aug 30, 2019

Thanks. That’s clear. How about receiveAsFlow name for this conversion?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found