You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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> = …
interfaceBroadcastFlow<T>: Flow<T> {
funclose()
funemit(value:T)
}
classMyComponent {
privateval eventBroadcast = broadcastFlow<Event>() // private, for emittingval events:Flow<Event> = eventBroadcast // public, for collectingprivatesuspendfundoSomething() {
// 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.
}
funonDestroy() {
eventBroadcast.close()
}
}
The text was updated successfully, but these errors were encountered:
Currently there are two high-level approaches to emitting
Flow
values:emit()
in a builder scope (flow {}
,channelFlow {}
,callbackFlow {}
).Channel
orBroadcastChannel
and converting it into aFlow
.The former (
emit()
) suspends the emitter until theFlow
collector has processed the emitted value.The latter (
BroadcastChannel
/Channel
) does not suspend the sender until theFlow
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
The text was updated successfully, but these errors were encountered: