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 762e85b63cf..7c43e0e9f99 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 @@ -8,9 +8,8 @@ import kotlinx.coroutines.flow.FlowCollector import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.internal.synchronized -interface SpecialMutableStateFlow : MutableStateFlow { +interface SpecialMutableStateFlow : StateFlow, FlowCollector, MutableSharedFlow { class Default( initialValue: T, internalScope: CoroutineScope = CoroutineScope(Dispatchers.Default) @@ -21,22 +20,16 @@ interface SpecialMutableStateFlow : MutableStateFlow { onBufferOverflow = BufferOverflow.DROP_OLDEST ) private val publicSharedFlow: MutableSharedFlow = MutableSharedFlow( - replay = 0, + replay = 1, extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST ) - private var _value: T = initialValue - override var value: T - get() { - return _value - } - set(value) { - tryEmit(value) - } + override var value: T = initialValue + private set private val job = internalSharedFlow.subscribe(internalScope) { - if (_value != it) { - _value = it + if (value != it) { + value = it publicSharedFlow.emit(it) } } @@ -46,18 +39,6 @@ interface SpecialMutableStateFlow : MutableStateFlow { override val subscriptionCount: StateFlow get() = publicSharedFlow.subscriptionCount - init { - value = initialValue - } - - override fun compareAndSet(expect: T, update: T): Boolean { - return if (value == expect) { - tryEmit(update) - } else { - false - } - } - @ExperimentalCoroutinesApi override fun resetReplayCache() = publicSharedFlow.resetReplayCache()