Skip to content

Commit

Permalink
Add missing ordering during merge
Browse files Browse the repository at this point in the history
  • Loading branch information
theolampert committed Oct 28, 2024
1 parent f755859 commit a5a8774
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Sources/Quilt/Quilt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,21 @@ public struct Quilt: Sendable {
/// Merges another Quilt document into this one
/// - Parameter quilt: The Quilt document to merge
public mutating func merge(_ quilt: Quilt) {
// Add new operations that we don't already have
operationLog += quilt.operationLog.filter { operation in
!self.operationLog.contains(where: { operation.opId == $0.opId })
}

// Sort operations by OpID to ensure consistent ordering
operationLog.sort { $0.opId < $1.opId }

// Update counter to highest seen
if let max = operationLog.max(by: {
$0.opId.counter < $1.opId.counter
})?.opId.counter {
counter = max + 1
}

commit()
}
}

0 comments on commit a5a8774

Please sign in to comment.