Merge commit 'fe9a32d1b99253de2e4b0b38653333317aa7e35a' into p2p

pull/20/head
Mikunj 6 years ago
commit ed85a0da3c

@ -1207,8 +1207,6 @@ static NSTimeInterval launchStartedAt;
OWSAssertIsOnMainThread(); OWSAssertIsOnMainThread();
OWSLogInfo(@"storageIsReady"); OWSLogInfo(@"storageIsReady");
[LokiAPI loadSwarmCache];
[self checkIfAppIsReady]; [self checkIfAppIsReady];
} }

@ -11,23 +11,22 @@ public extension LokiAPI {
private static let swarmCacheKey = "swarmCacheKey" private static let swarmCacheKey = "swarmCacheKey"
private static let swarmCacheCollection = "swarmCacheCollection" private static let swarmCacheCollection = "swarmCacheCollection"
fileprivate static var swarmCache: [String:[Target]] = [:] fileprivate static var swarmCache: [String:[Target]] {
get {
@objc public static func loadSwarmCache() {
var result: [String:[Target]]? = nil var result: [String:[Target]]? = nil
storage.dbReadConnection.read { transaction in storage.dbReadConnection.read { transaction in
let intermediate = transaction.object(forKey: swarmCacheKey, inCollection: swarmCacheCollection) as! [String:[TargetWrapper]]? let intermediate = transaction.object(forKey: swarmCacheKey, inCollection: swarmCacheCollection) as! [String:[TargetWrapper]]?
result = intermediate?.mapValues { $0.map { Target(from: $0) } } result = intermediate?.mapValues { $0.map { Target(from: $0) } }
} }
swarmCache = result ?? [:] return result ?? [:]
} }
set {
private static func saveSwarmCache() { let intermediate = newValue.mapValues { $0.map { TargetWrapper(from: $0) } }
let intermediate = swarmCache.mapValues { $0.map { TargetWrapper(from: $0) } }
storage.dbReadWriteConnection.readWrite { transaction in storage.dbReadWriteConnection.readWrite { transaction in
transaction.setObject(intermediate, forKey: swarmCacheKey, inCollection: swarmCacheCollection) transaction.setObject(intermediate, forKey: swarmCacheKey, inCollection: swarmCacheCollection)
} }
} }
}
// MARK: Internal API // MARK: Internal API
private static func getRandomSnode() -> Promise<Target> { private static func getRandomSnode() -> Promise<Target> {
@ -41,10 +40,7 @@ public extension LokiAPI {
return Promise<[Target]> { $0.fulfill(cachedSwarm) } return Promise<[Target]> { $0.fulfill(cachedSwarm) }
} else { } else {
let parameters: [String:Any] = [ "pubKey" : hexEncodedPublicKey ] let parameters: [String:Any] = [ "pubKey" : hexEncodedPublicKey ]
return getRandomSnode().then { invoke(.getSwarm, on: $0, associatedWith: hexEncodedPublicKey, parameters: parameters) }.map { parseTargets(from: $0) }.get { swarm in return getRandomSnode().then { invoke(.getSwarm, on: $0, associatedWith: hexEncodedPublicKey, parameters: parameters) }.map { parseTargets(from: $0) }.get { swarmCache[hexEncodedPublicKey] = $0 }
swarmCache[hexEncodedPublicKey] = swarm
saveSwarmCache()
}
} }
} }

@ -3,8 +3,8 @@ import PromiseKit
extension Promise : Hashable { extension Promise : Hashable {
public func hash(into hasher: inout Hasher) { public func hash(into hasher: inout Hasher) {
let reference = ObjectIdentifier(self).hashValue let reference = ObjectIdentifier(self)
hasher.combine(reference) hasher.combine(reference.hashValue)
} }
public static func == (lhs: Promise, rhs: Promise) -> Bool { public static func == (lhs: Promise, rhs: Promise) -> Bool {

Loading…
Cancel
Save