Chris Fillmore
08/25/2021, 6:24 PMMutableStateFlow.compareAndSet()
, but with a `sealed interface`:
sealed interface State {
object NotConnected : State
class Connected(val connection: SomeConnection) : State
}
val stateFlow = MutableStateFlow<State>(State.NotConnected)
...
// Obviously doesn't work, can't use State.Connected in this way
stateFlow.compareAndSet(State.Connected, State.NotConnected)
Chris Fillmore
08/25/2021, 6:49 PMinline fun <T, reified U: T> MutableStateFlow<T>.compareAndSet(update: T): Boolean = synchronized(this) {
val value = this.value
return if (value is U) compareAndSet(value, update) else false
}