Merge pull request #25 from kukukk/master

iOS: clean up by removing the handled response
This commit is contained in:
Michael Bykovski 2018-09-11 09:28:27 +02:00 committed by GitHub
commit 9a4369ce57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 2 deletions

View File

@ -28,6 +28,7 @@
<platform name="ios">
<source-file src="src/ios/Webserver.swift" />
<source-file src="src/ios/SynchronizedDictionary.swift" />
<dependency id="cordova-plugin-add-swift-support" version="1.6.1"/>
<framework src="libz.tbd" />

View File

@ -0,0 +1,25 @@
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
}
}
}

View File

@ -3,13 +3,13 @@
let TIMEOUT: Int = 60 * 3 * 1000000
var webServer: GCDWebServer = GCDWebServer()
var responses: Dictionary<String, Any> = [:]
var responses = SynchronizedDictionary<AnyHashable,Any?>()
var onRequestCommand: CDVInvokedUrlCommand? = nil
override func pluginInitialize() {
self.webServer = GCDWebServer()
self.onRequestCommand = nil
self.responses = [:]
self.responses = SynchronizedDictionary<AnyHashable,Any?>()
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!)
}