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`: * `Versions`:
* `Klock`: `2.4.7` -> `2.4.8` * `Klock`: `2.4.7` -> `2.4.8`
* `Serialization`: `1.3.0` -> `1.3.1` * `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 ## 0.8.1

View File

@ -12,7 +12,7 @@ import kotlinx.coroutines.*
interface StatesMachine<T : State> : StatesHandler<T, T> { interface StatesMachine<T : State> : StatesHandler<T, T> {
suspend fun launchStateHandling( suspend fun launchStateHandling(
state: T, state: T,
handlers: List<CustomizableHandlerHolder<in T, T>> handlers: List<CheckableHandlerHolder<in T, T>>
): T? { ): T? {
return handlers.firstOrNull { it.checkHandleable(state) } ?.run { return handlers.firstOrNull { it.checkHandleable(state) } ?.run {
handleState(state) handleState(state)
@ -35,7 +35,7 @@ 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<CustomizableHandlerHolder<in T, T>> handlers: List<CheckableHandlerHolder<in T, T>>
) = DefaultStatesMachine(statesManager, handlers) ) = DefaultStatesMachine(statesManager, handlers)
} }
} }
@ -46,7 +46,7 @@ interface StatesMachine<T : State> : StatesHandler<T, T> {
*/ */
class DefaultStatesMachine <T: State>( class DefaultStatesMachine <T: State>(
private val statesManager: StatesManager<T>, private val statesManager: StatesManager<T>,
private val handlers: List<CustomizableHandlerHolder<in T, T>> private val handlers: List<CheckableHandlerHolder<in T, T>>
) : StatesMachine<T> { ) : StatesMachine<T> {
/** /**
* Will call [launchStateHandling] for state handling * Will call [launchStateHandling] for state handling

View File

@ -9,14 +9,18 @@ class FSMBuilder<T : State>(
var statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), var statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
var defaultStateHandler: StatesHandler<T, T>? = StatesHandler { null } 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>) { 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>) { 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>) { fun <I : T> addStrict(kClass: KClass<I>, handler: StatesHandler<I, T>) {