mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-19 23:27:15 +00:00
improvements in FSM
This commit is contained in:
parent
50ea40bc3a
commit
2a4cb8c5f9
@ -5,6 +5,9 @@
|
||||
* `Versions`:
|
||||
* `Klock`: `2.4.7` -> `2.4.8`
|
||||
* `Serialization`: `1.3.0` -> `1.3.1`
|
||||
* `FSM`:
|
||||
* Now it is possible to pass any `CheckableHandlerHolder` in `FSMBuilder`
|
||||
* Now `StatesMachine` works with `CheckableHandlerHolder` instead of `CustomizableHandlerHolder`
|
||||
|
||||
## 0.8.1
|
||||
|
||||
|
@ -12,7 +12,7 @@ import kotlinx.coroutines.*
|
||||
interface StatesMachine<T : State> : StatesHandler<T, T> {
|
||||
suspend fun launchStateHandling(
|
||||
state: T,
|
||||
handlers: List<CustomizableHandlerHolder<in T, T>>
|
||||
handlers: List<CheckableHandlerHolder<in T, T>>
|
||||
): T? {
|
||||
return handlers.firstOrNull { it.checkHandleable(state) } ?.run {
|
||||
handleState(state)
|
||||
@ -35,7 +35,7 @@ interface StatesMachine<T : State> : StatesHandler<T, T> {
|
||||
*/
|
||||
operator fun <T: State> invoke(
|
||||
statesManager: StatesManager<T>,
|
||||
handlers: List<CustomizableHandlerHolder<in T, T>>
|
||||
handlers: List<CheckableHandlerHolder<in T, T>>
|
||||
) = DefaultStatesMachine(statesManager, handlers)
|
||||
}
|
||||
}
|
||||
@ -46,7 +46,7 @@ interface StatesMachine<T : State> : StatesHandler<T, T> {
|
||||
*/
|
||||
class DefaultStatesMachine <T: State>(
|
||||
private val statesManager: StatesManager<T>,
|
||||
private val handlers: List<CustomizableHandlerHolder<in T, T>>
|
||||
private val handlers: List<CheckableHandlerHolder<in T, T>>
|
||||
) : StatesMachine<T> {
|
||||
/**
|
||||
* Will call [launchStateHandling] for state handling
|
||||
|
@ -9,14 +9,18 @@ class FSMBuilder<T : State>(
|
||||
var statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
|
||||
var defaultStateHandler: StatesHandler<T, T>? = StatesHandler { null }
|
||||
) {
|
||||
private var states = mutableListOf<CustomizableHandlerHolder<T, T>>()
|
||||
private var states = mutableListOf<CheckableHandlerHolder<T, T>>()
|
||||
|
||||
fun add(handler: CheckableHandlerHolder<T, T>) {
|
||||
states.add(handler)
|
||||
}
|
||||
|
||||
fun <I : T> add(kClass: KClass<I>, handler: StatesHandler<I, T>) {
|
||||
states.add(CheckableHandlerHolder(kClass, false, handler))
|
||||
add(CheckableHandlerHolder(kClass, false, handler))
|
||||
}
|
||||
|
||||
fun <I : T> add(filter: suspend (state: State) -> Boolean, handler: StatesHandler<I, T>) {
|
||||
states.add(handler.holder(filter))
|
||||
add(handler.holder(filter))
|
||||
}
|
||||
|
||||
fun <I : T> addStrict(kClass: KClass<I>, handler: StatesHandler<I, T>) {
|
||||
|
Loading…
Reference in New Issue
Block a user