Skip to content

Commit b3e305f

Browse files
committed
Add a variadic method for emit completion handlers in swift
1 parent d763fad commit b3e305f

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

Source/SocketIO/Client/SocketIOClient.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,18 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
221221
handleClientEvent(.error, data: [event, items, error])
222222
}
223223
}
224+
225+
/// Send an event to the server, with optional data items and write completion handler.
226+
///
227+
/// If an error occurs trying to transform `items` into their socket representation, a `SocketClientEvent.error`
228+
/// will be emitted. The structure of the error data is `[eventName, items, theError]`
229+
///
230+
/// - parameter event: The event to send.
231+
/// - parameter items: The items to send with this event. May be left out.
232+
/// - parameter completion: Callback called on transport write completion.
233+
open func emit(_ event: String, _ items: SocketData..., completion: @escaping () -> ()) {
234+
emit([event] + items, completion: completion)
235+
}
224236

225237
/// Same as emit, but meant for Objective-C
226238
///

Source/SocketIO/Client/SocketIOClientSpec.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ public protocol SocketIOClientSpec : AnyObject {
101101
/// - parameter items: The items to send with this event. May be left out.
102102
func emit(_ event: String, _ items: SocketData...)
103103

104+
/// Send an event to the server, with optional data items and write completion handler.
105+
///
106+
/// If an error occurs trying to transform `items` into their socket representation, a `SocketClientEvent.error`
107+
/// will be emitted. The structure of the error data is `[eventName, items, theError]`
108+
///
109+
/// - parameter event: The event to send.
110+
/// - parameter items: The items to send with this event. May be left out.
111+
/// - parameter completion: Callback called on transport write completion.
112+
func emit(_ event: String, _ items: SocketData..., completion: @escaping () -> ())
113+
104114
/// Call when you wish to tell the server that you've received the event for `ack`.
105115
///
106116
/// - parameter ack: The ack number.

Tests/TestSocketIO/SocketSideEffectTest.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class SocketSideEffectTest: XCTestCase {
2727
XCTAssertEqual(socket.currentAck, 1)
2828
}
2929

30+
func testEmitCompletionSyntax() {
31+
socket.emit("test", completion: {})
32+
socket.emit("test", "thing", completion: {})
33+
}
34+
3035
func testHandleAck() {
3136
let expect = expectation(description: "handled ack")
3237
socket.emitWithAck("test").timingOut(after: 0) {data in

0 commit comments

Comments
 (0)