diff --git a/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowState.kt b/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowState.kt index e5649e153c7..6212c2667eb 100644 --- a/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowState.kt +++ b/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/FlowState.kt @@ -9,35 +9,36 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers class FlowState( - private val state: MutableState, + initial: T, internalScope: CoroutineScope = CoroutineScope(Dispatchers.Main) ) : MutableState, - SpecialMutableStateFlow.Default(state.value, internalScope) { + SpecialMutableStateFlow.Default(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 MutableState.asFlowState(scope: CoroutineScope = CoroutineScope(Dispatchers.Main)) = FlowState(this, scope) +//fun MutableState.asFlowState(scope: CoroutineScope = CoroutineScope(Dispatchers.Main)) = FlowState(this, scope) diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SpecialStateFlow.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SpecialStateFlow.kt index 8509aa6c8fd..a3464256fd8 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SpecialStateFlow.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SpecialStateFlow.kt @@ -16,7 +16,7 @@ interface SpecialMutableStateFlow : StateFlow, FlowCollector, MutableSh ) : SpecialMutableStateFlow { protected val internalSharedFlow: MutableSharedFlow = MutableSharedFlow( replay = 0, - extraBufferCapacity = 1, + extraBufferCapacity = 2, onBufferOverflow = BufferOverflow.DROP_OLDEST ) protected val publicSharedFlow: MutableSharedFlow = MutableSharedFlow( @@ -32,7 +32,7 @@ interface SpecialMutableStateFlow : StateFlow, FlowCollector, MutableSh _value = value publicSharedFlow.emit(value) } - protected val job = internalSharedFlow.subscribeSafelyWithoutExceptions(internalScope) { + protected val job = internalSharedFlow.subscribe(internalScope) { if (_value != it) { onChange(it) }