mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 22:25:57 +00:00
Refactor SyncCore
This commit is contained in:
42
submodules/SyncCore/Sources/CachedResolvedByNamePeer.swift
Normal file
42
submodules/SyncCore/Sources/CachedResolvedByNamePeer.swift
Normal file
@@ -0,0 +1,42 @@
|
||||
import Postbox
|
||||
|
||||
public final class CachedResolvedByNamePeer: PostboxCoding {
|
||||
public let peerId: PeerId?
|
||||
public let timestamp: Int32
|
||||
|
||||
public static func key(name: String) -> ValueBoxKey {
|
||||
let key: ValueBoxKey
|
||||
if let nameData = name.data(using: .utf8) {
|
||||
key = ValueBoxKey(length: nameData.count)
|
||||
nameData.withUnsafeBytes { (bytes: UnsafePointer<Int8>) -> Void in
|
||||
memcpy(key.memory, bytes, nameData.count)
|
||||
}
|
||||
} else {
|
||||
key = ValueBoxKey(length: 0)
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
public init(peerId: PeerId?, timestamp: Int32) {
|
||||
self.peerId = peerId
|
||||
self.timestamp = timestamp
|
||||
}
|
||||
|
||||
public init(decoder: PostboxDecoder) {
|
||||
if let peerId = decoder.decodeOptionalInt64ForKey("p") {
|
||||
self.peerId = PeerId(peerId)
|
||||
} else {
|
||||
self.peerId = nil
|
||||
}
|
||||
self.timestamp = decoder.decodeInt32ForKey("t", orElse: 0)
|
||||
}
|
||||
|
||||
public func encode(_ encoder: PostboxEncoder) {
|
||||
if let peerId = self.peerId {
|
||||
encoder.encodeInt64(peerId.toInt64(), forKey: "p")
|
||||
} else {
|
||||
encoder.encodeNil(forKey: "p")
|
||||
}
|
||||
encoder.encodeInt32(self.timestamp, forKey: "t")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user