mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-17 13:53:49 +00:00
solution of #109
This commit is contained in:
parent
67ff9cc9b3
commit
de3d14dc41
@ -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
|
||||
|
||||
|
@ -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
|
||||
)
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user