Skip to content

Commit c642017

Browse files
committed
Merge pull request #250 from socketio/off-with-id
Off with id
2 parents ccad13a + 7b11e7c commit c642017

File tree

3 files changed

+64
-14
lines changed

3 files changed

+64
-14
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ case Secure(Bool) // If the connection should use TLS. Default is false.
150150
```
151151
Methods
152152
-------
153-
1. `on(event: String, callback: NormalCallback)` - Adds a handler for an event. Items are passed by an array. `ack` can be used to send an ack when one is requested. See example.
154-
2. `once(event: String, callback: NormalCallback)` - Adds a handler that will only be executed once.
153+
1. `on(event: String, callback: NormalCallback) -> NSUUID` - Adds a handler for an event. Items are passed by an array. `ack` can be used to send an ack when one is requested. See example. Returns a unique id for the handler.
154+
2. `once(event: String, callback: NormalCallback) -> NSUUID` - Adds a handler that will only be executed once. Returns a unique id for the handler.
155155
3. `onAny(callback:((event: String, items: AnyObject?)) -> Void)` - Adds a handler for all events. It will be called on any received event.
156156
4. `emit(event: String, _ items: AnyObject...)` - Sends a message. Can send multiple items.
157157
5. `emit(event: String, withItems items: [AnyObject])` - `emit` for Objective-C
@@ -164,7 +164,8 @@ Methods
164164
12. `joinNamespace()` - Causes the client to join nsp. Shouldn't need to be called unless you change nsp manually.
165165
13. `leaveNamespace()` - Causes the client to leave the nsp and go back to /
166166
14. `off(event: String)` - Removes all event handlers for event.
167-
15. `removeAllHandlers()` - Removes all handlers.
167+
15. `off(id id: NSUUID)` - Removes the event that corresponds to id.
168+
16. `removeAllHandlers()` - Removes all handlers.
168169

169170
Client Events
170171
------

SocketIO-MacTests/SocketSideEffectTest.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@ class SocketSideEffectTest: XCTestCase {
5757
waitForExpectationsWithTimeout(3, handler: nil)
5858
}
5959

60+
func testHandleOnceEvent() {
61+
let expectation = expectationWithDescription("handled event")
62+
socket.once("test") {data, ack in
63+
XCTAssertEqual(data[0] as? String, "hello world")
64+
XCTAssertEqual(self.socket.testHandlers.count, 0)
65+
expectation.fulfill()
66+
}
67+
68+
socket.parseSocketMessage("2[\"test\",\"hello world\"]")
69+
waitForExpectationsWithTimeout(3, handler: nil)
70+
}
71+
72+
func testOffWithEvent() {
73+
socket.on("test") {data, ack in }
74+
XCTAssertEqual(socket.testHandlers.count, 1)
75+
socket.on("test") {data, ack in }
76+
XCTAssertEqual(socket.testHandlers.count, 2)
77+
socket.off("test")
78+
XCTAssertEqual(socket.testHandlers.count, 0)
79+
}
80+
81+
func testOffWithId() {
82+
let handler = socket.on("test") {data, ack in }
83+
XCTAssertEqual(socket.testHandlers.count, 1)
84+
socket.on("test") {data, ack in }
85+
XCTAssertEqual(socket.testHandlers.count, 2)
86+
socket.off(id: handler)
87+
XCTAssertEqual(socket.testHandlers.count, 1)
88+
}
89+
6090
func testHandleBinaryEvent() {
6191
let expectation = expectationWithDescription("handled binary event")
6292
socket.on("test") {data, ack in

Source/SocketIOClient.swift

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -389,39 +389,46 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
389389
handlers = handlers.filter { $0.event != event }
390390
}
391391

392+
/**
393+
Removes a handler with the specified UUID gotten from an `on` or `once`
394+
*/
395+
public func off(id id: NSUUID) {
396+
DefaultSocketLogger.Logger.log("Removing handler with id: %@", type: logType, args: id)
397+
398+
handlers = handlers.filter { $0.id != id }
399+
}
400+
392401
/**
393402
Adds a handler for an event.
403+
Returns: A unique id for the handler
394404
*/
395-
public func on(event: String, callback: NormalCallback) {
405+
public func on(event: String, callback: NormalCallback) -> NSUUID {
396406
DefaultSocketLogger.Logger.log("Adding handler for event: %@", type: logType, args: event)
397407

398408
let handler = SocketEventHandler(event: event, id: NSUUID(), callback: callback)
399409
handlers.append(handler)
410+
411+
return handler.id
400412
}
401413

402414
/**
403415
Adds a single-use handler for an event.
416+
Returns: A unique id for the handler
404417
*/
405-
public func once(event: String, callback: NormalCallback) {
418+
public func once(event: String, callback: NormalCallback) -> NSUUID {
406419
DefaultSocketLogger.Logger.log("Adding once handler for event: %@", type: logType, args: event)
407420

408421
let id = NSUUID()
409422

410423
let handler = SocketEventHandler(event: event, id: id) {[weak self] data, ack in
411424
guard let this = self else {return}
412-
this.handlers = this.handlers.filter {$0.id != id}
425+
this.off(id: id)
413426
callback(data, ack)
414427
}
415428

416429
handlers.append(handler)
417-
}
418-
419-
/**
420-
Removes all handlers.
421-
Can be used after disconnecting to break any potential remaining retain cycles.
422-
*/
423-
public func removeAllHandlers() {
424-
handlers.removeAll(keepCapacity: false)
430+
431+
return handler.id
425432
}
426433

427434
/**
@@ -457,6 +464,14 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
457464
tryReconnect()
458465
}
459466

467+
/**
468+
Removes all handlers.
469+
Can be used after disconnecting to break any potential remaining retain cycles.
470+
*/
471+
public func removeAllHandlers() {
472+
handlers.removeAll(keepCapacity: false)
473+
}
474+
460475
private func tryReconnect() {
461476
if reconnectTimer == nil {
462477
DefaultSocketLogger.Logger.log("Starting reconnect", type: logType)
@@ -495,6 +510,10 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
495510

496511
// Test extensions
497512
extension SocketIOClient {
513+
var testHandlers: [SocketEventHandler] {
514+
return handlers
515+
}
516+
498517
func setTestable() {
499518
status = .Connected
500519
}

0 commit comments

Comments
 (0)