This commit is contained in:
InsanusMokrassar committed 2023-11-30 01:05:01 +06:00
1 parent 22d7ac3e22
commit 6f174cae1d
2 files changed
+22 -21

No files matched your search

@@ -9,35 +9,36 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
class FlowState<T>(
private val state: MutableState<T>,
initial: T,
internalScope: CoroutineScope = CoroutineScope(Dispatchers.Main)
) : MutableState<T>,
SpecialMutableStateFlow.Default<T>(state.value, internalScope) {
SpecialMutableStateFlow.Default<T>(initial, internalScope) {
private var internalValue: T = initial
override var value: T
get() = super.value
get() = internalValue
set(value) {
internalValue = value
tryEmit(value)
}
override fun component1(): T = state.component1()
override fun component2(): (T) -> Unit = state.component2()
private val stateState = derivedStateOf {
if (value != state.value) {
value = state.value
}
override suspend fun onChange(value: T) {
internalValue = value
super.onChange(value)
}
override suspend fun onChange(value: T) {
super.onChange(value)
override fun component1(): T = value
if (state.value != value) {
doInUI {
state.value = value
}
}
override fun component2(): (T) -> Unit = { tryEmit(it) }
override fun tryEmit(value: T): Boolean {
internalValue = value
return super.tryEmit(value)
}
override suspend fun emit(value: T) {
internalValue = value
super.emit(value)
}
}
fun <T> MutableState<T>.asFlowState(scope: CoroutineScope = CoroutineScope(Dispatchers.Main)) = FlowState(this, scope)
//fun <T> MutableState<T>.asFlowState(scope: CoroutineScope = CoroutineScope(Dispatchers.Main)) = FlowState(this, scope)