Compare commits

...

8 Commits

5 changed files with 55 additions and 19 deletions

View File

@@ -1,5 +1,17 @@
# Changelog
## 0.20.18
* `Coroutines`:
* `SpecialMutableStateFlow` now extends `MutableStateFlow`
* `Compose`:
* Deprecate `FlowState` due to its complexity in fixes
## 0.20.17
* `Versions`:
* `Serialization`: `1.6.1` -> `1.6.2`
## 0.20.16
* `Versions`:

View File

@@ -1,10 +1,7 @@
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
@@ -12,6 +9,7 @@ import kotlinx.coroutines.Dispatchers
* This type works like [MutableState], [kotlinx.coroutines.flow.StateFlow] and [kotlinx.coroutines.flow.MutableSharedFlow].
* Based on [SpecialMutableStateFlow]
*/
@Deprecated("Will be removed soon")
class FlowState<T>(
initial: T,
internalScope: CoroutineScope = CoroutineScope(Dispatchers.Default)
@@ -25,9 +23,9 @@ class FlowState<T>(
tryEmit(value)
}
override suspend fun onChange(value: T) {
override fun onChangeWithoutSync(value: T) {
internalValue = value
super.onChange(value)
super.onChangeWithoutSync(value)
}
override fun component1(): T = value

View File

@@ -3,10 +3,14 @@ package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.internal.SynchronizedObject
import kotlinx.coroutines.internal.synchronized
/**
* Works like [StateFlow], but guarantee that latest value update will always be delivered to
@@ -15,7 +19,9 @@ import kotlinx.coroutines.flow.StateFlow
open class SpecialMutableStateFlow<T>(
initialValue: T,
internalScope: CoroutineScope = CoroutineScope(Dispatchers.Default)
) : StateFlow<T>, FlowCollector<T>, MutableSharedFlow<T> {
) : MutableStateFlow<T>, FlowCollector<T>, MutableSharedFlow<T> {
@OptIn(InternalCoroutinesApi::class)
private val syncObject = SynchronizedObject()
protected val internalSharedFlow: MutableSharedFlow<T> = MutableSharedFlow(
replay = 0,
extraBufferCapacity = 2,
@@ -28,16 +34,13 @@ open class SpecialMutableStateFlow<T>(
)
protected var _value: T = initialValue
override val value: T
override var 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)
set(value) {
doOnChangeAction(value)
}
protected val job = internalSharedFlow.subscribe(internalScope) {
doOnChangeAction(it)
}
override val replayCache: List<T>
@@ -45,6 +48,29 @@ open class SpecialMutableStateFlow<T>(
override val subscriptionCount: StateFlow<Int>
get() = publicSharedFlow.subscriptionCount
@OptIn(InternalCoroutinesApi::class)
override fun compareAndSet(expect: T, update: T): Boolean {
return synchronized(syncObject) {
if (expect == _value && update != _value) {
doOnChangeAction(update)
}
expect == _value
}
}
protected open fun onChangeWithoutSync(value: T) {
_value = value
publicSharedFlow.tryEmit(value)
}
@OptIn(InternalCoroutinesApi::class)
protected open fun doOnChangeAction(value: T) {
synchronized(syncObject) {
if (_value != value) {
onChangeWithoutSync(value)
}
}
}
@ExperimentalCoroutinesApi
override fun resetReplayCache() = publicSharedFlow.resetReplayCache()

View File

@@ -15,5 +15,5 @@ crypto_js_version=4.1.1
# Project data
group=dev.inmo
version=0.20.16
android_code_version=222
version=0.20.18
android_code_version=224

View File

@@ -1,7 +1,7 @@
[versions]
kt = "1.9.21"
kt-serialization = "1.6.1"
kt-serialization = "1.6.2"
kt-coroutines = "1.7.3"
kslog = "1.3.1"
@@ -22,11 +22,11 @@ koin = "3.5.0"
okio = "3.6.0"
ksp = "1.9.21-1.0.15"
kotlin-poet = "1.15.1"
kotlin-poet = "1.15.2"
versions = "0.50.0"
android-gradle = "8.1.4"
android-gradle = "8.2.0"
dexcount = "4.0.0"
android-coreKtx = "1.12.0"