initialization fixes

This commit is contained in:
InsanusMokrassar 2023-11-30 01:22:43 +06:00
parent 6f174cae1d
commit 871b27f37d
2 changed files with 45 additions and 47 deletions

View File

@ -10,9 +10,9 @@ import kotlinx.coroutines.Dispatchers
class FlowState<T>( class FlowState<T>(
initial: T, initial: T,
internalScope: CoroutineScope = CoroutineScope(Dispatchers.Main) internalScope: CoroutineScope = CoroutineScope(Dispatchers.Default)
) : MutableState<T>, ) : MutableState<T>,
SpecialMutableStateFlow.Default<T>(initial, internalScope) { SpecialMutableStateFlow<T>(initial, internalScope) {
private var internalValue: T = initial private var internalValue: T = initial
override var value: T override var value: T
get() = internalValue get() = internalValue

View File

@ -9,51 +9,49 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
interface SpecialMutableStateFlow<T> : StateFlow<T>, FlowCollector<T>, MutableSharedFlow<T> { open class SpecialMutableStateFlow<T>(
open class Default<T>( initialValue: T,
initialValue: T, internalScope: CoroutineScope = CoroutineScope(Dispatchers.Default)
internalScope: CoroutineScope = CoroutineScope(Dispatchers.Default) ) : StateFlow<T>, FlowCollector<T>, MutableSharedFlow<T> {
) : SpecialMutableStateFlow<T> { protected val internalSharedFlow: MutableSharedFlow<T> = MutableSharedFlow(
protected val internalSharedFlow: MutableSharedFlow<T> = MutableSharedFlow( replay = 0,
replay = 0, extraBufferCapacity = 2,
extraBufferCapacity = 2, onBufferOverflow = BufferOverflow.DROP_OLDEST
onBufferOverflow = BufferOverflow.DROP_OLDEST )
) protected val publicSharedFlow: MutableSharedFlow<T> = MutableSharedFlow(
protected val publicSharedFlow: MutableSharedFlow<T> = MutableSharedFlow( replay = 1,
replay = 1, extraBufferCapacity = 1,
extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST
onBufferOverflow = BufferOverflow.DROP_OLDEST )
)
protected var _value: T = initialValue protected var _value: T = initialValue
override val value: T override val value: T
get() = _value get() = _value
protected open suspend fun onChange(value: T) { protected open suspend fun onChange(value: T) {
_value = value _value = value
publicSharedFlow.emit(value) publicSharedFlow.emit(value)
}
protected val job = internalSharedFlow.subscribe(internalScope) {
if (_value != it) {
onChange(it)
}
}
override val replayCache: List<T>
get() = publicSharedFlow.replayCache
override val subscriptionCount: StateFlow<Int>
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<T>) = publicSharedFlow.collect(collector)
} }
protected val job = internalSharedFlow.subscribe(internalScope) {
if (_value != it) {
onChange(it)
}
}
override val replayCache: List<T>
get() = publicSharedFlow.replayCache
override val subscriptionCount: StateFlow<Int>
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<T>) = publicSharedFlow.collect(collector)
} }