kevin.cianfarini
10/05/2023, 7:02 PMSend and receive operations to channels are fair with respect to the order of their invocation from multiple coroutines. They are served in first-in first-out order, e.g. the first coroutine to invokegets the element. — <https://kotlinlang.org/docs/channels.html#channels-are-fairreceive
|Channels are fair docs>and
Merges the given flows into a single flow without preserving an order of elements. All flows are merged concurrently, without limit on the number of simultaneously collected flows. — <https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/merge.html
|Iterable<Flow>.merge>Does this mean that it’s possible for merged flow to emit elements in an unfair fashion? Doesn’t merging these flows just send elements to a single underlying channel?
Zach Klippenstein (he/him) [MOD]
10/05/2023, 7:06 PMkevin.cianfarini
10/05/2023, 7:07 PMkevin.cianfarini
10/05/2023, 7:07 PMkevin.cianfarini
10/05/2023, 7:07 PMZach Klippenstein (he/him) [MOD]
10/05/2023, 7:08 PMZach Klippenstein (he/him) [MOD]
10/05/2023, 7:08 PMkevin.cianfarini
10/05/2023, 7:09 PM/**
* Merges the given flows into a single flow without preserving an order of elements.
* All flows are merged concurrently, without limit on the number of simultaneously collected flows.
*
* ### Operator fusion
*
* Applications of [flowOn], [buffer], and [produceIn] _after_ this operator are fused with
* its concurrent merging so that only one properly configured channel is used for execution of merging logic.
*/
public fun <T> Iterable<Flow<T>>.merge(): Flow<T> {
/*
* This is a fuseable implementation of the following operator:
* channelFlow {
* forEach { flow ->
* launch {
* flow.collect { send(it) }
* }
* }
* }
*/
return ChannelLimitedFlowMerge(this)
}
Zach Klippenstein (he/him) [MOD]
10/05/2023, 7:09 PMkevin.cianfarini
10/05/2023, 7:09 PMCasey Brooks
10/05/2023, 7:14 PMchannel.send
.
Basically, there’s no synchronization among the flows themselves, the synchronization comes from the values being placed into a channelkevin.cianfarini
10/05/2023, 7:15 PMlaunched
jobs I supposekevin.cianfarini
10/05/2023, 7:16 PMCasey Brooks
10/05/2023, 7:16 PM