From 8b39882e834aba1692675a70baa427691bdda346 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 27 Dec 2021 15:57:55 +0600 Subject: [PATCH] fixes in DefaultUpdatableStatesMachine --- CHANGELOG.md | 3 +++ .../inmo/micro_utils/fsm/common/UpdatableStatesMachine.kt | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 007b5c2d1a2..f538f5039bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ * `Server`: * Fixes in `uniloadMultipart` +* `FSM`: + * Fixes in `DefaultUpdatableStatesMachine` + ## 0.8.8 * `Versions`: diff --git a/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/UpdatableStatesMachine.kt b/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/UpdatableStatesMachine.kt index deadfb62324..76fccd49c88 100644 --- a/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/UpdatableStatesMachine.kt +++ b/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/UpdatableStatesMachine.kt @@ -28,12 +28,12 @@ open class DefaultUpdatableStatesMachine( override suspend fun performStateUpdate(previousState: Optional, actualState: T, scope: CoroutineScope) { statesJobsMutex.withLock { - if (previousState.dataOrNull() != actualState) { + if (compare(previousState, actualState)) { statesJobs[actualState] ?.cancel() } val job = previousState.mapOnPresented { statesJobs.remove(it) - } ?: scope.launch { + } ?.takeIf { it.isActive } ?: scope.launch { performUpdate(actualState) }.also { job -> job.invokeOnCompletion { _ -> @@ -52,6 +52,8 @@ open class DefaultUpdatableStatesMachine( } } + protected suspend fun compare(previous: Optional, new: T): Boolean = previous.dataOrNull() != new + override suspend fun updateChain(currentState: T, newState: T) { statesManager.update(currentState, newState) }