Swiftgram/submodules/Postbox/Sources/TimestampBasedMessageAttributesView.swift
2021-02-09 22:55:41 +04:00

53 lines
1.7 KiB
Swift

import Foundation
final class MutableTimestampBasedMessageAttributesView {
let tag: UInt16
var head: TimestampBasedMessageAttributesEntry?
init(postbox: Postbox, tag: UInt16) {
self.tag = tag
self.head = postbox.timestampBasedMessageAttributesTable.head(tag: tag)
}
func replay(postbox: Postbox, operations: [TimestampBasedMessageAttributesOperation]) -> Bool {
var updated = false
var invalidatedHead = false
for operation in operations {
switch operation {
case let .add(entry):
if entry.tag == self.tag {
if let head = self.head {
if entry.index < head.index {
self.head = entry
updated = true
}
} else {
self.head = entry
updated = true
}
}
case let .remove(entry):
if entry.tag == self.tag {
if let head = self.head, head.index == entry.index {
self.head = nil
updated = true
invalidatedHead = true
}
}
}
}
if invalidatedHead {
self.head = postbox.timestampBasedMessageAttributesTable.head(tag: self.tag)
}
return updated
}
}
public final class TimestampBasedMessageAttributesView {
public let head: TimestampBasedMessageAttributesEntry?
init(_ view: MutableTimestampBasedMessageAttributesView) {
self.head = view.head
}
}