Skip to content

Commit

Permalink
Fix a syncing bug that could remove the wrong row from the row order …
Browse files Browse the repository at this point in the history
…table and corrupt an outline
  • Loading branch information
vincode-io committed Jan 18, 2024
1 parent 75c9570 commit 5b3c726
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions VinCloudKit/Sources/VinCloudKit/VCKModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,35 @@ public extension VCKModel {
return OrderedSet(merged)
}

func merge<T>(client: [T]?, ancestor: [T]?, server: [T]?) -> [T]? where T:Equatable {
func merge<T>(client: [T]?, ancestor: [T]?, server: [T]?) -> [T]? where T:Equatable, T:Hashable {
switch VCKMergeScenario.evaluate(client: client, ancestor: ancestor, server: server) {
case .clientWins:
return client
case .serverWins:
return server
case .threeWayMerge:
guard let client, let server, let ancestor else { fatalError("We should always have all 3 values for a 3 way merge.") }
let diff = server.difference(from: ancestor)
guard let merged = client.applying(diff) else {
return client
guard let client, let ancestor, let server else { fatalError("We should always have all 3 values for a 3 way merge.") }

var merged = client
let diffs = server.difference(from: ancestor).inferringMoves()

for diff in diffs {
switch diff {
case .insert(let offset, let value, _):
if offset < merged.count {
merged.insert(value, at: offset)
} else {
merged.insert(value, at: merged.count)
}
case .remove(_, let value, _):
merged.removeFirst(object: value)
}
}

return merged
}
}

}

private extension VCKModel {
Expand Down

0 comments on commit 5b3c726

Please sign in to comment.