From 24f3106fe7b3cae676a1c76f1288b7da65a89d87 Mon Sep 17 00:00:00 2001 From: Erik Little Date: Fri, 30 Mar 2018 14:56:19 -0400 Subject: [PATCH] Add raw view to ack emitter --- Source/SocketIO/Ack/SocketAckEmitter.swift | 16 ++++- Source/SocketIO/Client/SocketIOClient.swift | 6 +- Source/SocketIO/Client/SocketRawView.swift | 67 ++++++++++++++++++- Tests/TestSocketIOObjc/SocketObjectiveCTest.m | 1 + 4 files changed, 85 insertions(+), 5 deletions(-) diff --git a/Source/SocketIO/Ack/SocketAckEmitter.swift b/Source/SocketIO/Ack/SocketAckEmitter.swift index 274db9f..417fe94 100644 --- a/Source/SocketIO/Ack/SocketAckEmitter.swift +++ b/Source/SocketIO/Ack/SocketAckEmitter.swift @@ -29,8 +29,20 @@ import Foundation /// /// **NOTE**: You should not store this beyond the life of the event handler. public final class SocketAckEmitter : NSObject { - let socket: SocketIOClient - let ackNum: Int + private unowned let socket: SocketIOClient + private let ackNum: Int + + /// A view into this emitter where emits do not check for binary data. + /// + /// Usage: + /// + /// ```swift + /// ack.rawEmitView.with(myObject) + /// ``` + /// + /// **NOTE**: It is not safe to hold on to this view beyond the life of the socket. + @objc + public private(set) lazy var rawEmitView = SocketRawAckView(socket: socket, ackNum: ackNum) // MARK: Properties diff --git a/Source/SocketIO/Client/SocketIOClient.swift b/Source/SocketIO/Client/SocketIOClient.swift index 03364a8..0318054 100644 --- a/Source/SocketIO/Client/SocketIOClient.swift +++ b/Source/SocketIO/Client/SocketIOClient.swift @@ -310,9 +310,13 @@ open class SocketIOClient : NSObject, SocketIOClientSpec { /// - parameter ack: The ack number. /// - parameter with: The data for this ack. open func emitAck(_ ack: Int, with items: [Any]) { + emitAck(ack, with: items, binary: true) + } + + func emitAck(_ ack: Int, with items: [Any], binary: Bool) { guard status == .connected else { return } - let packet = SocketPacket.packetFromEmit(items, id: ack, nsp: nsp, ack: true, checkForBinary: true) + let packet = SocketPacket.packetFromEmit(items, id: ack, nsp: nsp, ack: true, checkForBinary: binary) let str = packet.packetString DefaultSocketLogger.Logger.log("Emitting Ack: \(str)", type: logType) diff --git a/Source/SocketIO/Client/SocketRawView.swift b/Source/SocketIO/Client/SocketRawView.swift index b7c1f8c..ed4389b 100644 --- a/Source/SocketIO/Client/SocketRawView.swift +++ b/Source/SocketIO/Client/SocketRawView.swift @@ -1,6 +1,26 @@ // -// Created by Erik Little on 3/30/18. +// SocketRawView.swift +// Socket.IO-Client-Swift // +// Created by Erik Little on 3/30/18. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. import Foundation @@ -11,7 +31,7 @@ import Foundation /// ```swift /// socket.rawEmitView.emit("myEvent", myObject) /// ``` -public final class SocketRawView: NSObject { +public final class SocketRawView : NSObject { private unowned let socket: SocketIOClient init(socket: SocketIOClient) { @@ -103,3 +123,46 @@ public final class SocketRawView: NSObject { return socket.createOnAck([event] + items, binary: false) } } + +/// Class that gives a backwards compatible way to cause an emit not to recursively check for Data objects. +/// +/// Usage: +/// +/// ```swift +/// ack.rawEmitView.with(myObject) +/// ``` +public final class SocketRawAckView : NSObject { + private unowned let socket: SocketIOClient + private let ackNum: Int + + init(socket: SocketIOClient, ackNum: Int) { + self.socket = socket + self.ackNum = ackNum + } + + /// Call to ack receiving this event. + /// + /// 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 `[ackNum, items, theError]` + /// + /// - parameter items: A variable number of items to send when acking. + public func with(_ items: SocketData...) { + guard ackNum != -1 else { return } + + do { + socket.emitAck(ackNum, with: try items.map({ try $0.socketRepresentation() }), binary: false) + } catch let err { + socket.handleClientEvent(.error, data: [ackNum, items, err]) + } + } + + /// Call to ack receiving this event. + /// + /// - parameter items: An array of items to send when acking. Use `[]` to send nothing. + @objc + public func with(_ items: [Any]) { + guard ackNum != -1 else { return } + + socket.emitAck(ackNum, with: items, binary: false) + } +} diff --git a/Tests/TestSocketIOObjc/SocketObjectiveCTest.m b/Tests/TestSocketIOObjc/SocketObjectiveCTest.m index 55b6710..1ad3475 100644 --- a/Tests/TestSocketIOObjc/SocketObjectiveCTest.m +++ b/Tests/TestSocketIOObjc/SocketObjectiveCTest.m @@ -26,6 +26,7 @@ - (void)testOnSyntax { [self.socket on:@"someCallback" callback:^(NSArray* data, SocketAckEmitter* ack) { [ack with:@[@1]]; + [[ack rawEmitView] with:@[@"hello"]]; }]; }