Add raw view to ack emitter

This commit is contained in:
Erik Little 2018-03-30 14:56:19 -04:00
parent 1b6144a78c
commit 24f3106fe7
No known key found for this signature in database
GPG Key ID: 62F837E56F4E9320
4 changed files with 85 additions and 5 deletions

View File

@ -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

View File

@ -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)

View File

@ -1,6 +1,26 @@
//
// 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
@ -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)
}
}

View File

@ -26,6 +26,7 @@
- (void)testOnSyntax {
[self.socket on:@"someCallback" callback:^(NSArray* data, SocketAckEmitter* ack) {
[ack with:@[@1]];
[[ack rawEmitView] with:@[@"hello"]];
}];
}