solution of

This commit is contained in:
InsanusMokrassar 2021-11-03 15:36:25 +06:00
parent 67ff9cc9b3
commit de3d14dc41
4 changed files with 27 additions and 12 deletions
CHANGELOG.md
fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common

View File

@ -7,6 +7,9 @@
* `Ktor`: `1.6.4` -> `1.6.5`
* `Common`:
* Type `Either` got its own serializer
* `FSM`:
* `Common`:
* Add opportunity for comfortable adding default state handler
## 0.7.4

View File

@ -6,7 +6,7 @@ import kotlin.reflect.KClass
* Default realization of [StatesHandler]. It will incapsulate checking of [State] type in [checkHandleable] and class
* casting in [handleState]
*/
class StateHandlerHolder<I : State>(
class StatesHandlerHolder<I : State>(
private val inputKlass: KClass<I>,
private val strict: Boolean = false,
private val delegateTo: StatesHandler<I>
@ -19,9 +19,20 @@ class StateHandlerHolder<I : State>(
/**
* Calls [delegateTo] method [StatesHandler.handleState] with [state] casted to [I]. Use [checkHandleable]
* to be sure that this [StateHandlerHolder] will be able to handle [state]
* to be sure that this [StatesHandlerHolder] will be able to handle [state]
*/
override suspend fun StatesMachine.handleState(state: State): State? {
return delegateTo.run { handleState(state as I) }
}
}
@Deprecated("Renamed", ReplaceWith("StatesHandlerHolder"))
typealias StateHandlerHolder<T> = StatesHandlerHolder<T>
inline fun <reified T : State> StatesHandler<T>.holder(
strict: Boolean = true
) = StatesHandlerHolder(
T::class,
strict,
this
)

View File

@ -2,11 +2,10 @@ package dev.inmo.micro_utils.fsm.common
import dev.inmo.micro_utils.coroutines.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.asFlow
private suspend fun <I : State> StatesMachine.launchStateHandling(
state: State,
handlers: List<StateHandlerHolder<out I>>
handlers: List<StatesHandlerHolder<out I>>
): State? {
return handlers.firstOrNull { it.checkHandleable(state) } ?.run {
handleState(state)
@ -35,7 +34,7 @@ interface StatesMachine : StatesHandler<State> {
*/
operator fun invoke(
statesManager: StatesManager,
handlers: List<StateHandlerHolder<*>>
handlers: List<StatesHandlerHolder<*>>
) = DefaultStatesMachine(statesManager, handlers)
}
}
@ -46,7 +45,7 @@ interface StatesMachine : StatesHandler<State> {
*/
class DefaultStatesMachine (
private val statesManager: StatesManager,
private val handlers: List<StateHandlerHolder<*>>
private val handlers: List<StatesHandlerHolder<*>>
) : StatesMachine {
/**
* Will call [launchStateHandling] for state handling

View File

@ -2,25 +2,27 @@ package dev.inmo.micro_utils.fsm.common.dsl
import dev.inmo.micro_utils.fsm.common.*
import dev.inmo.micro_utils.fsm.common.managers.*
import dev.inmo.micro_utils.fsm.common.managers.InMemoryStatesManager
import kotlin.reflect.KClass
class FSMBuilder(
var statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo())
var statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
var defaultStateHandler: StatesHandler<State>? = StatesHandler { null }
) {
private var states = mutableListOf<StateHandlerHolder<*>>()
private var states = mutableListOf<StatesHandlerHolder<*>>()
fun <I : State> add(kClass: KClass<I>, handler: StatesHandler<I>) {
states.add(StateHandlerHolder(kClass, false, handler))
states.add(StatesHandlerHolder(kClass, false, handler))
}
fun <I : State> addStrict(kClass: KClass<I>, handler: StatesHandler<I>) {
states.add(StateHandlerHolder(kClass, true, handler))
states.add(StatesHandlerHolder(kClass, true, handler))
}
fun build() = StatesMachine(
statesManager,
states.toList()
states.toList().let { list ->
defaultStateHandler ?.let { list + it.holder(false) } ?: list
}
)
}