improvements in FSM

This commit is contained in:
InsanusMokrassar 2021-11-12 13:19:15 +06:00
parent 50ea40bc3a
commit 2a4cb8c5f9
3 changed files with 13 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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>) {