fsms fixes

This commit is contained in:
InsanusMokrassar 2022-06-23 18:47:59 +06:00
parent c8759843f7
commit cd22d76fa7
3 changed files with 10 additions and 8 deletions

View File

@ -2,6 +2,10 @@
## 0.11.6
* `FSM`:
* `Common`
* Several fixes related to the jobs handling
## 0.11.5
* `Coroutines`:

View File

@ -6,6 +6,7 @@ import dev.inmo.micro_utils.coroutines.*
import dev.inmo.micro_utils.fsm.common.utils.StateHandlingErrorHandler
import dev.inmo.micro_utils.fsm.common.utils.defaultStateHandlingErrorHandler
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
@ -85,10 +86,10 @@ open class DefaultStatesMachine <T: State>(
protected open suspend fun performUpdate(state: T) {
val newState = launchStateHandling(state, handlers)
if (newState != null) {
statesManager.update(state, newState)
} else {
if (newState == null) {
statesManager.endChain(state)
} else {
statesManager.update(state, newState)
}
}
@ -118,7 +119,7 @@ open class DefaultStatesMachine <T: State>(
* [StatesManager.endChain].
*/
override fun start(scope: CoroutineScope): Job = scope.launchSafelyWithoutExceptions {
statesManager.onStartChain.subscribeSafelyWithoutExceptions(this) {
(statesManager.getActiveStates().asFlow() + statesManager.onStartChain).subscribeSafelyWithoutExceptions(this) {
launch { performStateUpdate(Optional.absent(), it, scope.LinkedSupervisorScope()) }
}
statesManager.onChainStateUpdated.subscribeSafelyWithoutExceptions(this) {
@ -134,10 +135,6 @@ open class DefaultStatesMachine <T: State>(
}
}
}
statesManager.getActiveStates().forEach {
launch { performStateUpdate(Optional.absent(), it, scope.LinkedSupervisorScope()) }
}
}
/**

View File

@ -52,6 +52,7 @@ open class DefaultUpdatableStatesMachine<T : State>(
statesJobs.remove(
jobsStates[job] ?: return@withLock
)
jobsStates.remove(job)
}
}
}