Add a variadic method for emit completion handlers in swift

This commit is contained in:
Andy 2018-10-02 20:55:38 +07:00
parent d763fad449
commit b3e305fd14
3 changed files with 27 additions and 0 deletions

View File

@ -221,6 +221,18 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
handleClientEvent(.error, data: [event, items, error]) 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`
/// will be emitted. The structure of the error data is `[eventName, items, theError]`
///
/// - parameter event: The event to send.
/// - parameter items: The items to send with this event. May be left out.
/// - parameter completion: Callback called on transport write completion.
open func emit(_ event: String, _ items: SocketData..., completion: @escaping () -> ()) {
emit([event] + items, completion: completion)
}
/// Same as emit, but meant for Objective-C /// Same as emit, but meant for Objective-C
/// ///

View File

@ -101,6 +101,16 @@ public protocol SocketIOClientSpec : AnyObject {
/// - 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.
func emit(_ event: String, _ items: SocketData...) func emit(_ event: String, _ items: SocketData...)
/// 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`
/// will be emitted. The structure of the error data is `[eventName, items, theError]`
///
/// - parameter event: The event to send.
/// - parameter items: The items to send with this event. May be left out.
/// - parameter completion: Callback called on transport write completion.
func emit(_ event: String, _ items: SocketData..., completion: @escaping () -> ())
/// 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`.
/// ///
/// - parameter ack: The ack number. /// - parameter ack: The ack number.

View File

@ -27,6 +27,11 @@ class SocketSideEffectTest: XCTestCase {
XCTAssertEqual(socket.currentAck, 1) XCTAssertEqual(socket.currentAck, 1)
} }
func testEmitCompletionSyntax() {
socket.emit("test", completion: {})
socket.emit("test", "thing", completion: {})
}
func testHandleAck() { func testHandleAck() {
let expect = expectation(description: "handled ack") let expect = expectation(description: "handled ack")
socket.emitWithAck("test").timingOut(after: 0) {data in socket.emitWithAck("test").timingOut(after: 0) {data in