From 7d3b1f8e7570c17471aa52e5a037ed9d4973d7ac Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 6 Oct 2021 13:56:58 +0600 Subject: [PATCH] StatesMachine is interface --- .../micro_utils/fsm/common/StatesMachine.kt | 23 +++++++++++++++---- .../micro_utils/fsm/common/dsl/FSMBuilder.kt | 4 +++- .../common/managers/DefaultStatesManager.kt | 4 ++-- .../InMemoryDefaultStatesManagerRepo.kt | 21 +++++++++++++++++ 4 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/managers/InMemoryDefaultStatesManagerRepo.kt diff --git a/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/StatesMachine.kt b/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/StatesMachine.kt index 5c2ea9592d9..dde2cdc11b2 100644 --- a/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/StatesMachine.kt +++ b/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/StatesMachine.kt @@ -13,13 +13,28 @@ private suspend fun StatesMachine.launchStateHandling( } } -class StatesMachine ( +interface StatesMachine : StatesHandler { + fun start(scope: CoroutineScope): Job + suspend fun startChain(state: State) + + companion object { + /** + * Creates [DefaultStatesMachine] + */ + operator fun invoke( + statesManager: StatesManager, + handlers: List> + ) = DefaultStatesMachine(statesManager, handlers) + } +} + +class DefaultStatesMachine ( private val statesManager: StatesManager, private val handlers: List> -) : StatesHandler { +) : 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) } } diff --git a/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/dsl/FSMBuilder.kt b/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/dsl/FSMBuilder.kt index f0694f360ef..a5fd74a5ddb 100644 --- a/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/dsl/FSMBuilder.kt +++ b/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/dsl/FSMBuilder.kt @@ -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>() diff --git a/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/managers/DefaultStatesManager.kt b/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/managers/DefaultStatesManager.kt index dda9ae1729e..3479ff53637 100644 --- a/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/managers/DefaultStatesManager.kt +++ b/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/managers/DefaultStatesManager.kt @@ -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>(0) override val onChainStateUpdated: Flow> = _onChainStateUpdated.asSharedFlow() diff --git a/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/managers/InMemoryDefaultStatesManagerRepo.kt b/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/managers/InMemoryDefaultStatesManagerRepo.kt new file mode 100644 index 00000000000..211d3f5370d --- /dev/null +++ b/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/managers/InMemoryDefaultStatesManagerRepo.kt @@ -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 = 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 = map.values.toList() + + override suspend fun getContextState(context: Any): State? = map[context] + + override suspend fun contains(context: Any): Boolean = map.contains(context) +}