mirror of
				https://github.com/InsanusMokrassar/MicroUtils.git
				synced 2025-10-30 19:50:31 +00:00 
			
		
		
		
	fixes
This commit is contained in:
		| @@ -9,35 +9,36 @@ import kotlinx.coroutines.CoroutineScope | |||||||
| import kotlinx.coroutines.Dispatchers | import kotlinx.coroutines.Dispatchers | ||||||
|  |  | ||||||
| class FlowState<T>( | class FlowState<T>( | ||||||
|     private val state: MutableState<T>, |     initial: T, | ||||||
|     internalScope: CoroutineScope = CoroutineScope(Dispatchers.Main) |     internalScope: CoroutineScope = CoroutineScope(Dispatchers.Main) | ||||||
| ) : MutableState<T>, | ) : MutableState<T>, | ||||||
|     SpecialMutableStateFlow.Default<T>(state.value, internalScope) { |     SpecialMutableStateFlow.Default<T>(initial, internalScope) { | ||||||
|  |     private var internalValue: T = initial | ||||||
|     override var value: T |     override var value: T | ||||||
|         get() = super.value |         get() = internalValue | ||||||
|         set(value) { |         set(value) { | ||||||
|  |             internalValue = value | ||||||
|             tryEmit(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) { |     override suspend fun onChange(value: T) { | ||||||
|  |         internalValue = value | ||||||
|         super.onChange(value) |         super.onChange(value) | ||||||
|  |     } | ||||||
|  |  | ||||||
|         if (state.value != value) { |     override fun component1(): T = value | ||||||
|             doInUI { |  | ||||||
|                 state.value = value |     override fun component2(): (T) -> Unit = { tryEmit(it) } | ||||||
|             } |  | ||||||
|  |     override fun tryEmit(value: T): Boolean { | ||||||
|  |         internalValue = value | ||||||
|  |         return super.tryEmit(value) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     override suspend fun emit(value: T) { | ||||||
|  |         internalValue = value | ||||||
|  |         super.emit(value) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| fun <T> MutableState<T>.asFlowState(scope: CoroutineScope = CoroutineScope(Dispatchers.Main)) = FlowState(this, scope) | //fun <T> MutableState<T>.asFlowState(scope: CoroutineScope = CoroutineScope(Dispatchers.Main)) = FlowState(this, scope) | ||||||
|   | |||||||
| @@ -16,7 +16,7 @@ interface SpecialMutableStateFlow<T> : StateFlow<T>, FlowCollector<T>, MutableSh | |||||||
|     ) : SpecialMutableStateFlow<T> { |     ) : SpecialMutableStateFlow<T> { | ||||||
|         protected val internalSharedFlow: MutableSharedFlow<T> = MutableSharedFlow( |         protected val internalSharedFlow: MutableSharedFlow<T> = MutableSharedFlow( | ||||||
|             replay = 0, |             replay = 0, | ||||||
|             extraBufferCapacity = 1, |             extraBufferCapacity = 2, | ||||||
|             onBufferOverflow = BufferOverflow.DROP_OLDEST |             onBufferOverflow = BufferOverflow.DROP_OLDEST | ||||||
|         ) |         ) | ||||||
|         protected val publicSharedFlow: MutableSharedFlow<T> = MutableSharedFlow( |         protected val publicSharedFlow: MutableSharedFlow<T> = MutableSharedFlow( | ||||||
| @@ -32,7 +32,7 @@ interface SpecialMutableStateFlow<T> : StateFlow<T>, FlowCollector<T>, MutableSh | |||||||
|             _value = value |             _value = value | ||||||
|             publicSharedFlow.emit(value) |             publicSharedFlow.emit(value) | ||||||
|         } |         } | ||||||
|         protected val job = internalSharedFlow.subscribeSafelyWithoutExceptions(internalScope) { |         protected val job = internalSharedFlow.subscribe(internalScope) { | ||||||
|             if (_value != it) { |             if (_value != it) { | ||||||
|                 onChange(it) |                 onChange(it) | ||||||
|             } |             } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user