StatesMachine is interface

This commit is contained in:
InsanusMokrassar 2021-10-06 13:56:58 +06:00
parent 119a0588cc
commit 7d3b1f8e75
4 changed files with 45 additions and 7 deletions

View File

@ -13,13 +13,28 @@ private suspend fun <I : State> StatesMachine.launchStateHandling(
} }
} }
class StatesMachine ( interface StatesMachine : StatesHandler<State> {
fun start(scope: CoroutineScope): Job
suspend fun startChain(state: State)
companion object {
/**
* Creates [DefaultStatesMachine]
*/
operator fun invoke(
statesManager: StatesManager,
handlers: List<StateHandlerHolder<*>>
) = DefaultStatesMachine(statesManager, handlers)
}
}
class DefaultStatesMachine (
private val statesManager: StatesManager, private val statesManager: StatesManager,
private val handlers: List<StateHandlerHolder<*>> private val handlers: List<StateHandlerHolder<*>>
) : StatesHandler<State> { ) : StatesMachine {
override suspend fun StatesMachine.handleState(state: State): State? = launchStateHandling(state, handlers) override suspend fun StatesMachine.handleState(state: State): State? = launchStateHandling(state, handlers)
fun start(scope: CoroutineScope): Job = scope.launchSafelyWithoutExceptions { override fun start(scope: CoroutineScope): Job = scope.launchSafelyWithoutExceptions {
val statePerformer: suspend (State) -> Unit = { state: State -> val statePerformer: suspend (State) -> Unit = { state: State ->
val newState = launchStateHandling(state, handlers) val newState = launchStateHandling(state, handlers)
if (newState != null) { if (newState != null) {
@ -40,7 +55,7 @@ class StatesMachine (
} }
} }
suspend fun startChain(state: State) { override suspend fun startChain(state: State) {
statesManager.startChain(state) statesManager.startChain(state)
} }
} }

View File

@ -1,10 +1,12 @@
package dev.inmo.micro_utils.fsm.common.dsl 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.InMemoryStatesManager
import kotlin.reflect.KClass import kotlin.reflect.KClass
class FSMBuilder( class FSMBuilder(
var statesManager: StatesManager = InMemoryStatesManager() var statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo())
) { ) {
private var states = mutableListOf<StateHandlerHolder<*>>() private var states = mutableListOf<StateHandlerHolder<*>>()

View File

@ -40,8 +40,8 @@ interface DefaultStatesManagerRepo {
* any event will be sent to [onChainStateUpdated], [onStartChain] or [onEndChain]. * any event will be sent to [onChainStateUpdated], [onStartChain] or [onEndChain].
*/ */
class DefaultStatesManager( class DefaultStatesManager(
private val onContextsConflictResolver: suspend (old: State, new: State, currentNew: State) -> Boolean = { _, _, _ -> true }, private val repo: DefaultStatesManagerRepo,
private val repo: DefaultStatesManagerRepo private val onContextsConflictResolver: suspend (old: State, new: State, currentNew: State) -> Boolean = { _, _, _ -> true }
) : StatesManager { ) : StatesManager {
private val _onChainStateUpdated = MutableSharedFlow<Pair<State, State>>(0) private val _onChainStateUpdated = MutableSharedFlow<Pair<State, State>>(0)
override val onChainStateUpdated: Flow<Pair<State, State>> = _onChainStateUpdated.asSharedFlow() override val onChainStateUpdated: Flow<Pair<State, State>> = _onChainStateUpdated.asSharedFlow()

View File

@ -0,0 +1,21 @@
package dev.inmo.micro_utils.fsm.common.managers
import dev.inmo.micro_utils.fsm.common.State
class InMemoryDefaultStatesManagerRepo(
private val map: MutableMap<Any, State> = mutableMapOf()
) : DefaultStatesManagerRepo {
override suspend fun set(state: State) {
map[state.context] = state
}
override suspend fun removeState(state: State) {
map.remove(state.context)
}
override suspend fun getStates(): List<State> = map.values.toList()
override suspend fun getContextState(context: Any): State? = map[context]
override suspend fun contains(context: Any): Boolean = map.contains(context)
}