Skip to content

Commit b58adbe

Browse files
committed
fix(model-client): drop local changes when the synchronization fails
When there is an exception during the merge of a version received from the server with the local version, just replace the local version with the remote version. Losing some recent changes is less critical that getting out of sync with the server.
1 parent 370c9ec commit b58adbe

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

model-client/src/commonMain/kotlin/org/modelix/model/client2/ReplicatedModel.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,13 @@ class ReplicatedModel(val client: IModelClientV2, val branchRef: BranchReference
9191
if (newRemoteVersion.getContentHash() != localVersion.getContentHash()) {
9292
otBranch.runWrite {
9393
applyPendingLocalChanges()
94-
localVersion = VersionMerger(newRemoteVersion.store, client.getIdGenerator())
95-
.mergeChange(localVersion, newRemoteVersion)
94+
try {
95+
localVersion = VersionMerger(newRemoteVersion.store, client.getIdGenerator())
96+
.mergeChange(localVersion, newRemoteVersion)
97+
} catch (ex: Exception) {
98+
LOG.warn(ex) { "Failed to merge remote version $newRemoteVersion into local version $localVersion. Resetting to remote version." }
99+
localVersion = newRemoteVersion
100+
}
96101
rawBranch.writeTransaction.tree = localVersion.tree
97102
}
98103
}
@@ -136,6 +141,10 @@ class ReplicatedModel(val client: IModelClientV2, val branchRef: BranchReference
136141
Started,
137142
Disposed
138143
}
144+
145+
companion object {
146+
private val LOG = mu.KotlinLogging.logger { }
147+
}
139148
}
140149

141150
fun IModelClientV2.getReplicatedModel(branchRef: BranchReference, query: ModelQuery? = null): ReplicatedModel {

0 commit comments

Comments
 (0)