Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SocketIOClientSwift/SocketEventHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private func emitAckCallbackObjectiveC(socket: SocketIOClient?, num: Int?)
}

struct SocketEventHandler {
let id = NSUUID()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't compare structs. I looked at other Swift-based event dispatchers that support removing specific handlers and they use id's like this to compare them.

let event: String
let callback: NormalCallback?
let callBackObjectiveC: NormalCallbackObjectiveC?
Expand Down
32 changes: 32 additions & 0 deletions SocketIOClientSwift/SocketIOClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,38 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
handlers.append(handler)
}

/**
Adds a single-use handler for an event.
*/
public func once(event: String, callback: NormalCallback) {
Logger.log("Adding once handler for event: %@", type: logType, args: event)

var handler: SocketEventHandler?;

handler = SocketEventHandler(event: event) { (data, ack: AckEmitter?) in
self.handlers = ContiguousArray(self.handlers.filter { $0.id != handler!.id })
callback(data, ack)
}

handlers.append(handler!)
}

/**
Adds a single-use handler for an event.
*/
public func once(event event: String, callback: NormalCallbackObjectiveC) {
Logger.log("Adding once handler for event: %@", type: logType, args: event)

var handler: SocketEventHandler?;

handler = SocketEventHandler(event: event) { (data, ack: AckEmitterObjectiveC?) in
self.handlers = ContiguousArray(self.handlers.filter { $0.id != handler!.id })
callback(data, ack)
}

handlers.append(handler!)
}

/**
Removes all handlers.
Can be used after disconnecting to break any potential remaining retain cycles.
Expand Down