add option for handle queue

This commit is contained in:
Erik 2015-08-05 06:09:24 -04:00
parent e792006245
commit ffbe1e4ec4
3 changed files with 24 additions and 15 deletions

View File

@ -121,6 +121,7 @@ Options
- `sessionDelegate: NSURLSessionDelegate` Sets an NSURLSessionDelegate for the underlying engine. Useful if you need to handle self-signed certs. Default is nil.
- `path: String` - If the server uses a custom path. ex: `"/swift"`. Default is `""`
- `extraHeaders: [String: String]?` - Adds custom headers to the initial request. Default is nil.
- `handleQueue: dispatch_queue_t` - The dispatch queue that handlers are run on. Default is the main queue.
Methods
-------

View File

@ -40,8 +40,6 @@ final class SocketEventHandler {
func executeCallback(_ items:NSArray? = nil, withAck ack:Int? = nil, withAckType type:Int? = nil,
withSocket socket:SocketIOClient? = nil) {
dispatch_async(dispatch_get_main_queue()) {[weak self] in
self?.callback?(items, ack != nil ? emitAckCallback(socket!, ack!) : nil)
}
self.callback?(items, ack != nil ? emitAckCallback(socket!, ack!) : nil)
}
}

View File

@ -46,7 +46,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
public let socketURL:String
public let handleAckQueue = dispatch_queue_create("handleAckQueue", DISPATCH_QUEUE_SERIAL)
public let handleQueue = dispatch_queue_create("handleQueue", DISPATCH_QUEUE_SERIAL)
public let handleQueue: dispatch_queue_t!
public let emitQueue = dispatch_queue_create("emitQueue", DISPATCH_QUEUE_SERIAL)
public var closed:Bool {
return _closed
@ -121,6 +121,12 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
self.reconnectWait = abs(reconnectWait)
}
if let handleQueue = opts?["handleQueue"] as? dispatch_queue_t {
self.handleQueue = handleQueue
} else {
self.handleQueue = dispatch_get_main_queue()
}
super.init()
}
@ -393,7 +399,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
args: event, data ?? "")
if anyHandler != nil {
dispatch_async(dispatch_get_main_queue()) {[weak self] in
dispatch_async(handleQueue) {[weak self] in
self?.anyHandler?(SocketAnyEvent(event: event, items: data))
}
}
@ -401,9 +407,13 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
for handler in handlers {
if handler.event == event {
if ack != nil {
handler.executeCallback(data, withAck: ack!, withSocket: self)
dispatch_async(handleQueue) {[weak self] in
handler.executeCallback(data, withAck: ack!, withSocket: self)
}
} else {
handler.executeCallback(data)
dispatch_async(handleQueue) {[weak self] in
handler.executeCallback(data)
}
}
}
}
@ -448,14 +458,14 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
let handler = SocketEventHandler(event: event, callback: callback)
handlers.append(handler)
}
/**
Removes all handlers.
Can be used after disconnecting to break any potential remaining retain cycles.
*/
public func removeAllHandlers() {
handlers.removeAll(keepCapacity: false)
}
/**
Removes all handlers.
Can be used after disconnecting to break any potential remaining retain cycles.
*/
public func removeAllHandlers() {
handlers.removeAll(keepCapacity: false)
}
/**
Adds a handler that will be called on every event.