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.
|
/// - parameter items: The items to send with this event. May be left out.
|
||||||
open func emit(_ event: String, _ items: SocketData...) {
|
open func emit(_ event: String, _ items: SocketData...) {
|
||||||
do {
|
do {
|
||||||
try emit(event, with: items.map({ try $0.socketRepresentation() }), completion: {})
|
try emit(event, with: items.map({ try $0.socketRepresentation() }))
|
||||||
} catch {
|
} catch {
|
||||||
DefaultSocketLogger.Logger.error("Error creating socketRepresentation for emit: \(event), \(items)",
|
DefaultSocketLogger.Logger.error("Error creating socketRepresentation for emit: \(event), \(items)",
|
||||||
type: logType)
|
type: logType)
|
||||||
@ -247,7 +247,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
|
|||||||
/// - parameter items: The items to send with this event. Send an empty array to send no data.
|
/// - parameter items: The items to send with this event. Send an empty array to send no data.
|
||||||
@objc
|
@objc
|
||||||
open func emit(_ event: String, with items: [Any]) {
|
open func emit(_ event: String, with items: [Any]) {
|
||||||
emit([event] + items, completion: {})
|
emit([event] + items)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Same as emit, but meant for Objective-C
|
/// Same as emit, but meant for Objective-C
|
||||||
@ -313,15 +313,22 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
|
|||||||
return createOnAck([event] + items)
|
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
|
// wrap the completion handler so it always runs async via handlerQueue
|
||||||
let wrappedCompletion = {[weak self] in
|
let wrappedCompletion = {[weak self] in
|
||||||
guard let this = self else { return }
|
guard let this = self else { return }
|
||||||
this.manager?.handleQueue.async { completion?() }
|
this.manager?.handleQueue.async {
|
||||||
|
completion()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
guard status == .connected else {
|
guard status == .connected else {
|
||||||
wrappedCompletion();
|
wrappedCompletion()
|
||||||
handleClientEvent(.error, data: ["Tried emitting when not connected"])
|
handleClientEvent(.error, data: ["Tried emitting when not connected"])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -331,7 +338,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
|
|||||||
|
|
||||||
DefaultSocketLogger.Logger.log("Emitting: \(str), Ack: \(isAck)", type: logType)
|
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`.
|
/// 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 {
|
for data in datas {
|
||||||
if case let .right(bin) = createBinaryDataForSend(using: data) {
|
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, {}))
|
postWait.append((bin, {}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -180,7 +180,7 @@ extension SocketEngineSpec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Send an engine message (4)
|
/// Send an engine message (4)
|
||||||
func send(_ msg: String, withData datas: [Data], completion: (() -> ())? = nil) {
|
func send(_ msg: String, withData datas: [Data], completion: @escaping () -> () = {}) {
|
||||||
write(msg, withType: .message, withData: datas, completion: completion ?? {})
|
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 withType: The type of message to send.
|
||||||
/// - parameter withData: The data associated with this message.
|
/// - parameter withData: The data associated with this message.
|
||||||
/// - parameter completion: Callback called on transport write completion.
|
/// - 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
|
// WebSocket methods
|
||||||
@ -57,7 +60,11 @@ extension SocketEngineWebsocket {
|
|||||||
/// - parameter withType: The type of message to send.
|
/// - parameter withType: The type of message to send.
|
||||||
/// - parameter withData: The data associated with this message.
|
/// - parameter withData: The data associated with this message.
|
||||||
/// - parameter completion: Callback called on transport write completion.
|
/// - 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")
|
DefaultSocketLogger.Logger.log("Sending ws: \(str) as type: \(type.rawValue)", type: "SocketEngineWebSocket")
|
||||||
|
|
||||||
ws?.write(string: "\(type.rawValue)\(str)")
|
ws?.write(string: "\(type.rawValue)\(str)")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user