mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-18 14:47:15 +00:00
StatesMachine is interface
This commit is contained in:
parent
119a0588cc
commit
7d3b1f8e75
@ -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 handlers: List<StateHandlerHolder<*>>
|
||||
) : StatesHandler<State> {
|
||||
) : StatesMachine {
|
||||
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 newState = launchStateHandling(state, handlers)
|
||||
if (newState != null) {
|
||||
@ -40,7 +55,7 @@ class StatesMachine (
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun startChain(state: State) {
|
||||
override suspend fun startChain(state: State) {
|
||||
statesManager.startChain(state)
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,12 @@
|
||||
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 = InMemoryStatesManager()
|
||||
var statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo())
|
||||
) {
|
||||
private var states = mutableListOf<StateHandlerHolder<*>>()
|
||||
|
||||
|
@ -40,8 +40,8 @@ interface DefaultStatesManagerRepo {
|
||||
* any event will be sent to [onChainStateUpdated], [onStartChain] or [onEndChain].
|
||||
*/
|
||||
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 {
|
||||
private val _onChainStateUpdated = MutableSharedFlow<Pair<State, State>>(0)
|
||||
override val onChainStateUpdated: Flow<Pair<State, State>> = _onChainStateUpdated.asSharedFlow()
|
||||
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user