socket.io-client-swift icon indicating copy to clipboard operation
socket.io-client-swift copied to clipboard

Emit parameters wrapped twice

Open OneSman7 opened this issue 4 years ago • 2 comments

Hello! Found an issue with your code emit methods syntax, which breaks the use of it if wrapped in another method. Take for example a simple emit:

open func emit(_ event: String, _ items: SocketData..., completion: (() -> ())? = nil)  {
        do {
            emit([event] + (try items.map({ try $0.socketRepresentation() })), completion: completion)
        } catch {
            DefaultSocketLogger.Logger.error("Error creating socketRepresentation for emit: \(event), \(items)",
                                             type: logType)

            handleClientEvent(.error, data: [event, items, error])
        }
    }

You make use of variadic parameter, which is totally fine when used directly. For example socket.emit(event, arg1, arg2)

But if you wrap the call to this method:

func send(event: String, with arguments: [SocketTransferable]) {
       socket.emit(event, arguments)
}

the call send(event, arguments) will result in arguments being wrapped in array again because of how variadic parameters work. I can even write my method like this

func send(event: String, with arguments: SocketTransferable...) {
       socket.emit(event, arguments)
}

and still get errors from server for calls like send(event, arg1, arg2).

OneSman7 avatar Feb 05 '21 15:02 OneSman7

A great idea will be to have an overload that accepts array. Like old obj-c methods that were removed

OneSman7 avatar Feb 05 '21 15:02 OneSman7

@nuclearace I created PR (https://github.com/socketio/socket.io-client-swift/pull/1324) wit proposed changes

OneSman7 avatar Feb 05 '21 16:02 OneSman7