From cffcc0832541e620fca5867d96ac233543cce03a Mon Sep 17 00:00:00 2001 From: matt-ramotar Date: Sun, 17 Nov 2024 20:04:30 -0500 Subject: [PATCH] Fix #667 Signed-off-by: matt-ramotar --- .../store/store5/impl/RealMutableStore.kt | 66 ++++++++++--------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RealMutableStore.kt b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RealMutableStore.kt index cd577f2c..914eada6 100644 --- a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RealMutableStore.kt +++ b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/RealMutableStore.kt @@ -56,11 +56,15 @@ internal class RealMutableStore { - logger.debug(eagerConflictResolutionResult.value.toString()) + val message = when (val result = eagerConflictResolutionResult.value) { + is UpdaterResult.Success.Typed<*> -> result.value.toString() + is UpdaterResult.Success.Untyped -> result.value.toString() + } + logger.debug(message) } EagerConflictResolutionResult.Success.NoConflicts -> { - logger.debug(eagerConflictResolutionResult.toString()) + logger.debug("No conflicts.") } } @@ -225,48 +229,46 @@ internal class RealMutableStore tryEagerlyResolveConflicts(key: Key): EagerConflictResolutionResult = - withThreadSafety(key) { + private suspend fun tryEagerlyResolveConflicts(key: Key): EagerConflictResolutionResult { + + val (latest, conflictsExist) = withThreadSafety(key) { val latest = delegate.latestOrNull(key) - when { - latest == null || bookkeeper == null || conflictsMightExist(key).not() -> EagerConflictResolutionResult.Success.NoConflicts - else -> { - try { - val updaterResult = - updater.post(key, latest).also { updaterResult -> - if (updaterResult is UpdaterResult.Success) { - updateWriteRequestQueue(key = key, created = now(), updaterResult = updaterResult) - } - } + val conflictsExist = latest != null && bookkeeper != null && conflictsMightExist(key) + latest to conflictsExist + } - when (updaterResult) { - is UpdaterResult.Error.Exception -> EagerConflictResolutionResult.Error.Exception(updaterResult.error) - is UpdaterResult.Error.Message -> EagerConflictResolutionResult.Error.Message(updaterResult.message) - is UpdaterResult.Success -> EagerConflictResolutionResult.Success.ConflictsResolved(updaterResult) - } - } catch (throwable: Throwable) { - EagerConflictResolutionResult.Error.Exception(throwable) + if (!conflictsExist || latest == null) { + return EagerConflictResolutionResult.Success.NoConflicts + } + + + return try { + val updaterResult = + updater.post(key, latest).also { updaterResult -> + if (updaterResult is UpdaterResult.Success) { + updateWriteRequestQueue(key = key, created = now(), updaterResult = updaterResult) + bookkeeper?.clear(key) } } - } - } - private suspend fun safeInitWriteRequestQueue(key: Key) = - withThreadSafety(key) { - if (keyToWriteRequestQueue[key] == null) { - keyToWriteRequestQueue[key] = ArrayDeque() + when (updaterResult) { + is UpdaterResult.Error.Exception -> EagerConflictResolutionResult.Error.Exception(updaterResult.error) + is UpdaterResult.Error.Message -> EagerConflictResolutionResult.Error.Message(updaterResult.message) + is UpdaterResult.Success -> EagerConflictResolutionResult.Success.ConflictsResolved(updaterResult) } + } catch (throwable: Throwable) { + EagerConflictResolutionResult.Error.Exception(throwable) } + } - private suspend fun safeInitThreadSafety(key: Key) = + private suspend fun safeInitStore(key: Key) { storeLock.withLock { if (keyToThreadSafety[key] == null) { keyToThreadSafety[key] = ThreadSafety() } + if(keyToWriteRequestQueue[key] == null) { + keyToWriteRequestQueue[key] = ArrayDeque() + } } - - private suspend fun safeInitStore(key: Key) { - safeInitThreadSafety(key) - safeInitWriteRequestQueue(key) } }