Skip to content

Commit ffbe1e4

Browse files
committed
add option for handle queue
1 parent e792006 commit ffbe1e4

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Options
121121
- `sessionDelegate: NSURLSessionDelegate` Sets an NSURLSessionDelegate for the underlying engine. Useful if you need to handle self-signed certs. Default is nil.
122122
- `path: String` - If the server uses a custom path. ex: `"/swift"`. Default is `""`
123123
- `extraHeaders: [String: String]?` - Adds custom headers to the initial request. Default is nil.
124+
- `handleQueue: dispatch_queue_t` - The dispatch queue that handlers are run on. Default is the main queue.
124125

125126
Methods
126127
-------

SocketIOClientSwift/SocketEventHandler.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ final class SocketEventHandler {
4040

4141
func executeCallback(_ items:NSArray? = nil, withAck ack:Int? = nil, withAckType type:Int? = nil,
4242
withSocket socket:SocketIOClient? = nil) {
43-
dispatch_async(dispatch_get_main_queue()) {[weak self] in
44-
self?.callback?(items, ack != nil ? emitAckCallback(socket!, ack!) : nil)
45-
}
43+
self.callback?(items, ack != nil ? emitAckCallback(socket!, ack!) : nil)
4644
}
4745
}

SocketIOClientSwift/SocketIOClient.swift

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
4646

4747
public let socketURL:String
4848
public let handleAckQueue = dispatch_queue_create("handleAckQueue", DISPATCH_QUEUE_SERIAL)
49-
public let handleQueue = dispatch_queue_create("handleQueue", DISPATCH_QUEUE_SERIAL)
49+
public let handleQueue: dispatch_queue_t!
5050
public let emitQueue = dispatch_queue_create("emitQueue", DISPATCH_QUEUE_SERIAL)
5151
public var closed:Bool {
5252
return _closed
@@ -121,6 +121,12 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
121121
self.reconnectWait = abs(reconnectWait)
122122
}
123123

124+
if let handleQueue = opts?["handleQueue"] as? dispatch_queue_t {
125+
self.handleQueue = handleQueue
126+
} else {
127+
self.handleQueue = dispatch_get_main_queue()
128+
}
129+
124130
super.init()
125131
}
126132

@@ -393,17 +399,21 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
393399
args: event, data ?? "")
394400

395401
if anyHandler != nil {
396-
dispatch_async(dispatch_get_main_queue()) {[weak self] in
402+
dispatch_async(handleQueue) {[weak self] in
397403
self?.anyHandler?(SocketAnyEvent(event: event, items: data))
398404
}
399405
}
400406

401407
for handler in handlers {
402408
if handler.event == event {
403409
if ack != nil {
404-
handler.executeCallback(data, withAck: ack!, withSocket: self)
410+
dispatch_async(handleQueue) {[weak self] in
411+
handler.executeCallback(data, withAck: ack!, withSocket: self)
412+
}
405413
} else {
406-
handler.executeCallback(data)
414+
dispatch_async(handleQueue) {[weak self] in
415+
handler.executeCallback(data)
416+
}
407417
}
408418
}
409419
}
@@ -448,14 +458,14 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
448458
let handler = SocketEventHandler(event: event, callback: callback)
449459
handlers.append(handler)
450460
}
451-
452-
/**
453-
Removes all handlers.
454-
Can be used after disconnecting to break any potential remaining retain cycles.
455-
*/
456-
public func removeAllHandlers() {
457-
handlers.removeAll(keepCapacity: false)
458-
}
461+
462+
/**
463+
Removes all handlers.
464+
Can be used after disconnecting to break any potential remaining retain cycles.
465+
*/
466+
public func removeAllHandlers() {
467+
handlers.removeAll(keepCapacity: false)
468+
}
459469

460470
/**
461471
Adds a handler that will be called on every event.

0 commit comments

Comments
 (0)