Skip to content

Emitting Flow values without builder scope and Channel #1902

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
fluidsonic opened this issue Apr 4, 2020 · 3 comments
Closed

Emitting Flow values without builder scope and Channel #1902

fluidsonic opened this issue Apr 4, 2020 · 3 comments
Labels

Comments

@fluidsonic
Copy link

fluidsonic commented Apr 4, 2020

Currently there are two high-level approaches to emitting Flow values:

  • Calling emit() in a builder scope (flow {}, channelFlow {}, callbackFlow {}).
  • Sending to a Channel or BroadcastChannel and converting it into a Flow.

The former (emit()) suspends the emitter until the Flow collector has processed the emitted value.
The latter (BroadcastChannel/Channel) does not suspend the sender until the Flow collector has processed the emitted value.

In order to allow, outside of a builder scope, a value emission that suspends the emitter until the collector has processed the value it may be necessary to introduce a third way to emit values.

Use case

This is necessary to support use cases like #1901.

Very rough idea

fun <T> broadcastFlow(): BroadcastFlow<T> =interface BroadcastFlow<T>: Flow<T> {
   fun close()
   fun emit(value: T)
}

class MyComponent {
   private val eventBroadcast = broadcastFlow<Event>() // private, for emitting
   val events: Flow<Event> = eventBroadcast // public, for collecting

   private suspend fun doSomething() {
      // Do something.

      // Notify collectors that something was done.
      eventBroadcast.emit(SomethingEvent())

      // Wait for all collectors to react to whatever was done
      // in order to avoid concurrent state modifications.
      // E.g. an UI may depend on multiple components being in a synchronized state
      // after this work has been completed.

      // Finalize something and return.
   }

   fun onDestroy() {
      eventBroadcast.close()
   }
}
@elizarov
Copy link
Contributor

elizarov commented Apr 7, 2020

But what are those use-cases? Note, that in your example MyComponent.onSomething() would be a suspending function. Where did you encounter such need?

@elizarov elizarov added the flow label Apr 7, 2020
@fluidsonic
Copy link
Author

Thanks, I've added the suspend.
Regarding use case this is just an enabling feature for #1901.

@qwwdfsad
Copy link
Collaborator

Seems to be resolved with state and shared flows

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

3 participants