Maciek
07/20/2020, 11:50 AMTim Malseed
07/20/2020, 11:52 AMFlow
, then I believe Flow.zip
is the operator you’re looking for hereMaciek
07/20/2020, 11:54 AMTim Malseed
07/20/2020, 12:21 PMonEach()
, you could try first()
Kroppeb
07/20/2020, 2:14 PMselect
https://kotlinlang.org/docs/reference/coroutines/select-expression.htmljulian
07/20/2020, 4:36 PMzip
will wait for an emission from both flows. It alone doesn't allow you to race 2 flows and cancel the loser once you have a winner.first
would cancel all upstreams once you have a winner.zip
to cancel upstreams without forcing zip
to wait for emissions from both zipped streams.louiscad
07/21/2020, 3:55 AMraceOf
where the winner coroutine cancels the others, and where its value is used.
https://github.com/LouisCAD/Splitties/blob/017c21aaa03fc14859ea6b8e2500b3093f8dbc62/modules/coroutines/src/commonMain/kotlin/splitties/coroutines/Racing.kt#L29-52
The other code in the file supports the race
function which allows to launch racers dynamically, if some coroutines need to join the race only based on some conditions.awaitCancellation()
(which justs suspends until cancelled, returning Nothing
) for UI code where multiple user actions are allowed in a scope/context.Tim Malseed
07/21/2020, 3:58 AMThe resulting flow completes as soon as one of the flows completes and cancel is called on the remaining flowI took this to mean that it does not wait for both flows to emit. Am I misinterpreting the docs?
louiscad
07/21/2020, 4:01 AMTim Malseed
07/21/2020, 4:02 AMjulian
07/21/2020, 4:05 AMzip
waits for an emission from both flows being zipped before executing the lambda supplied to it. This lambda is the first opportunity one has to do anything with the flows. The next opportunity is downstream from the zip
operator. Problem is, you can't cancel either flow from inside the `zip`'s lambda. And by the time you cancel downstream of zip
, both flows have already emitted.Tim Malseed
07/21/2020, 4:06 AM