https://kotlinlang.org logo
Title
i

Icaro Temponi

07/31/2019, 5:49 PM
Just to make shure I got that right, I'd like to know if this chain does the following: - Run updateNotifications on each emission - If the function takes too long to execute, buffer 1 emission and ignore all others - When the function finishes, run it again if there's a buffered emission
flow()
        .conflate()
        .buffer(1)
        .onEach { updateNotifications() }
        .launchIn(GlobalScope)
l

louiscad

07/31/2019, 5:52 PM
That's what it should do AFAIK, but you can still test it, test sources are a great place for this.
z

Zach Klippenstein (he/him) [MOD]

07/31/2019, 6:01 PM
conflate
and
buffer
are contradictory and will be fused, with
conflate
winning:
Note that conflate operator is a shortcut for buffer with capacity of Channel.CONFLATED.
Adjacent applications of conflate/buffer, channelFlow, flowOn, produceIn, and broadcastIn are always fused so that only one properly configured channel is used for execution. Conflation takes precedence over buffer() calls with any other capacity.
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/conflate.html
👍 1
l

louiscad

07/31/2019, 6:05 PM
That's right!
conflate()
always buffer latest element anyway, so you can get rid of
buffer(1)
and have the desired behavior.
i

Icaro Temponi

07/31/2019, 6:11 PM
Gosh, I should've read the entire KDoc of the operator 😅, thanks for the clarification, Flows are really awesome
d

Dominaezzz

07/31/2019, 6:18 PM
Oh, I thought that was intentional.
i

Icaro Temponi

07/31/2019, 6:30 PM
Wrote some tests and the
conflate
operator alone did just what I needed 👍
👍 2