mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-02 06:50:08 +00:00
Compare commits
No commits in common. "43232afa621a7e1aaca68bc4e4bf9efac91c9534" and "33b7c85fc258e0669157241eb51f69db79526b2c" have entirely different histories.
43232afa62
...
33b7c85fc2
10
CHANGELOG.md
10
CHANGELOG.md
@ -1,15 +1,5 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## 0.10.5
|
|
||||||
|
|
||||||
* `Versions`
|
|
||||||
* `Compose`: `1.2.0-alpha01-dev683` -> `1.2.0-alpha01-dev686`
|
|
||||||
* `Repos`
|
|
||||||
* `Android`:
|
|
||||||
* New function `SharedPreferencesKeyValueRepo`
|
|
||||||
* `FSM`
|
|
||||||
* Add `StateHandlingErrorHandler` and opportunity to handle states handling errors
|
|
||||||
|
|
||||||
## 0.10.4
|
## 0.10.4
|
||||||
|
|
||||||
* `Versions`:
|
* `Versions`:
|
||||||
|
@ -3,8 +3,6 @@ package dev.inmo.micro_utils.fsm.common
|
|||||||
import dev.inmo.micro_utils.common.Optional
|
import dev.inmo.micro_utils.common.Optional
|
||||||
import dev.inmo.micro_utils.common.onPresented
|
import dev.inmo.micro_utils.common.onPresented
|
||||||
import dev.inmo.micro_utils.coroutines.*
|
import dev.inmo.micro_utils.coroutines.*
|
||||||
import dev.inmo.micro_utils.fsm.common.utils.StateHandlingErrorHandler
|
|
||||||
import dev.inmo.micro_utils.fsm.common.utils.defaultStateHandlingErrorHandler
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
@ -15,24 +13,13 @@ import kotlinx.coroutines.sync.withLock
|
|||||||
* handling until [start] method will be called
|
* handling until [start] method will be called
|
||||||
*/
|
*/
|
||||||
interface StatesMachine<T : State> : StatesHandler<T, T> {
|
interface StatesMachine<T : State> : StatesHandler<T, T> {
|
||||||
suspend fun launchStateHandling(
|
|
||||||
state: T,
|
|
||||||
handlers: List<CheckableHandlerHolder<in T, T>>,
|
|
||||||
onStateHandlingErrorHandler: StateHandlingErrorHandler<T>
|
|
||||||
): T? {
|
|
||||||
return runCatchingSafely {
|
|
||||||
handlers.firstOrNull { it.checkHandleable(state) } ?.run {
|
|
||||||
handleState(state)
|
|
||||||
}
|
|
||||||
}.getOrElse {
|
|
||||||
onStateHandlingErrorHandler(state, it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
suspend fun launchStateHandling(
|
suspend fun launchStateHandling(
|
||||||
state: T,
|
state: T,
|
||||||
handlers: List<CheckableHandlerHolder<in T, T>>
|
handlers: List<CheckableHandlerHolder<in T, T>>
|
||||||
): T? {
|
): T? {
|
||||||
return launchStateHandling(state, handlers, defaultStateHandlingErrorHandler())
|
return handlers.firstOrNull { it.checkHandleable(state) } ?.run {
|
||||||
|
handleState(state)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -51,9 +38,8 @@ interface StatesMachine<T : State> : StatesHandler<T, T> {
|
|||||||
*/
|
*/
|
||||||
operator fun <T: State> invoke(
|
operator fun <T: State> invoke(
|
||||||
statesManager: StatesManager<T>,
|
statesManager: StatesManager<T>,
|
||||||
handlers: List<CheckableHandlerHolder<in T, T>>,
|
handlers: List<CheckableHandlerHolder<in T, T>>
|
||||||
onStateHandlingErrorHandler: StateHandlingErrorHandler<T> = defaultStateHandlingErrorHandler()
|
) = DefaultStatesMachine(statesManager, handlers)
|
||||||
) = DefaultStatesMachine(statesManager, handlers, onStateHandlingErrorHandler)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,17 +52,12 @@ interface StatesMachine<T : State> : StatesHandler<T, T> {
|
|||||||
open class DefaultStatesMachine <T: State>(
|
open class DefaultStatesMachine <T: State>(
|
||||||
protected val statesManager: StatesManager<T>,
|
protected val statesManager: StatesManager<T>,
|
||||||
protected val handlers: List<CheckableHandlerHolder<in T, T>>,
|
protected val handlers: List<CheckableHandlerHolder<in T, T>>,
|
||||||
protected val onStateHandlingErrorHandler: StateHandlingErrorHandler<T> = defaultStateHandlingErrorHandler()
|
|
||||||
) : StatesMachine<T> {
|
) : StatesMachine<T> {
|
||||||
/**
|
/**
|
||||||
* Will call [launchStateHandling] for state handling
|
* Will call [launchStateHandling] for state handling
|
||||||
*/
|
*/
|
||||||
override suspend fun StatesMachine<in T>.handleState(state: T): T? = launchStateHandling(state, handlers)
|
override suspend fun StatesMachine<in T>.handleState(state: T): T? = launchStateHandling(state, handlers)
|
||||||
|
|
||||||
override suspend fun launchStateHandling(state: T, handlers: List<CheckableHandlerHolder<in T, T>>): T? {
|
|
||||||
return launchStateHandling(state, handlers, onStateHandlingErrorHandler)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This
|
* This
|
||||||
*/
|
*/
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package dev.inmo.micro_utils.fsm.common
|
package dev.inmo.micro_utils.fsm.common
|
||||||
|
|
||||||
import dev.inmo.micro_utils.common.*
|
import dev.inmo.micro_utils.common.*
|
||||||
import dev.inmo.micro_utils.fsm.common.utils.StateHandlingErrorHandler
|
|
||||||
import dev.inmo.micro_utils.fsm.common.utils.defaultStateHandlingErrorHandler
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
|
|
||||||
@ -22,11 +20,9 @@ interface UpdatableStatesMachine<T : State> : StatesMachine<T> {
|
|||||||
open class DefaultUpdatableStatesMachine<T : State>(
|
open class DefaultUpdatableStatesMachine<T : State>(
|
||||||
statesManager: StatesManager<T>,
|
statesManager: StatesManager<T>,
|
||||||
handlers: List<CheckableHandlerHolder<in T, T>>,
|
handlers: List<CheckableHandlerHolder<in T, T>>,
|
||||||
onStateHandlingErrorHandler: StateHandlingErrorHandler<T> = defaultStateHandlingErrorHandler()
|
|
||||||
) : DefaultStatesMachine<T>(
|
) : DefaultStatesMachine<T>(
|
||||||
statesManager,
|
statesManager,
|
||||||
handlers,
|
handlers
|
||||||
onStateHandlingErrorHandler
|
|
||||||
), UpdatableStatesMachine<T> {
|
), UpdatableStatesMachine<T> {
|
||||||
protected val jobsStates = mutableMapOf<Job, T>()
|
protected val jobsStates = mutableMapOf<Job, T>()
|
||||||
|
|
||||||
@ -38,7 +34,7 @@ open class DefaultUpdatableStatesMachine<T : State>(
|
|||||||
*/
|
*/
|
||||||
override suspend fun performStateUpdate(previousState: Optional<T>, actualState: T, scope: CoroutineScope) {
|
override suspend fun performStateUpdate(previousState: Optional<T>, actualState: T, scope: CoroutineScope) {
|
||||||
statesJobsMutex.withLock {
|
statesJobsMutex.withLock {
|
||||||
if (shouldReplaceJob(previousState, actualState)) {
|
if (compare(previousState, actualState)) {
|
||||||
statesJobs[actualState] ?.cancel()
|
statesJobs[actualState] ?.cancel()
|
||||||
}
|
}
|
||||||
val job = previousState.mapOnPresented {
|
val job = previousState.mapOnPresented {
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
package dev.inmo.micro_utils.fsm.common.utils
|
|
||||||
|
|
||||||
typealias StateHandlingErrorHandler<T> = suspend (T, Throwable) -> T?
|
|
||||||
val DefaultStateHandlingErrorHandler: StateHandlingErrorHandler<*> = { _, _ -> null }
|
|
||||||
inline fun <T> defaultStateHandlingErrorHandler(): StateHandlingErrorHandler<T> = DefaultStateHandlingErrorHandler as StateHandlingErrorHandler<T>
|
|
||||||
|
|
@ -14,5 +14,5 @@ crypto_js_version=4.1.1
|
|||||||
# Project data
|
# Project data
|
||||||
|
|
||||||
group=dev.inmo
|
group=dev.inmo
|
||||||
version=0.10.5
|
version=0.10.4
|
||||||
android_code_version=120
|
android_code_version=119
|
||||||
|
@ -4,7 +4,7 @@ kt = "1.6.21"
|
|||||||
kt-serialization = "1.3.3"
|
kt-serialization = "1.3.3"
|
||||||
kt-coroutines = "1.6.1"
|
kt-coroutines = "1.6.1"
|
||||||
|
|
||||||
jb-compose = "1.2.0-alpha01-dev686"
|
jb-compose = "1.2.0-alpha01-dev683"
|
||||||
jb-exposed = "0.38.2"
|
jb-exposed = "0.38.2"
|
||||||
jb-dokka = "1.6.21"
|
jb-dokka = "1.6.21"
|
||||||
|
|
||||||
@ -16,7 +16,7 @@ ktor = "2.0.1"
|
|||||||
gh-release = "2.3.7"
|
gh-release = "2.3.7"
|
||||||
|
|
||||||
android-gradle = "7.0.4"
|
android-gradle = "7.0.4"
|
||||||
dexcount = "3.1.0"
|
dexcount = "3.0.1"
|
||||||
|
|
||||||
android-coreKtx = "1.7.0"
|
android-coreKtx = "1.7.0"
|
||||||
android-recyclerView = "1.2.1"
|
android-recyclerView = "1.2.1"
|
||||||
|
@ -160,9 +160,3 @@ class KeyValueStore<T : Any> internal constructor (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <T : Any> SharedPreferencesKeyValueRepo(
|
|
||||||
context: Context,
|
|
||||||
name: String = "default",
|
|
||||||
cacheValues: Boolean = false
|
|
||||||
) = context.keyValueStore<T>(name, cacheValues)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user