diff --git a/Postbox/Coding.swift b/Postbox/Coding.swift index a2a4f9779a..611f6ed211 100644 --- a/Postbox/Coding.swift +++ b/Postbox/Coding.swift @@ -708,17 +708,7 @@ public final class Decoder { return 0 } } - - public func decodeInt32ForKey(_ key: String) -> Int32 { - if Decoder.positionOnStringKey(self.buffer.memory, offset: &self.offset, maxOffset: self.buffer.length, length: self.buffer.length, key: key, valueType: .Int32) { - var value: Int32 = 0 - memcpy(&value, self.buffer.memory + self.offset, 4) - self.offset += 4 - return value - } else { - return 0 - } - } + public func decodeInt32ForKey(_ key: StaticString) -> Int32? { if Decoder.positionOnKey(self.buffer.memory, offset: &self.offset, maxOffset: self.buffer.length, length: self.buffer.length, key: key, valueType: .Int32) { diff --git a/Postbox/PeerMergedOperationLogView.swift b/Postbox/PeerMergedOperationLogView.swift index c403ced292..7993c10b8d 100644 --- a/Postbox/PeerMergedOperationLogView.swift +++ b/Postbox/PeerMergedOperationLogView.swift @@ -37,6 +37,16 @@ final class MutablePeerMergedOperationLogView { self.tailIndex = entry.mergedIndex } } + case let .updateContents(entry): + if entry.tag == self.tag { + loop: for i in 0 ..< self.entries.count { + if self.entries[i].tagLocalIndex == entry.tagLocalIndex { + self.entries[i] = entry + updated = true + break loop + } + } + } case let .remove(tag, mergedIndices): if tag == self.tag { updated = true diff --git a/Postbox/PeerOperationLogTable.swift b/Postbox/PeerOperationLogTable.swift index 71f8413f7a..e88a27dedd 100644 --- a/Postbox/PeerOperationLogTable.swift +++ b/Postbox/PeerOperationLogTable.swift @@ -3,6 +3,7 @@ import Foundation enum PeerMergedOperationLogOperation { case append(PeerMergedOperationLogEntry) case remove(tag: PeerOperationLogTag, mergedIndices: Set) + case updateContents(PeerMergedOperationLogEntry) } public struct PeerMergedOperationLogEntry { @@ -296,9 +297,18 @@ final class PeerOperationLogTable: Table { assert(value.length - value.offset == Int(contentLength)) if let contents = Decoder(buffer: MemoryBuffer(memory: value.memory.advanced(by: value.offset), capacity: Int(contentLength), length: Int(contentLength), freeWhenDone: false)).decodeRootObject() { let entryUpdate = f(PeerOperationLogEntry(peerId: peerId, tag: tag, tagLocalIndex: tagLocalIndex, mergedIndex: mergedIndex, contents: contents)) - switch entryUpdate.mergedIndex { + var updatedContents: Coding? + switch entryUpdate.contents { case .none: break + case let .update(contents): + updatedContents = contents + } + switch entryUpdate.mergedIndex { + case .none: + if let previousMergedIndex = previousMergedIndex, let updatedContents = updatedContents { + operations.append(.updateContents(PeerMergedOperationLogEntry(peerId: peerId, tag: tag, tagLocalIndex: tagLocalIndex, mergedIndex: previousMergedIndex, contents: updatedContents))) + } case .remove: if let mergedIndexValue = mergedIndex { mergedIndex = nil @@ -312,14 +322,7 @@ final class PeerOperationLogTable: Table { } let updatedMergedIndexValue = self.mergedIndexTable.add(peerId: peerId, tag: tag, tagLocalIndex: tagLocalIndex) mergedIndex = updatedMergedIndexValue - operations.append(.append(PeerMergedOperationLogEntry(peerId: peerId, tag: tag, tagLocalIndex: tagLocalIndex, mergedIndex: updatedMergedIndexValue, contents: contents))) - } - var updatedContents: Coding? - switch entryUpdate.contents { - case .none: - break - case let .update(contents): - updatedContents = contents + operations.append(.append(PeerMergedOperationLogEntry(peerId: peerId, tag: tag, tagLocalIndex: tagLocalIndex, mergedIndex: updatedMergedIndexValue, contents: updatedContents ?? contents))) } if previousMergedIndex != mergedIndex || updatedContents != nil { let buffer = WriteBuffer()