fixes in DefaultUpdatableStatesMachine

This commit is contained in:
InsanusMokrassar 2021-12-27 15:57:55 +06:00
parent e639ae172b
commit 8b39882e83
2 changed files with 7 additions and 2 deletions

View File

@ -6,6 +6,9 @@
* `Server`: * `Server`:
* Fixes in `uniloadMultipart` * Fixes in `uniloadMultipart`
* `FSM`:
* Fixes in `DefaultUpdatableStatesMachine`
## 0.8.8 ## 0.8.8
* `Versions`: * `Versions`:

View File

@ -28,12 +28,12 @@ open class DefaultUpdatableStatesMachine<T : State>(
override suspend fun performStateUpdate(previousState: Optional<T>, actualState: T, scope: CoroutineScope) { override suspend fun performStateUpdate(previousState: Optional<T>, actualState: T, scope: CoroutineScope) {
statesJobsMutex.withLock { statesJobsMutex.withLock {
if (previousState.dataOrNull() != actualState) { if (compare(previousState, actualState)) {
statesJobs[actualState] ?.cancel() statesJobs[actualState] ?.cancel()
} }
val job = previousState.mapOnPresented { val job = previousState.mapOnPresented {
statesJobs.remove(it) statesJobs.remove(it)
} ?: scope.launch { } ?.takeIf { it.isActive } ?: scope.launch {
performUpdate(actualState) performUpdate(actualState)
}.also { job -> }.also { job ->
job.invokeOnCompletion { _ -> job.invokeOnCompletion { _ ->
@ -52,6 +52,8 @@ open class DefaultUpdatableStatesMachine<T : State>(
} }
} }
protected suspend fun compare(previous: Optional<T>, new: T): Boolean = previous.dataOrNull() != new
override suspend fun updateChain(currentState: T, newState: T) { override suspend fun updateChain(currentState: T, newState: T) {
statesManager.update(currentState, newState) statesManager.update(currentState, newState)
} }