Swiftgram/submodules/Postbox/Postbox/PeerChatInterfaceStateTable.swift
Peter 4459dc5b47 Add 'submodules/Postbox/' from commit '534443c710e63ff4ea595b5dc7be94550c467734'
git-subtree-dir: submodules/Postbox
git-subtree-mainline: 373769682ef152a8d5ef41ccb064a8387b2ca6f0
git-subtree-split: 534443c710e63ff4ea595b5dc7be94550c467734
2019-06-11 18:56:39 +01:00

79 lines
3.1 KiB
Swift

import Foundation
final class PeerChatInterfaceStateTable: Table {
static func tableSpec(_ id: Int32) -> ValueBoxTable {
return ValueBoxTable(id: id, keyType: .int64, compactValuesOnCreation: false)
}
private var states: [PeerId: PeerChatInterfaceState?] = [:]
private var peerIdsWithUpdatedStates = Set<PeerId>()
private let sharedKey = ValueBoxKey(length: 8)
private func key(_ peerId: PeerId, sharedKey: ValueBoxKey = ValueBoxKey(length: 8)) -> ValueBoxKey {
sharedKey.setInt64(0, value: peerId.toInt64())
return sharedKey
}
func get(_ peerId: PeerId) -> PeerChatInterfaceState? {
if let cachedValue = self.states[peerId] {
return cachedValue
} else if let value = self.valueBox.get(self.table, key: self.key(peerId, sharedKey: self.sharedKey)), let state = PostboxDecoder(buffer: value).decodeRootObject() as? PeerChatInterfaceState {
self.states[peerId] = state
return state
} else {
self.states[peerId] = nil
return nil
}
}
func set(_ peerId: PeerId, state: PeerChatInterfaceState?) -> (updated: Bool, updatedEmbeddedState: Bool) {
let currentState = self.get(peerId)
var updated = false
var updatedEmbeddedState = false
if let currentState = currentState, let state = state {
if !currentState.isEqual(to: state) {
updated = true
if let currentEmbeddedState = currentState.chatListEmbeddedState, let embeddedState = state.chatListEmbeddedState {
if !currentEmbeddedState.isEqual(to: embeddedState) {
updatedEmbeddedState = true
}
} else if (currentState.chatListEmbeddedState != nil) != (state.chatListEmbeddedState != nil) {
updatedEmbeddedState = true
}
}
} else if (currentState != nil) != (state != nil) {
updated = true
updatedEmbeddedState = true
}
if updated {
self.states[peerId] = state
self.peerIdsWithUpdatedStates.insert(peerId)
}
return (updated, updatedEmbeddedState)
}
override func clearMemoryCache() {
self.states.removeAll()
self.peerIdsWithUpdatedStates.removeAll()
}
override func beforeCommit() {
if !self.peerIdsWithUpdatedStates.isEmpty {
let sharedEncoder = PostboxEncoder()
for peerId in self.peerIdsWithUpdatedStates {
if let state = self.states[peerId] {
if let state = state {
sharedEncoder.reset()
sharedEncoder.encodeRootObject(state)
self.valueBox.set(self.table, key: self.key(peerId, sharedKey: self.sharedKey), value: sharedEncoder.readBufferNoCopy())
} else {
self.valueBox.remove(self.table, key: self.key(peerId, sharedKey: self.sharedKey), secure: false)
}
}
}
self.peerIdsWithUpdatedStates.removeAll()
}
}
}