mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-18 14:47:15 +00:00
FlowState
This commit is contained in:
parent
9b30c3a155
commit
22d7ac3e22
@ -0,0 +1,43 @@
|
||||
package dev.inmo.micro_utils.coroutines.compose
|
||||
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.compose.runtime.State
|
||||
import androidx.compose.runtime.derivedStateOf
|
||||
import dev.inmo.micro_utils.coroutines.SpecialMutableStateFlow
|
||||
import dev.inmo.micro_utils.coroutines.doInUI
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
class FlowState<T>(
|
||||
private val state: MutableState<T>,
|
||||
internalScope: CoroutineScope = CoroutineScope(Dispatchers.Main)
|
||||
) : MutableState<T>,
|
||||
SpecialMutableStateFlow.Default<T>(state.value, internalScope) {
|
||||
override var value: T
|
||||
get() = super.value
|
||||
set(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) {
|
||||
super.onChange(value)
|
||||
|
||||
if (state.value != value) {
|
||||
doInUI {
|
||||
state.value = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> MutableState<T>.asFlowState(scope: CoroutineScope = CoroutineScope(Dispatchers.Main)) = FlowState(this, scope)
|
@ -10,27 +10,31 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
|
||||
interface SpecialMutableStateFlow<T> : StateFlow<T>, FlowCollector<T>, MutableSharedFlow<T> {
|
||||
class Default<T>(
|
||||
open class Default<T>(
|
||||
initialValue: T,
|
||||
internalScope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||
) : SpecialMutableStateFlow<T> {
|
||||
private val internalSharedFlow: MutableSharedFlow<T> = MutableSharedFlow(
|
||||
protected val internalSharedFlow: MutableSharedFlow<T> = MutableSharedFlow(
|
||||
replay = 0,
|
||||
extraBufferCapacity = 1,
|
||||
onBufferOverflow = BufferOverflow.DROP_OLDEST
|
||||
)
|
||||
private val publicSharedFlow: MutableSharedFlow<T> = MutableSharedFlow(
|
||||
protected val publicSharedFlow: MutableSharedFlow<T> = MutableSharedFlow(
|
||||
replay = 1,
|
||||
extraBufferCapacity = 1,
|
||||
onBufferOverflow = BufferOverflow.DROP_OLDEST
|
||||
)
|
||||
|
||||
override var value: T = initialValue
|
||||
private set
|
||||
private val job = internalSharedFlow.subscribe(internalScope) {
|
||||
if (value != it) {
|
||||
value = it
|
||||
publicSharedFlow.emit(it)
|
||||
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.subscribeSafelyWithoutExceptions(internalScope) {
|
||||
if (_value != it) {
|
||||
onChange(it)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,7 @@ kt-coroutines = "1.7.3"
|
||||
kslog = "1.3.1"
|
||||
|
||||
jb-compose = "1.5.11"
|
||||
jb-exposed = "0.44.1"
|
||||
jb-exposed = "0.45.0"
|
||||
jb-dokka = "1.9.10"
|
||||
|
||||
korlibs = "4.0.10"
|
||||
@ -21,7 +21,7 @@ koin = "3.5.0"
|
||||
|
||||
okio = "3.6.0"
|
||||
|
||||
ksp = "1.9.20-1.0.14"
|
||||
ksp = "1.9.21-1.0.15"
|
||||
kotlin-poet = "1.15.1"
|
||||
|
||||
versions = "0.50.0"
|
||||
|
Loading…
Reference in New Issue
Block a user