From 75057023cbccc252ab35bc0580332e9dfd15936b Mon Sep 17 00:00:00 2001 From: Erik Little Date: Wed, 17 Oct 2018 09:55:11 -0400 Subject: [PATCH] clean up emit completion code --- Source/SocketIO/Client/SocketIOClient.swift | 25 ++++++++++++------- .../Engine/SocketEnginePollable.swift | 2 -- Source/SocketIO/Engine/SocketEngineSpec.swift | 6 ++--- .../Engine/SocketEngineWebsocket.swift | 11 ++++++-- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/Source/SocketIO/Client/SocketIOClient.swift b/Source/SocketIO/Client/SocketIOClient.swift index 7ac8b74..f724552 100644 --- a/Source/SocketIO/Client/SocketIOClient.swift +++ b/Source/SocketIO/Client/SocketIOClient.swift @@ -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`. diff --git a/Source/SocketIO/Engine/SocketEnginePollable.swift b/Source/SocketIO/Engine/SocketEnginePollable.swift index 7b70736..7e09307 100644 --- a/Source/SocketIO/Engine/SocketEnginePollable.swift +++ b/Source/SocketIO/Engine/SocketEnginePollable.swift @@ -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, {})) } } diff --git a/Source/SocketIO/Engine/SocketEngineSpec.swift b/Source/SocketIO/Engine/SocketEngineSpec.swift index c7b073c..f97490e 100644 --- a/Source/SocketIO/Engine/SocketEngineSpec.swift +++ b/Source/SocketIO/Engine/SocketEngineSpec.swift @@ -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) } } diff --git a/Source/SocketIO/Engine/SocketEngineWebsocket.swift b/Source/SocketIO/Engine/SocketEngineWebsocket.swift index b3fad5e..0ded9a4 100644 --- a/Source/SocketIO/Engine/SocketEngineWebsocket.swift +++ b/Source/SocketIO/Engine/SocketEngineWebsocket.swift @@ -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)")