Skip to content

Commit 312737e

Browse files
committed
Update parameter name
1 parent a5a8774 commit 312737e

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

Sources/Quilt/OpID.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ public struct OpID: Comparable, Hashable, Codable, Equatable, CustomStringConver
77
/// - Parameters:
88
/// - counter: The sequence number of this operation
99
/// - id: The UUID of the user who created this operation
10-
public init(counter: Int, id: UUID) {
10+
public init(counter: Int, userID: UUID) {
1111
self.counter = counter
12-
self.id = id
12+
self.userID = userID
1313
}
1414

1515
public static func < (lhs: OpID, rhs: OpID) -> Bool {
1616
if lhs.counter == rhs.counter {
17-
return lhs.id.uuidString < rhs.id.uuidString
17+
return lhs.userID.uuidString < rhs.userID.uuidString
1818
} else {
1919
return lhs.counter < rhs.counter
2020
}
2121
}
2222

2323
public let counter: Int
24-
public let id: UUID
24+
public let userID: UUID
2525

2626
public var description: String {
27-
"\(counter)@\(id)"
27+
"\(counter)@\(userID)"
2828
}
2929
}

Sources/Quilt/Quilt.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public struct Quilt: Sendable {
5252
public mutating func insert(character: Character, atIndex: Int) {
5353
// Use currentContent since we're interested in the index of a character not action
5454
let operation = Operation(
55-
opId: .init(counter: counter, id: user),
55+
opId: .init(counter: counter, userID: user),
5656
type: .insert(character),
5757
afterId: currentContent[safeIndex: atIndex - 1]?.opId
5858
)
@@ -68,7 +68,7 @@ public struct Quilt: Sendable {
6868
guard let opID = currentContent[safeIndex: atIndex]?.opId
6969
?? currentContent.first?.opId else { return }
7070
let operation = Operation(
71-
opId: .init(counter: counter, id: user),
71+
opId: .init(counter: counter, userID: user),
7272
type: .remove(opID)
7373
)
7474
operationLog.append(operation)
@@ -90,7 +90,7 @@ public struct Quilt: Sendable {
9090
let start: SpanMarker = .before(currentContent[fromIndex].opId)
9191
let end: SpanMarker = .before(currentContent[toIndex].opId)
9292
let operation = Operation(
93-
opId: .init(counter: counter, id: user),
93+
opId: .init(counter: counter, userID: user),
9494
type: .addMark(type: mark, start: start, end: end)
9595
)
9696
operationLog.append(operation)
@@ -111,7 +111,7 @@ public struct Quilt: Sendable {
111111
let start: SpanMarker = .before(currentContent[fromIndex].opId)
112112
let end: SpanMarker = .before(currentContent[toIndex].opId)
113113
let operation = Operation(
114-
opId: .init(counter: counter, id: user),
114+
opId: .init(counter: counter, userID: user),
115115
type: .removeMark(
116116
type: mark,
117117
start: start,

Tests/QuiltTests/QuiltTests.swift

+49-17
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func getString(_ doc: Quilt) -> String {
2727

2828
#expect(
2929
quilt.operationLog[0] == Operation(
30-
opId: OpID(counter: 0, id: user),
30+
opId: OpID(counter: 0, userID: user),
3131
type: .insert("T"),
3232
afterId: nil
3333
)
@@ -37,7 +37,7 @@ func getString(_ doc: Quilt) -> String {
3737

3838
#expect(
3939
quilt.operationLog[1] == Operation(
40-
opId: OpID(counter: 1, id: user),
40+
opId: OpID(counter: 1, userID: user),
4141
type: .insert("H"),
4242
afterId: quilt.operationLog[0].id
4343
)
@@ -47,7 +47,7 @@ func getString(_ doc: Quilt) -> String {
4747

4848
#expect(
4949
quilt.operationLog[2] == Operation(
50-
opId: OpID(counter: 2, id: user),
50+
opId: OpID(counter: 2, userID: user),
5151
type: .remove(quilt.operationLog[1].id)
5252
)
5353
)
@@ -64,11 +64,11 @@ func getString(_ doc: Quilt) -> String {
6464
quilt.addMark(mark: .bold, fromIndex: 0, toIndex: 4)
6565

6666
#expect(quilt.operationLog[5] == Operation(
67-
opId: OpID(counter: 5, id: user),
67+
opId: OpID(counter: 5, userID: user),
6868
type: .addMark(
6969
type: .bold,
70-
start: .before(OpID(counter: 0, id: user)),
71-
end: .before(OpID(counter: 4, id: user))
70+
start: .before(OpID(counter: 0, userID: user)),
71+
end: .before(OpID(counter: 4, userID: user))
7272
)
7373
))
7474

@@ -79,11 +79,11 @@ func getString(_ doc: Quilt) -> String {
7979
)
8080

8181
#expect(quilt.operationLog[6] == Operation(
82-
opId: OpID(counter: 6, id: user),
82+
opId: OpID(counter: 6, userID: user),
8383
type: .removeMark(
8484
type: .bold,
85-
start: .before(OpID(counter: 0, id: user)),
86-
end: .before(OpID(counter: 4, id: user))
85+
start: .before(OpID(counter: 0, userID: user)),
86+
end: .before(OpID(counter: 4, userID: user))
8787
)
8888
))
8989
}
@@ -100,9 +100,9 @@ func getString(_ doc: Quilt) -> String {
100100
// MARK: - OpID Tests
101101

102102
@Test func testOpIDComparison() {
103-
let id1 = OpID(counter: 1, id: user)
104-
let id2 = OpID(counter: 2, id: user)
105-
let id3 = OpID(counter: 2, id: otherUser)
103+
let id1 = OpID(counter: 1, userID: user)
104+
let id2 = OpID(counter: 2, userID: user)
105+
let id3 = OpID(counter: 2, userID: otherUser)
106106

107107
#expect(id1 < id2)
108108
#expect(id2 > id1)
@@ -113,11 +113,6 @@ func getString(_ doc: Quilt) -> String {
113113
#expect(id2 < id3)
114114
}
115115

116-
@Test func testOpIDDescription() {
117-
let id = OpID(counter: 42, id: user)
118-
#expect(id.description == "42@\(user)")
119-
}
120-
121116
// MARK: - Quilt Merge Tests
122117

123118
@Test func testMergeQuilt() {
@@ -170,6 +165,43 @@ func getString(_ doc: Quilt) -> String {
170165
#expect(getString(doc2) == "The quick brown catdog")
171166
}
172167

168+
@Test func testConcurrentEditsOfSameWord() {
169+
var doc1 = Quilt(user: user)
170+
var doc2 = Quilt(user: otherUser)
171+
172+
let str = "The quick brown fox"
173+
str.enumerated().forEach { (idx, char) in
174+
doc1.insert(character: char, atIndex: idx)
175+
}
176+
177+
doc2.merge(doc1)
178+
179+
doc2.remove(atIndex: 18)
180+
doc2.remove(atIndex: 17)
181+
doc2.remove(atIndex: 16)
182+
183+
doc2.insert(character: "c", atIndex: 16)
184+
doc2.insert(character: "a", atIndex: 17)
185+
doc2.insert(character: "t", atIndex: 18)
186+
187+
doc1.remove(atIndex: 18)
188+
doc1.remove(atIndex: 17)
189+
doc1.remove(atIndex: 16)
190+
191+
doc1.insert(character: "c", atIndex: 16)
192+
doc1.insert(character: "a", atIndex: 17)
193+
doc1.insert(character: "t", atIndex: 18)
194+
195+
#expect(getString(doc1) == "The quick brown cat")
196+
#expect(getString(doc2) == "The quick brown cat")
197+
198+
doc1.merge(doc2)
199+
doc2.merge(doc1)
200+
201+
#expect(getString(doc1) == "The quick brown cat")
202+
#expect(getString(doc2) == "The quick brown cat")
203+
}
204+
173205
@Test func testMergeDuplicateOperations() {
174206
var quilt1 = Quilt(user: user)
175207
var quilt2 = Quilt(user: otherUser) // Changed to different user

0 commit comments

Comments
 (0)