mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-18 06:40:07 +00:00
small fixes in SpecialStateFlow
This commit is contained in:
parent
9d2b50e55d
commit
915bac64b1
@ -8,9 +8,8 @@ import kotlinx.coroutines.flow.FlowCollector
|
|||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
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
|
||||||
import kotlinx.coroutines.internal.synchronized
|
|
||||||
|
|
||||||
interface SpecialMutableStateFlow<T> : MutableStateFlow<T> {
|
interface SpecialMutableStateFlow<T> : StateFlow<T>, FlowCollector<T>, MutableSharedFlow<T> {
|
||||||
class Default<T>(
|
class Default<T>(
|
||||||
initialValue: T,
|
initialValue: T,
|
||||||
internalScope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
internalScope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||||
@ -21,22 +20,16 @@ interface SpecialMutableStateFlow<T> : MutableStateFlow<T> {
|
|||||||
onBufferOverflow = BufferOverflow.DROP_OLDEST
|
onBufferOverflow = BufferOverflow.DROP_OLDEST
|
||||||
)
|
)
|
||||||
private val publicSharedFlow: MutableSharedFlow<T> = MutableSharedFlow(
|
private val publicSharedFlow: MutableSharedFlow<T> = MutableSharedFlow(
|
||||||
replay = 0,
|
replay = 1,
|
||||||
extraBufferCapacity = 1,
|
extraBufferCapacity = 1,
|
||||||
onBufferOverflow = BufferOverflow.DROP_OLDEST
|
onBufferOverflow = BufferOverflow.DROP_OLDEST
|
||||||
)
|
)
|
||||||
|
|
||||||
private var _value: T = initialValue
|
override var value: T = initialValue
|
||||||
override var value: T
|
private set
|
||||||
get() {
|
|
||||||
return _value
|
|
||||||
}
|
|
||||||
set(value) {
|
|
||||||
tryEmit(value)
|
|
||||||
}
|
|
||||||
private val job = internalSharedFlow.subscribe(internalScope) {
|
private val job = internalSharedFlow.subscribe(internalScope) {
|
||||||
if (_value != it) {
|
if (value != it) {
|
||||||
_value = it
|
value = it
|
||||||
publicSharedFlow.emit(it)
|
publicSharedFlow.emit(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -46,18 +39,6 @@ interface SpecialMutableStateFlow<T> : MutableStateFlow<T> {
|
|||||||
override val subscriptionCount: StateFlow<Int>
|
override val subscriptionCount: StateFlow<Int>
|
||||||
get() = publicSharedFlow.subscriptionCount
|
get() = publicSharedFlow.subscriptionCount
|
||||||
|
|
||||||
init {
|
|
||||||
value = initialValue
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun compareAndSet(expect: T, update: T): Boolean {
|
|
||||||
return if (value == expect) {
|
|
||||||
tryEmit(update)
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExperimentalCoroutinesApi
|
@ExperimentalCoroutinesApi
|
||||||
override fun resetReplayCache() = publicSharedFlow.resetReplayCache()
|
override fun resetReplayCache() = publicSharedFlow.resetReplayCache()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user