clean up emit completion code
This commit is contained in:
parent
d839a35340
commit
75057023cb
@ -213,7 +213,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
|
||||
/// - parameter items: The items to send with this event. May be left out.
|
||||
open func emit(_ event: String, _ items: SocketData...) {
|
||||
do {
|
||||
try emit(event, with: items.map({ try $0.socketRepresentation() }), completion: {})
|
||||
try emit(event, with: items.map({ try $0.socketRepresentation() }))
|
||||
} catch {
|
||||
DefaultSocketLogger.Logger.error("Error creating socketRepresentation for emit: \(event), \(items)",
|
||||
type: logType)
|
||||
@ -221,7 +221,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
|
||||
handleClientEvent(.error, data: [event, items, error])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Send an event to the server, with optional data items and write completion handler.
|
||||
///
|
||||
/// If an error occurs trying to transform `items` into their socket representation, a `SocketClientEvent.error`
|
||||
@ -247,9 +247,9 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
|
||||
/// - parameter items: The items to send with this event. Send an empty array to send no data.
|
||||
@objc
|
||||
open func emit(_ event: String, with items: [Any]) {
|
||||
emit([event] + items, completion: {})
|
||||
emit([event] + items)
|
||||
}
|
||||
|
||||
|
||||
/// Same as emit, but meant for Objective-C
|
||||
///
|
||||
/// - parameter event: The event to send.
|
||||
@ -313,15 +313,22 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
|
||||
return createOnAck([event] + items)
|
||||
}
|
||||
|
||||
func emit(_ data: [Any], ack: Int? = nil, binary: Bool = true, isAck: Bool = false, completion: (() -> ())? = nil) {
|
||||
func emit(_ data: [Any],
|
||||
ack: Int? = nil,
|
||||
binary: Bool = true,
|
||||
isAck: Bool = false,
|
||||
completion: @escaping () -> () = {}
|
||||
) {
|
||||
// wrap the completion handler so it always runs async via handlerQueue
|
||||
let wrappedCompletion = {[weak self] in
|
||||
guard let this = self else { return }
|
||||
this.manager?.handleQueue.async { completion?() }
|
||||
this.manager?.handleQueue.async {
|
||||
completion()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
guard status == .connected else {
|
||||
wrappedCompletion();
|
||||
wrappedCompletion()
|
||||
handleClientEvent(.error, data: ["Tried emitting when not connected"])
|
||||
return
|
||||
}
|
||||
@ -331,7 +338,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
|
||||
|
||||
DefaultSocketLogger.Logger.log("Emitting: \(str), Ack: \(isAck)", type: logType)
|
||||
|
||||
manager?.engine?.send(str, withData: packet.binary, completion: completion != nil ? wrappedCompletion : nil)
|
||||
manager?.engine?.send(str, withData: packet.binary, completion: wrappedCompletion)
|
||||
}
|
||||
|
||||
/// Call when you wish to tell the server that you've received the event for `ack`.
|
||||
|
||||
@ -226,8 +226,6 @@ extension SocketEnginePollable {
|
||||
|
||||
for data in datas {
|
||||
if case let .right(bin) = createBinaryDataForSend(using: data) {
|
||||
// completion handler will be called on initial message write
|
||||
// TODO: call completion after last message in batch
|
||||
postWait.append((bin, {}))
|
||||
}
|
||||
}
|
||||
|
||||
@ -159,7 +159,7 @@ extension SocketEngineSpec {
|
||||
func addHeaders(to req: inout URLRequest, includingCookies additionalCookies: [HTTPCookie]? = nil) {
|
||||
var cookiesToAdd: [HTTPCookie] = cookies ?? []
|
||||
cookiesToAdd += additionalCookies ?? []
|
||||
|
||||
|
||||
if !cookiesToAdd.isEmpty {
|
||||
req.allHTTPHeaderFields = HTTPCookie.requestHeaderFields(with: cookiesToAdd)
|
||||
}
|
||||
@ -180,7 +180,7 @@ extension SocketEngineSpec {
|
||||
}
|
||||
|
||||
/// Send an engine message (4)
|
||||
func send(_ msg: String, withData datas: [Data], completion: (() -> ())? = nil) {
|
||||
write(msg, withType: .message, withData: datas, completion: completion ?? {})
|
||||
func send(_ msg: String, withData datas: [Data], completion: @escaping () -> () = {}) {
|
||||
write(msg, withType: .message, withData: datas, completion: completion)
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,7 +38,10 @@ public protocol SocketEngineWebsocket : SocketEngineSpec {
|
||||
/// - parameter withType: The type of message to send.
|
||||
/// - parameter withData: The data associated with this message.
|
||||
/// - parameter completion: Callback called on transport write completion.
|
||||
func sendWebSocketMessage(_ str: String, withType type: SocketEnginePacketType, withData datas: [Data], completion: @escaping () -> ())
|
||||
func sendWebSocketMessage(_ str: String,
|
||||
withType type: SocketEnginePacketType,
|
||||
withData datas: [Data],
|
||||
completion: @escaping () -> ())
|
||||
}
|
||||
|
||||
// WebSocket methods
|
||||
@ -57,7 +60,11 @@ extension SocketEngineWebsocket {
|
||||
/// - parameter withType: The type of message to send.
|
||||
/// - parameter withData: The data associated with this message.
|
||||
/// - parameter completion: Callback called on transport write completion.
|
||||
public func sendWebSocketMessage(_ str: String, withType type: SocketEnginePacketType, withData datas: [Data], completion: @escaping () -> ()) {
|
||||
public func sendWebSocketMessage(_ str: String,
|
||||
withType type: SocketEnginePacketType,
|
||||
withData datas: [Data],
|
||||
completion: @escaping () -> ()
|
||||
) {
|
||||
DefaultSocketLogger.Logger.log("Sending ws: \(str) as type: \(type.rawValue)", type: "SocketEngineWebSocket")
|
||||
|
||||
ws?.write(string: "\(type.rawValue)\(str)")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user