reactormonk
06/18/2024, 6:25 PM)
suspend fun detectTVOnSignal(tvState: MutableStateFlow<TVState>): TurnedOnDetectionHeuristics? {
val result = coroutineScope {
val transientToOn = tvState.filter { it is TVState.TransientToOn }.produceIn(this)
val on = tvState.filter { it is TVState.On }.produceIn(this)
Timber.d { "starting select" }
select<TurnedOnDetectionHeuristics?> {
transientToOn.onReceive {
Timber.d { "starting second select" }
select {
on.onReceive {
Timber.d { "got transition and on" }
TurnedOnDetectionHeuristics.On
}
onTimeout(30.seconds) {
Timber.d { "only got transition" }
TurnedOnDetectionHeuristics.TransitionToOn
}
}
}
on.onReceive {
Timber.d { "got on" }
TurnedOnDetectionHeuristics.On
}
onTimeout(30.seconds) {
Timber.d { "timed out on on" }
null
}
}
}
Timber.d { "Result: $result" }
return result
}
I get:
• starting select
• got on
<nothing>
^ I'd expect the "Result" here.ephemient
06/18/2024, 6:29 PMreactormonk
06/18/2024, 6:29 PMselect
only picks and executes the first matching clause, which is then the resultephemient
06/18/2024, 6:30 PMselect
is done, but the coroutineScope
is still alive because it has children (the launched produceIn
jobs)reactormonk
06/18/2024, 6:30 PMephemient
06/18/2024, 6:30 PMState flow never completes.
ephemient
06/18/2024, 6:31 PMreactormonk
06/18/2024, 6:31 PMephemient
06/18/2024, 6:34 PMreactormonk
06/18/2024, 6:34 PMTransientToOn
signal, maybe not.reactormonk
06/18/2024, 6:35 PMephemient
06/18/2024, 6:37 PMval onOrTransition = withTimeoutOrNull(30.seconds) {
tvState.firstOrNull { it is TVState.On || it is TVState.TransientToOn }
}
val on = if (onOrTransition !is TVState.On) {
withTimeoutOrNull(30.seconds) {
tvState.firstOrNull { it is TVState.On }
}
} else {
onOrTransition
}
reactormonk
06/18/2024, 6:38 PMreactormonk
06/18/2024, 6:39 PM