Mikael Ståldal
10/10/2024, 11:39 AMSam
10/10/2024, 12:19 PMkevin.cianfarini
10/10/2024, 1:36 PMStateFlow<Boolean>
kevin.cianfarini
10/10/2024, 1:45 PMval stateFlow = StateFlow<Boolean>(false)
// notify
stateFlow.value = true
// await notification by suspending until the stateflow's value is true
withTimeout(timeout) { stateFlow.first { it } }
kevin.cianfarini
10/10/2024, 1:46 PMCondition
you might be able to build one fairly easily which wraps a channel.Pablichjenkov
10/10/2024, 2:58 PMfirst
throw an exception if no value has been emitted?kevin.cianfarini
10/10/2024, 2:59 PMtrue
value is emitted.Pablichjenkov
10/10/2024, 3:16 PMThrows NoSuchElementException if the flow was empty.
The key question here is, what determines a flow is empty.
Sorry for deviating a bit from the original question 🙂kevin.cianfarini
10/10/2024, 3:17 PMkevin.cianfarini
10/10/2024, 3:18 PMkevin.cianfarini
10/10/2024, 3:25 PMflowOf()
and flow { }
are both considered to be empty because they complete and never emit anythingPablichjenkov
10/10/2024, 3:52 PMPablichjenkov
10/10/2024, 3:53 PMkevin.cianfarini
10/10/2024, 3:55 PMkevin.cianfarini
10/10/2024, 3:55 PMkevin.cianfarini
10/10/2024, 3:56 PMFlow
interfacerocketraman
10/10/2024, 4:35 PMlock
(or a block of code in withLock
) wrapped in withTimeout
.
Another approach would be to use a Channel. Wrap receive
in withTimeout
.
If you need to wait on multiple conditions you can combine awaiting on jobs, or channels, or deferreds, or a timeout, with select.kevin.cianfarini
10/10/2024, 4:41 PMrocketraman
10/10/2024, 4:44 PMkevin.cianfarini
10/10/2024, 4:45 PMrocketraman
10/10/2024, 4:46 PMCompletableDeferred
. The coroutine calls await
surrounded by withTimeout
. This can also be combined with select
for multiple conditions. It can be completed with Unit
, doesn't have to be a value (but could be).