mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-09 02:00:01 +00:00
updates in FSM
This commit is contained in:
@@ -10,6 +10,7 @@ kotlin {
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
api project(":micro_utils.common")
|
||||
api project(":micro_utils.coroutines")
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,11 @@
|
||||
package dev.inmo.micro_utils.fsm.common
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.micro_utils.common.Optional
|
||||
import dev.inmo.micro_utils.common.onPresented
|
||||
import dev.inmo.micro_utils.coroutines.*
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
|
||||
/**
|
||||
* Default [StatesMachine] may [startChain] and use inside logic for handling [State]s. By default you may use
|
||||
@@ -42,17 +45,53 @@ interface StatesMachine<T : State> : StatesHandler<T, T> {
|
||||
|
||||
/**
|
||||
* Default realization of [StatesMachine]. It uses [statesManager] for incapsulation of [State]s storing and contexts
|
||||
* resolving, and uses [launchStateHandling] for [State] handling
|
||||
* resolving, and uses [launchStateHandling] for [State] handling.
|
||||
*
|
||||
* This class suppose to be extended in case you wish some custom behaviour inside of [launchStateHandling], for example
|
||||
*/
|
||||
class DefaultStatesMachine <T: State>(
|
||||
private val statesManager: StatesManager<T>,
|
||||
private val handlers: List<CheckableHandlerHolder<in T, T>>
|
||||
open class DefaultStatesMachine <T: State>(
|
||||
protected val statesManager: StatesManager<T>,
|
||||
protected val handlers: List<CheckableHandlerHolder<in T, T>>,
|
||||
) : StatesMachine<T> {
|
||||
/**
|
||||
* Will call [launchStateHandling] for state handling
|
||||
*/
|
||||
override suspend fun StatesMachine<in T>.handleState(state: T): T? = launchStateHandling(state, handlers)
|
||||
|
||||
/**
|
||||
* This
|
||||
*/
|
||||
protected val statesJobs = mutableMapOf<T, Job>()
|
||||
protected val statesJobsMutex = Mutex()
|
||||
|
||||
protected open suspend fun performUpdate(state: T) {
|
||||
val newState = launchStateHandling(state, handlers)
|
||||
if (newState != null) {
|
||||
statesManager.update(state, newState)
|
||||
} else {
|
||||
statesManager.endChain(state)
|
||||
}
|
||||
}
|
||||
|
||||
open suspend fun performStateUpdate(previousState: Optional<T>, actualState: T, scope: CoroutineScope) {
|
||||
statesJobsMutex.withLock {
|
||||
statesJobs[actualState] ?.cancel()
|
||||
statesJobs[actualState] = scope.launch {
|
||||
performUpdate(actualState)
|
||||
}.also { job ->
|
||||
job.invokeOnCompletion { _ ->
|
||||
scope.launch {
|
||||
statesJobsMutex.withLock {
|
||||
if (statesJobs[actualState] == job) {
|
||||
statesJobs.remove(actualState)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Launch handling of states. On [statesManager] [StatesManager.onStartChain],
|
||||
* [statesManager] [StatesManager.onChainStateUpdated] will be called lambda with performing of state. If
|
||||
@@ -60,23 +99,15 @@ class DefaultStatesMachine <T: State>(
|
||||
* [StatesManager.endChain].
|
||||
*/
|
||||
override fun start(scope: CoroutineScope): Job = scope.launchSafelyWithoutExceptions {
|
||||
val statePerformer: suspend (T) -> Unit = { state: T ->
|
||||
val newState = launchStateHandling(state, handlers)
|
||||
if (newState != null) {
|
||||
statesManager.update(state, newState)
|
||||
} else {
|
||||
statesManager.endChain(state)
|
||||
}
|
||||
}
|
||||
statesManager.onStartChain.subscribeSafelyWithoutExceptions(this) {
|
||||
launch { statePerformer(it) }
|
||||
launch { performStateUpdate(Optional.absent(), it, scope.LinkedSupervisorScope()) }
|
||||
}
|
||||
statesManager.onChainStateUpdated.subscribeSafelyWithoutExceptions(this) {
|
||||
launch { statePerformer(it.second) }
|
||||
launch { performStateUpdate(Optional.presented(it.first), it.second, scope.LinkedSupervisorScope()) }
|
||||
}
|
||||
|
||||
statesManager.getActiveStates().forEach {
|
||||
launch { statePerformer(it) }
|
||||
launch { performStateUpdate(Optional.absent(), it, scope.LinkedSupervisorScope()) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,57 @@
|
||||
package dev.inmo.micro_utils.fsm.common
|
||||
|
||||
import dev.inmo.micro_utils.common.*
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
|
||||
/**
|
||||
* This extender of [StatesMachine] interface declare one new function [updateChain]. Realizations of this interface
|
||||
* must be able to perform update of chain in internal [StatesManager]
|
||||
*/
|
||||
interface UpdatableStatesMachine<T : State> : StatesMachine<T> {
|
||||
/**
|
||||
* Update chain with current state equal to [currentState] with [newState]. Behaviour of this update preforming
|
||||
* in cases when [currentState] does not exist in [StatesManager] must be declared inside of realization of
|
||||
* [StatesManager.update] function
|
||||
*/
|
||||
suspend fun updateChain(currentState: T, newState: T)
|
||||
}
|
||||
|
||||
open class DefaultUpdatableStatesMachine<T : State>(
|
||||
statesManager: StatesManager<T>,
|
||||
handlers: List<CheckableHandlerHolder<in T, T>>,
|
||||
|
||||
) : DefaultStatesMachine<T>(
|
||||
statesManager,
|
||||
handlers
|
||||
), UpdatableStatesMachine<T> {
|
||||
protected val jobsStates = mutableMapOf<Job, T>()
|
||||
|
||||
override suspend fun performStateUpdate(previousState: Optional<T>, actualState: T, scope: CoroutineScope) {
|
||||
statesJobsMutex.withLock {
|
||||
statesJobs[actualState] ?.cancel()
|
||||
val job = previousState.mapOnPresented {
|
||||
statesJobs.remove(it)
|
||||
} ?: scope.launch {
|
||||
performUpdate(actualState)
|
||||
}.also { job ->
|
||||
job.invokeOnCompletion { _ ->
|
||||
scope.launch {
|
||||
statesJobsMutex.withLock {
|
||||
statesJobs.remove(
|
||||
jobsStates[job] ?: return@withLock
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
jobsStates.remove(job)
|
||||
statesJobs[actualState] = job
|
||||
jobsStates[job] = actualState
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun updateChain(currentState: T, newState: T) {
|
||||
statesManager.update(currentState, newState)
|
||||
}
|
||||
}
|
@@ -7,6 +7,14 @@ import kotlin.reflect.KClass
|
||||
|
||||
class FSMBuilder<T : State>(
|
||||
var statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
|
||||
val fsmBuilder: (states: List<CheckableHandlerHolder<T, T>>, defaultHandler: StatesHandler<T, T>?) -> StatesMachine<T> = { states, defaultHandler ->
|
||||
StatesMachine(
|
||||
statesManager,
|
||||
states.let { list ->
|
||||
defaultHandler ?.let { list + it.holder { true } } ?: list
|
||||
}
|
||||
)
|
||||
},
|
||||
var defaultStateHandler: StatesHandler<T, T>? = StatesHandler { null }
|
||||
) {
|
||||
private var states = mutableListOf<CheckableHandlerHolder<T, T>>()
|
||||
@@ -42,12 +50,7 @@ class FSMBuilder<T : State>(
|
||||
add(filter, handler)
|
||||
}
|
||||
|
||||
fun build() = StatesMachine(
|
||||
statesManager,
|
||||
states.toList().let { list ->
|
||||
defaultStateHandler ?.let { list + it.holder { true } } ?: list
|
||||
}
|
||||
)
|
||||
fun build() = fsmBuilder(states.toList(), defaultStateHandler)
|
||||
}
|
||||
|
||||
fun <T : State> buildFSM(
|
||||
|
Reference in New Issue
Block a user