From de3d14dc41f20006ace53f80b0c79208379cf6aa Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 3 Nov 2021 15:36:25 +0600 Subject: [PATCH] solution of #109 --- CHANGELOG.md | 3 +++ ...ateHandlerHolder.kt => StatesHandlerHolder.kt} | 15 +++++++++++++-- .../inmo/micro_utils/fsm/common/StatesMachine.kt | 7 +++---- .../inmo/micro_utils/fsm/common/dsl/FSMBuilder.kt | 14 ++++++++------ 4 files changed, 27 insertions(+), 12 deletions(-) rename fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/{StateHandlerHolder.kt => StatesHandlerHolder.kt} (71%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 261fbce2879..7c19c45fe22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/StateHandlerHolder.kt b/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/StatesHandlerHolder.kt similarity index 71% rename from fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/StateHandlerHolder.kt rename to fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/StatesHandlerHolder.kt index a92f9b96602..b849ba8462d 100644 --- a/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/StateHandlerHolder.kt +++ b/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/StatesHandlerHolder.kt @@ -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( +class StatesHandlerHolder( private val inputKlass: KClass, private val strict: Boolean = false, private val delegateTo: StatesHandler @@ -19,9 +19,20 @@ class StateHandlerHolder( /** * 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 = StatesHandlerHolder + +inline fun StatesHandler.holder( + strict: Boolean = true +) = StatesHandlerHolder( + T::class, + strict, + this +) 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 11170c5b030..3dc9ef89cfa 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 @@ -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 StatesMachine.launchStateHandling( state: State, - handlers: List> + handlers: List> ): State? { return handlers.firstOrNull { it.checkHandleable(state) } ?.run { handleState(state) @@ -35,7 +34,7 @@ interface StatesMachine : StatesHandler { */ operator fun invoke( statesManager: StatesManager, - handlers: List> + handlers: List> ) = DefaultStatesMachine(statesManager, handlers) } } @@ -46,7 +45,7 @@ interface StatesMachine : StatesHandler { */ class DefaultStatesMachine ( private val statesManager: StatesManager, - private val handlers: List> + private val handlers: List> ) : StatesMachine { /** * Will call [launchStateHandling] for state handling 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 a5fd74a5ddb..11d39ec0752 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 @@ -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? = StatesHandler { null } ) { - private var states = mutableListOf>() + private var states = mutableListOf>() fun add(kClass: KClass, handler: StatesHandler) { - states.add(StateHandlerHolder(kClass, false, handler)) + states.add(StatesHandlerHolder(kClass, false, handler)) } fun addStrict(kClass: KClass, handler: StatesHandler) { - 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 + } ) }