Skip to content
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

Provide CoroutineScope receiver to flowViaChannel lambda #1081

Closed
qwwdfsad opened this issue Apr 10, 2019 · 1 comment
Closed

Provide CoroutineScope receiver to flowViaChannel lambda #1081

qwwdfsad opened this issue Apr 10, 2019 · 1 comment
Assignees
Labels
Milestone

Comments

@qwwdfsad
Copy link
Collaborator

Original request: #254 (comment)

Additionally, we should discuss whether it will be CoroutineScope or ProducerScope and its relation with not yet discusses scopedFlow

@qwwdfsad
Copy link
Collaborator Author

After some thoughts, we've decided to provide CoroutineScope receiver, but remove suspend modifier from lambda, thus replacing the signature block: suspend (SendChannel<T>) -> Unit with block: CoroutineScope.(SendChannel<T>) -> Unit.

Why so? I see two possible usages of this API:

  • Integrating with callback-based API as in our KDoc sample. For this reason, one doesn't need suspension (it is even harmful to Java interoperability). CoroutineScope is not required either, but it is present in the implementation anyway (and we cannot get rid of it) and it provides additional safety mechanism as there is no temptation to use GlobalScope

  • Integration with blocking API, but from the coroutine world. For example:

flowViaChannel {
  withContext(IO) {
     channel.send(doBlockingCall())
  }
}

While this looks reasonable, it is not the most idiomatic way. Instead, flow builder is much more suitable:

flow {
  emit(doBlockingCall())
}.flowOn(IO)

cc @jcornaz as the author of the original request

@qwwdfsad qwwdfsad added this to the 1.2.1 milestone Apr 23, 2019
qwwdfsad added a commit that referenced this issue Apr 23, 2019
…pending.

    * It allows using flowViaChannel for integration with Java callbacks
    * CoroutineScope is provided to provide a lifecycle object (that is not otherwise accessible)
    * Suspending use-cases are covered with flow builder

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

No branches or pull requests

1 participant