Skip to content

Regression: channelFlow/flowViaChannel broken. #1259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
LouisCAD opened this issue Jun 7, 2019 · 3 comments
Closed

Regression: channelFlow/flowViaChannel broken. #1259

LouisCAD opened this issue Jun 7, 2019 · 3 comments

Comments

@LouisCAD
Copy link
Contributor

LouisCAD commented Jun 7, 2019

This is a bug.
I updated a project today where I used flowViaChannel to bridge from Observable from Google's Agera library for Android, and it stopped working properly.
Only reverting to the old implementation of flowViaChannel from 1.2.1 made it work.

The behavior I observe is that I only receive the first value.
Note that I use distinctUntilChanged() after it, and removing the conflate() operator applied before doesn't change the broken behavior.

For anyone that need the original working flowViaChannel until that is fixed, here's the code:

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import kotlin.experimental.ExperimentalTypeInference

@ExperimentalCoroutinesApi
@UseExperimental(ExperimentalTypeInference::class)
fun <T> flowViaChannel(
    bufferSize: Int = 16,
    @BuilderInference block: CoroutineScope.(channel: SendChannel<T>) -> Unit
): Flow<T> {
    return flow {
        coroutineScope {
            val channel = Channel<T>(bufferSize)
            launch {
                block(channel)
            }

            channel.consumeEach { value ->
                emit(value)
            }
        }
    }
}
@qwwdfsad
Copy link
Collaborator

qwwdfsad commented Jun 7, 2019

This is not a bug, but an incompatible change in flowViaChannel and it is written in the documentation:

The resulting flow completes as soon as the code in the [block] and all its children complete.
Use [awaitClose] as the last statement to keep it running.
[awaitClose] argument is called when either flow consumer cancels flow collection
or when callback-based API invokes [SendChannel.close] manually.

@LouisCAD
Copy link
Contributor Author

LouisCAD commented Jun 7, 2019

Shame on me. Was too quick to update. Will check doc more thoroughly next time.
Thank you for the info.

@LouisCAD LouisCAD closed this as completed Jun 7, 2019
@LouisCAD
Copy link
Contributor Author

LouisCAD commented Jun 7, 2019

Having flowViaChannel change its behavior on deprecation was maybe not the best update in kotlinx.coroutines ever, that said 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants