diff --git a/plugin.xml b/plugin.xml index 9f78762..fc3d83e 100644 --- a/plugin.xml +++ b/plugin.xml @@ -28,6 +28,7 @@ + diff --git a/src/ios/SynchronizedDictionary.swift b/src/ios/SynchronizedDictionary.swift new file mode 100644 index 0000000..6bfdbf2 --- /dev/null +++ b/src/ios/SynchronizedDictionary.swift @@ -0,0 +1,25 @@ +public class SynchronizedDictionary { + 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 + } + } +} diff --git a/src/ios/Webserver.swift b/src/ios/Webserver.swift index 88e7c77..be25517 100644 --- a/src/ios/Webserver.swift +++ b/src/ios/Webserver.swift @@ -3,13 +3,13 @@ let TIMEOUT: Int = 60 * 3 * 1000000 var webServer: GCDWebServer = GCDWebServer() - var responses: Dictionary = [:] + var responses = SynchronizedDictionary() var onRequestCommand: CDVInvokedUrlCommand? = nil override func pluginInitialize() { self.webServer = GCDWebServer() self.onRequestCommand = nil - self.responses = [:] + self.responses = SynchronizedDictionary() self.initHTTPRequestHandlers() } @@ -63,6 +63,9 @@ response?.setValue(value, forAdditionalHeader: key) } + // Remove the handled response + self.responses.removeValue(forKey: requestUUID) + // Complete the async response completionBlock(response!) }