solution of #109

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

View File

@ -7,6 +7,9 @@
* `Ktor`: `1.6.4` -> `1.6.5` * `Ktor`: `1.6.4` -> `1.6.5`
* `Common`: * `Common`:
* Type `Either` got its own serializer * Type `Either` got its own serializer
* `FSM`:
* `Common`:
* Add opportunity for comfortable adding default state handler
## 0.7.4 ## 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 * Default realization of [StatesHandler]. It will incapsulate checking of [State] type in [checkHandleable] and class
* casting in [handleState] * casting in [handleState]
*/ */
class StateHandlerHolder<I : State>( class StatesHandlerHolder<I : State>(
private val inputKlass: KClass<I>, private val inputKlass: KClass<I>,
private val strict: Boolean = false, private val strict: Boolean = false,
private val delegateTo: StatesHandler<I> 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] * 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? { override suspend fun StatesMachine.handleState(state: State): State? {
return delegateTo.run { handleState(state as I) } 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 dev.inmo.micro_utils.coroutines.*
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.flow.asFlow
private suspend fun <I : State> StatesMachine.launchStateHandling( private suspend fun <I : State> StatesMachine.launchStateHandling(
state: State, state: State,
handlers: List<StateHandlerHolder<out I>> handlers: List<StatesHandlerHolder<out I>>
): State? { ): State? {
return handlers.firstOrNull { it.checkHandleable(state) } ?.run { return handlers.firstOrNull { it.checkHandleable(state) } ?.run {
handleState(state) handleState(state)
@ -35,7 +34,7 @@ interface StatesMachine : StatesHandler<State> {
*/ */
operator fun invoke( operator fun invoke(
statesManager: StatesManager, statesManager: StatesManager,
handlers: List<StateHandlerHolder<*>> handlers: List<StatesHandlerHolder<*>>
) = DefaultStatesMachine(statesManager, handlers) ) = DefaultStatesMachine(statesManager, handlers)
} }
} }
@ -46,7 +45,7 @@ interface StatesMachine : StatesHandler<State> {
*/ */
class DefaultStatesMachine ( class DefaultStatesMachine (
private val statesManager: StatesManager, private val statesManager: StatesManager,
private val handlers: List<StateHandlerHolder<*>> private val handlers: List<StatesHandlerHolder<*>>
) : StatesMachine { ) : StatesMachine {
/** /**
* Will call [launchStateHandling] for state handling * 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.*
import dev.inmo.micro_utils.fsm.common.managers.* import dev.inmo.micro_utils.fsm.common.managers.*
import dev.inmo.micro_utils.fsm.common.managers.InMemoryStatesManager
import kotlin.reflect.KClass import kotlin.reflect.KClass
class FSMBuilder( 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>) { 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>) { 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( fun build() = StatesMachine(
statesManager, statesManager,
states.toList() states.toList().let { list ->
defaultStateHandler ?.let { list + it.holder(false) } ?: list
}
) )
} }