From 871b27f37d19967bf69a8e5af1530a1646fd5e2a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 30 Nov 2023 01:22:43 +0600 Subject: [PATCH] initialization fixes --- .../coroutines/compose/FlowState.kt | 4 +- .../coroutines/SpecialStateFlow.kt | 88 +++++++++---------- 2 files changed, 45 insertions(+), 47 deletions(-) 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 6212c2667eb..3df38d3b07c 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 @@ -10,9 +10,9 @@ import kotlinx.coroutines.Dispatchers class FlowState( initial: T, - internalScope: CoroutineScope = CoroutineScope(Dispatchers.Main) + internalScope: CoroutineScope = CoroutineScope(Dispatchers.Default) ) : MutableState, - SpecialMutableStateFlow.Default(initial, internalScope) { + SpecialMutableStateFlow(initial, internalScope) { private var internalValue: T = initial override var value: T get() = internalValue 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 a3464256fd8..9e5ca662cb7 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 @@ -9,51 +9,49 @@ import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow -interface SpecialMutableStateFlow : StateFlow, FlowCollector, MutableSharedFlow { - open class Default( - initialValue: T, - internalScope: CoroutineScope = CoroutineScope(Dispatchers.Default) - ) : SpecialMutableStateFlow { - protected val internalSharedFlow: MutableSharedFlow = MutableSharedFlow( - replay = 0, - extraBufferCapacity = 2, - onBufferOverflow = BufferOverflow.DROP_OLDEST - ) - protected val publicSharedFlow: MutableSharedFlow = MutableSharedFlow( - replay = 1, - extraBufferCapacity = 1, - onBufferOverflow = BufferOverflow.DROP_OLDEST - ) +open class SpecialMutableStateFlow( + initialValue: T, + internalScope: CoroutineScope = CoroutineScope(Dispatchers.Default) +) : StateFlow, FlowCollector, MutableSharedFlow { + protected val internalSharedFlow: MutableSharedFlow = MutableSharedFlow( + replay = 0, + extraBufferCapacity = 2, + onBufferOverflow = BufferOverflow.DROP_OLDEST + ) + protected val publicSharedFlow: MutableSharedFlow = MutableSharedFlow( + replay = 1, + extraBufferCapacity = 1, + onBufferOverflow = BufferOverflow.DROP_OLDEST + ) - protected var _value: T = initialValue - override val value: T - get() = _value - protected open suspend fun onChange(value: T) { - _value = value - publicSharedFlow.emit(value) - } - protected val job = internalSharedFlow.subscribe(internalScope) { - if (_value != it) { - onChange(it) - } - } - - override val replayCache: List - get() = publicSharedFlow.replayCache - override val subscriptionCount: StateFlow - get() = publicSharedFlow.subscriptionCount - - @ExperimentalCoroutinesApi - override fun resetReplayCache() = publicSharedFlow.resetReplayCache() - - override fun tryEmit(value: T): Boolean { - return internalSharedFlow.tryEmit(value) - } - - override suspend fun emit(value: T) { - internalSharedFlow.emit(value) - } - - override suspend fun collect(collector: FlowCollector) = publicSharedFlow.collect(collector) + protected var _value: T = initialValue + override val value: T + get() = _value + protected open suspend fun onChange(value: T) { + _value = value + publicSharedFlow.emit(value) } + protected val job = internalSharedFlow.subscribe(internalScope) { + if (_value != it) { + onChange(it) + } + } + + override val replayCache: List + get() = publicSharedFlow.replayCache + override val subscriptionCount: StateFlow + get() = publicSharedFlow.subscriptionCount + + @ExperimentalCoroutinesApi + override fun resetReplayCache() = publicSharedFlow.resetReplayCache() + + override fun tryEmit(value: T): Boolean { + return internalSharedFlow.tryEmit(value) + } + + override suspend fun emit(value: T) { + internalSharedFlow.emit(value) + } + + override suspend fun collect(collector: FlowCollector) = publicSharedFlow.collect(collector) }