Skip to content

Commit

Permalink
properly handling a passed in transaction on YText
Browse files Browse the repository at this point in the history
  • Loading branch information
heckj committed Mar 4, 2024
1 parent 8ec2e28 commit a7f9834
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 20 deletions.
72 changes: 54 additions & 18 deletions Sources/YSwift/YText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ public final class YText: Transactable, YCollection {
/// - text: The string to append.
/// - transaction: An optional transaction to use when appending the string.
public func append(_ text: String, in transaction: YrsTransaction? = nil) {
withTransaction(transaction) { txn in
self._text.append(tx: txn, text: text)
if let transaction {
self._text.append(tx: transaction, text: text)
} else {
withTransaction(transaction) { txn in
self._text.append(tx: txn, text: text)
}
}
}

Expand All @@ -34,8 +38,12 @@ public final class YText: Transactable, YCollection {
at index: UInt32,
in transaction: YrsTransaction? = nil
) {
withTransaction(transaction) { txn in
self._text.insert(tx: txn, index: index, chunk: text)
if let transaction {
self._text.insert(tx: transaction, index: index, chunk: text)
} else {
withTransaction(transaction) { txn in
self._text.insert(tx: txn, index: index, chunk: text)
}
}
}

Expand All @@ -51,8 +59,12 @@ public final class YText: Transactable, YCollection {
at index: UInt32,
in transaction: YrsTransaction? = nil
) {
withTransaction(transaction) { txn in
self._text.insertWithAttributes(tx: txn, index: index, chunk: text, attrs: Coder.encoded(attributes))
if let transaction {
self._text.insertWithAttributes(tx: transaction, index: index, chunk: text, attrs: Coder.encoded(attributes))
} else {
withTransaction(transaction) { txn in
self._text.insertWithAttributes(tx: txn, index: index, chunk: text, attrs: Coder.encoded(attributes))
}
}
}

Expand All @@ -66,8 +78,12 @@ public final class YText: Transactable, YCollection {
at index: UInt32,
in transaction: YrsTransaction? = nil
) {
withTransaction(transaction) { txn in
self._text.insertEmbed(tx: txn, index: index, content: Coder.encoded(embed))
if let transaction {
self._text.insertEmbed(tx: transaction, index: index, content: Coder.encoded(embed))
} else {
withTransaction(transaction) { txn in
self._text.insertEmbed(tx: txn, index: index, content: Coder.encoded(embed))
}
}
}

Expand All @@ -83,8 +99,12 @@ public final class YText: Transactable, YCollection {
at index: UInt32,
in transaction: YrsTransaction? = nil
) {
withTransaction(transaction) { txn in
self._text.insertEmbedWithAttributes(tx: txn, index: index, content: Coder.encoded(embed), attrs: Coder.encoded(attributes))
if let transaction {
self._text.insertEmbedWithAttributes(tx: transaction, index: index, content: Coder.encoded(embed), attrs: Coder.encoded(attributes))
} else {
withTransaction(transaction) { txn in
self._text.insertEmbedWithAttributes(tx: txn, index: index, content: Coder.encoded(embed), attrs: Coder.encoded(attributes))
}
}
}

Expand All @@ -100,8 +120,12 @@ public final class YText: Transactable, YCollection {
attributes: [String: Any],
in transaction: YrsTransaction? = nil
) {
withTransaction(transaction) { txn in
self._text.format(tx: txn, index: index, length: length, attrs: Coder.encoded(attributes))
if let transaction {
self._text.format(tx: transaction, index: index, length: length, attrs: Coder.encoded(attributes))
} else {
withTransaction(transaction) { txn in
self._text.format(tx: txn, index: index, length: length, attrs: Coder.encoded(attributes))
}
}
}

Expand All @@ -115,24 +139,36 @@ public final class YText: Transactable, YCollection {
length: UInt32,
in transaction: YrsTransaction? = nil
) {
withTransaction(transaction) { txn in
self._text.removeRange(tx: txn, start: start, length: length)
if let transaction {
self._text.removeRange(tx: transaction, start: start, length: length)
} else {
withTransaction(transaction) { txn in
self._text.removeRange(tx: txn, start: start, length: length)
}
}
}

/// Returns the string within the text.
/// - Parameter transaction: An optional transaction to use when appending the string.
public func getString(in transaction: YrsTransaction? = nil) -> String {
withTransaction(transaction) { txn in
self._text.getString(tx: txn)
if let transaction {
self._text.getString(tx: transaction)
} else {
withTransaction(transaction) { txn in
self._text.getString(tx: txn)
}
}
}

/// Returns the length of the string.
/// - Parameter transaction: An optional transaction to use when appending the string.
public func length(in transaction: YrsTransaction? = nil) -> UInt32 {
withTransaction(transaction) { txn in
self._text.length(tx: txn)
if let transaction {
self._text.length(tx: transaction)
} else {
withTransaction(transaction) { txn in
self._text.length(tx: txn)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/YSwiftTests/YUndoManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ class YUndoManagerTests: XCTestCase {

// create some changes locally
document.transactSync(origin: localOrigin) { txn in
self.text.insert("hello", at: 0)
self.text.insert("hello", at: 0, in: txn)
self.manager.wrap() // add changes on a stack: they will be undone as one
}

exchangeUpdates(document, remoteDocument)

// concurrent change on the remote replica
document.transactSync(origin: localOrigin) { txn in
self.text.insert(" world", at: 5)
self.text.insert(" world", at: 5, in: txn)
}
remoteText.insert("<break>", at: 1)

Expand Down

0 comments on commit a7f9834

Please sign in to comment.