ribesg
02/15/2024, 1:59 PMkevin.cianfarini
02/15/2024, 2:05 PMflow { … }
builder.
registering callbacks to a flow can be done with callbackFlow
.
Collecting many flows concurrently can be done with a channelFlow
jw
02/15/2024, 2:20 PMjw
02/15/2024, 2:20 PMjw
02/15/2024, 2:20 PMjw
02/15/2024, 2:21 PMkevin.cianfarini
02/15/2024, 2:23 PMkevin.cianfarini
02/15/2024, 2:27 PMribesg
02/15/2024, 2:32 PMkevin.cianfarini
02/15/2024, 2:33 PMkevin.cianfarini
02/15/2024, 2:33 PMChannel.consumeAsFlow
is hot but it’s not entirely clear that it isribesg
02/15/2024, 2:34 PMribesg
02/15/2024, 2:36 PMCasey Brooks
02/15/2024, 7:06 PMkevin.cianfarini
02/15/2024, 7:07 PMA helpful distinction is that there’s a 1-to-1 relationship between the emitter and collector of a cold flow. The emitter sends values into the Flow which directly go to the Collector through the Flow pipeline. Thus, the flow only exists when both the emitter and collector are connected to one another, and is somewhat imaginary. There’s not really a physical object connecting the emitter and collector together other, it’s just a clever application of suspend functions.Does this apply for constructs like
channelFlow
though? Consider the example where you register a callback to some API that sends values to a channel.Casey Brooks
02/15/2024, 7:08 PMkevin.cianfarini
02/15/2024, 7:11 PMjw
02/15/2024, 7:11 PMCasey Brooks
02/15/2024, 7:11 PMflow { }
block having multiple things emitting values. But there’s still a one-to-one connection from the flow { }
builder to the collector. The only difference with a callbackFlow is whether the callbacks producing values are suspending vs not suspendingjw
02/15/2024, 7:12 PMkevin.cianfarini
02/15/2024, 7:15 PMCasey Brooks
02/15/2024, 7:23 PMchannelFlow
is effectively just this:
flow {
val channel = Channel()
registerCallback { valueFromCallback -> channel.trySend(valueFromCallback) }
for(value in channel) {
emit(value)
}
}
kevin.cianfarini
02/15/2024, 7:25 PMCasey Brooks
02/15/2024, 7:28 PMflow { }
directly executes that lambda.
Even when considering buffering and other flow operators, that just sticks stuff between those two directly-connected points