mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-06 00:29:57 +00:00
FlowState
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package dev.inmo.micro_utils.coroutines.compose
|
||||
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.compose.runtime.State
|
||||
import androidx.compose.runtime.derivedStateOf
|
||||
import dev.inmo.micro_utils.coroutines.SpecialMutableStateFlow
|
||||
import dev.inmo.micro_utils.coroutines.doInUI
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
class FlowState<T>(
|
||||
private val state: MutableState<T>,
|
||||
internalScope: CoroutineScope = CoroutineScope(Dispatchers.Main)
|
||||
) : MutableState<T>,
|
||||
SpecialMutableStateFlow.Default<T>(state.value, internalScope) {
|
||||
override var value: T
|
||||
get() = super.value
|
||||
set(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) {
|
||||
super.onChange(value)
|
||||
|
||||
if (state.value != value) {
|
||||
doInUI {
|
||||
state.value = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> MutableState<T>.asFlowState(scope: CoroutineScope = CoroutineScope(Dispatchers.Main)) = FlowState(this, scope)
|
Reference in New Issue
Block a user