Skip to content

Commit 7505702

Browse files
committed
clean up emit completion code
1 parent d839a35 commit 7505702

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

Source/SocketIO/Client/SocketIOClient.swift

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,15 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
213213
/// - parameter items: The items to send with this event. May be left out.
214214
open func emit(_ event: String, _ items: SocketData...) {
215215
do {
216-
try emit(event, with: items.map({ try $0.socketRepresentation() }), completion: {})
216+
try emit(event, with: items.map({ try $0.socketRepresentation() }))
217217
} catch {
218218
DefaultSocketLogger.Logger.error("Error creating socketRepresentation for emit: \(event), \(items)",
219219
type: logType)
220220

221221
handleClientEvent(.error, data: [event, items, error])
222222
}
223223
}
224-
224+
225225
/// Send an event to the server, with optional data items and write completion handler.
226226
///
227227
/// If an error occurs trying to transform `items` into their socket representation, a `SocketClientEvent.error`
@@ -247,9 +247,9 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
247247
/// - parameter items: The items to send with this event. Send an empty array to send no data.
248248
@objc
249249
open func emit(_ event: String, with items: [Any]) {
250-
emit([event] + items, completion: {})
250+
emit([event] + items)
251251
}
252-
252+
253253
/// Same as emit, but meant for Objective-C
254254
///
255255
/// - parameter event: The event to send.
@@ -313,15 +313,22 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
313313
return createOnAck([event] + items)
314314
}
315315

316-
func emit(_ data: [Any], ack: Int? = nil, binary: Bool = true, isAck: Bool = false, completion: (() -> ())? = nil) {
316+
func emit(_ data: [Any],
317+
ack: Int? = nil,
318+
binary: Bool = true,
319+
isAck: Bool = false,
320+
completion: @escaping () -> () = {}
321+
) {
317322
// wrap the completion handler so it always runs async via handlerQueue
318323
let wrappedCompletion = {[weak self] in
319324
guard let this = self else { return }
320-
this.manager?.handleQueue.async { completion?() }
325+
this.manager?.handleQueue.async {
326+
completion()
327+
}
321328
}
322-
329+
323330
guard status == .connected else {
324-
wrappedCompletion();
331+
wrappedCompletion()
325332
handleClientEvent(.error, data: ["Tried emitting when not connected"])
326333
return
327334
}
@@ -331,7 +338,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
331338

332339
DefaultSocketLogger.Logger.log("Emitting: \(str), Ack: \(isAck)", type: logType)
333340

334-
manager?.engine?.send(str, withData: packet.binary, completion: completion != nil ? wrappedCompletion : nil)
341+
manager?.engine?.send(str, withData: packet.binary, completion: wrappedCompletion)
335342
}
336343

337344
/// Call when you wish to tell the server that you've received the event for `ack`.

Source/SocketIO/Engine/SocketEnginePollable.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,6 @@ extension SocketEnginePollable {
226226

227227
for data in datas {
228228
if case let .right(bin) = createBinaryDataForSend(using: data) {
229-
// completion handler will be called on initial message write
230-
// TODO: call completion after last message in batch
231229
postWait.append((bin, {}))
232230
}
233231
}

Source/SocketIO/Engine/SocketEngineSpec.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ extension SocketEngineSpec {
159159
func addHeaders(to req: inout URLRequest, includingCookies additionalCookies: [HTTPCookie]? = nil) {
160160
var cookiesToAdd: [HTTPCookie] = cookies ?? []
161161
cookiesToAdd += additionalCookies ?? []
162-
162+
163163
if !cookiesToAdd.isEmpty {
164164
req.allHTTPHeaderFields = HTTPCookie.requestHeaderFields(with: cookiesToAdd)
165165
}
@@ -180,7 +180,7 @@ extension SocketEngineSpec {
180180
}
181181

182182
/// Send an engine message (4)
183-
func send(_ msg: String, withData datas: [Data], completion: (() -> ())? = nil) {
184-
write(msg, withType: .message, withData: datas, completion: completion ?? {})
183+
func send(_ msg: String, withData datas: [Data], completion: @escaping () -> () = {}) {
184+
write(msg, withType: .message, withData: datas, completion: completion)
185185
}
186186
}

Source/SocketIO/Engine/SocketEngineWebsocket.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ public protocol SocketEngineWebsocket : SocketEngineSpec {
3838
/// - parameter withType: The type of message to send.
3939
/// - parameter withData: The data associated with this message.
4040
/// - parameter completion: Callback called on transport write completion.
41-
func sendWebSocketMessage(_ str: String, withType type: SocketEnginePacketType, withData datas: [Data], completion: @escaping () -> ())
41+
func sendWebSocketMessage(_ str: String,
42+
withType type: SocketEnginePacketType,
43+
withData datas: [Data],
44+
completion: @escaping () -> ())
4245
}
4346

4447
// WebSocket methods
@@ -57,7 +60,11 @@ extension SocketEngineWebsocket {
5760
/// - parameter withType: The type of message to send.
5861
/// - parameter withData: The data associated with this message.
5962
/// - parameter completion: Callback called on transport write completion.
60-
public func sendWebSocketMessage(_ str: String, withType type: SocketEnginePacketType, withData datas: [Data], completion: @escaping () -> ()) {
63+
public func sendWebSocketMessage(_ str: String,
64+
withType type: SocketEnginePacketType,
65+
withData datas: [Data],
66+
completion: @escaping () -> ()
67+
) {
6168
DefaultSocketLogger.Logger.log("Sending ws: \(str) as type: \(type.rawValue)", type: "SocketEngineWebSocket")
6269

6370
ws?.write(string: "\(type.rawValue)\(str)")

0 commit comments

Comments
 (0)