@@ -56,11 +56,15 @@ internal class RealMutableStore<Key : Any, Network : Any, Output : Any, Local :
5656 }
5757
5858 is EagerConflictResolutionResult .Success .ConflictsResolved -> {
59- logger.debug(eagerConflictResolutionResult.value.toString())
59+ val message = when (val result = eagerConflictResolutionResult.value) {
60+ is UpdaterResult .Success .Typed <* > -> result.value.toString()
61+ is UpdaterResult .Success .Untyped -> result.value.toString()
62+ }
63+ logger.debug(message)
6064 }
6165
6266 EagerConflictResolutionResult .Success .NoConflicts -> {
63- logger.debug(eagerConflictResolutionResult.toString() )
67+ logger.debug(" No conflicts. " )
6468 }
6569 }
6670
@@ -225,48 +229,46 @@ internal class RealMutableStore<Key : Any, Network : Any, Output : Any, Local :
225229 }
226230
227231 @AnyThread
228- private suspend fun <Response : Any > tryEagerlyResolveConflicts (key : Key ): EagerConflictResolutionResult <Response > =
229- withThreadSafety(key) {
232+ private suspend fun <Response : Any > tryEagerlyResolveConflicts (key : Key ): EagerConflictResolutionResult <Response > {
233+
234+ val (latest, conflictsExist) = withThreadSafety(key) {
230235 val latest = delegate.latestOrNull(key)
231- when {
232- latest == null || bookkeeper == null || conflictsMightExist(key).not () -> EagerConflictResolutionResult .Success .NoConflicts
233- else -> {
234- try {
235- val updaterResult =
236- updater.post(key, latest).also { updaterResult ->
237- if (updaterResult is UpdaterResult .Success ) {
238- updateWriteRequestQueue<Response >(key = key, created = now(), updaterResult = updaterResult)
239- }
240- }
236+ val conflictsExist = latest != null && bookkeeper != null && conflictsMightExist(key)
237+ latest to conflictsExist
238+ }
241239
242- when (updaterResult) {
243- is UpdaterResult .Error .Exception -> EagerConflictResolutionResult .Error .Exception (updaterResult.error)
244- is UpdaterResult .Error .Message -> EagerConflictResolutionResult .Error .Message (updaterResult.message)
245- is UpdaterResult .Success -> EagerConflictResolutionResult .Success .ConflictsResolved (updaterResult)
246- }
247- } catch (throwable: Throwable ) {
248- EagerConflictResolutionResult .Error .Exception (throwable)
240+ if (! conflictsExist || latest == null ) {
241+ return EagerConflictResolutionResult .Success .NoConflicts
242+ }
243+
244+
245+ return try {
246+ val updaterResult =
247+ updater.post(key, latest).also { updaterResult ->
248+ if (updaterResult is UpdaterResult .Success ) {
249+ updateWriteRequestQueue<Response >(key = key, created = now(), updaterResult = updaterResult)
250+ bookkeeper?.clear(key)
249251 }
250252 }
251- }
252- }
253253
254- private suspend fun safeInitWriteRequestQueue ( key : Key ) =
255- withThreadSafety(key) {
256- if (keyToWriteRequestQueue[key] == null ) {
257- keyToWriteRequestQueue[key] = ArrayDeque ( )
254+ when (updaterResult) {
255+ is UpdaterResult . Error . Exception -> EagerConflictResolutionResult . Error . Exception (updaterResult.error)
256+ is UpdaterResult . Error . Message -> EagerConflictResolutionResult . Error . Message (updaterResult.message)
257+ is UpdaterResult . Success -> EagerConflictResolutionResult . Success . ConflictsResolved (updaterResult )
258258 }
259+ } catch (throwable: Throwable ) {
260+ EagerConflictResolutionResult .Error .Exception (throwable)
259261 }
262+ }
260263
261- private suspend fun safeInitThreadSafety (key : Key ) =
264+ private suspend fun safeInitStore (key : Key ) {
262265 storeLock.withLock {
263266 if (keyToThreadSafety[key] == null ) {
264267 keyToThreadSafety[key] = ThreadSafety ()
265268 }
269+ if (keyToWriteRequestQueue[key] == null ) {
270+ keyToWriteRequestQueue[key] = ArrayDeque ()
271+ }
266272 }
267-
268- private suspend fun safeInitStore (key : Key ) {
269- safeInitThreadSafety(key)
270- safeInitWriteRequestQueue(key)
271273 }
272274}
0 commit comments