https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

Marc Knaup

04/04/2020, 12:04 PM
Here’s a very naive totally unsafe example of how it should work.
send(…)
will block untill all flows have processed the sent element. Is there something like that already?
d

Dominaezzz

04/04/2020, 4:10 PM
You could maintain a list of rendezvous channels (protected by
Mutex
), then return
consumeAsFlow()
in
receiveAsFlow()
.
What should happen if a consumer calls
receiveAsFlow
calls when
send
is suspended?
m

Marc Knaup

04/04/2020, 4:29 PM
receiveAsFlow
merely creates a new
Flow
. It’s not even suspending.
I’ve opened a new issue for this feature with new approach 🙂 https://github.com/Kotlin/kotlinx.coroutines/issues/1901
Channels can’t solve this issue. Channels never wait for for a value to be processed by the consumer. They only wait for retrieval.
d

Dominaezzz

04/04/2020, 4:33 PM
Flows cannot solve this either. You probably want a custom actor or something along those lines.
Especially since "processed" will have to be concretely defined between the sender and receiver.
m

Marc Knaup

04/04/2020, 4:37 PM
A regular
Flow
already has exactly that behavior:
emit()
suspends until the value collection is completed downstream. It just doesn’t work for broadcasts.
m

Marc Knaup

04/04/2020, 5:02 PM
Very interesting, thank you!
3 Views