cordova-plugin-webserver/src/ios/SynchronizedDictionary.swift
2018-08-21 11:57:01 +03:00

26 lines
781 B
Swift

public class SynchronizedDictionary<KeyType:Hashable, ValueType> {
private var dictionary: [KeyType:ValueType] = [:]
private let accessQueue = DispatchQueue(label: "SynchronizedDictionaryAccess", attributes: .concurrent)
public func removeValue(forKey: KeyType) {
self.accessQueue.async(flags:.barrier) {
self.dictionary.removeValue(forKey: forKey)
}
}
public subscript(key: KeyType) -> ValueType? {
set {
self.accessQueue.async(flags:.barrier) {
self.dictionary[key] = newValue
}
}
get {
var element: ValueType?
self.accessQueue.sync {
element = self.dictionary[key]
}
return element
}
}
}