Skip to content

Commit e00136f

Browse files
authored
Merge pull request #164 from pusher/unsubsribe-all
Unsubsribe all
2 parents 4c26917 + f38a5f3 commit e00136f

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

Sources/PusherConnection.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ public typealias PusherEventJSON = [String: AnyObject]
176176
self.channels.remove(name: channelName)
177177
}
178178
}
179+
180+
/**
181+
Unsubscribes from all PusherChannels
182+
*/
183+
internal func unsubscribeAll() {
184+
for (_, channel) in channels.channels {
185+
unsubscribe(channelName: channel.name)
186+
}
187+
}
179188

180189
/**
181190
Either writes a string directly to the websocket with the given event name

Sources/PusherSwift.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ let CLIENT_NAME = "pusher-websocket-swift"
129129
open func unsubscribe(_ channelName: String) {
130130
self.connection.unsubscribe(channelName: channelName)
131131
}
132+
133+
/**
134+
Unsubscribes the client from all channels
135+
*/
136+
open func unsubscribeAll() {
137+
self.connection.unsubscribeAll()
138+
}
132139

133140
/**
134141
Binds the client's global channel to all events

Tests/Mocks.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ open class MockWebSocket: WebSocket {
6060
self.delegate?.websocketDidReceiveMessage(socket: self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"channel\":\"test-channel\",\"data\":\"{}\"}")
6161
}
6262
)
63+
} else if string == "{\"data\":{\"channel\":\"test-channel2\"},\"event\":\"pusher:subscribe\"}" || string == "{\"event\":\"pusher:subscribe\",\"data\":{\"channel\":\"test-channel2\"}}" {
64+
let _ = stubber.stub(
65+
functionName: "writeString",
66+
args: [string],
67+
functionToCall: {
68+
self.delegate?.websocketDidReceiveMessage(socket: self, text: "{\"event\":\"pusher_internal:subscription_succeeded\",\"channel\":\"test-channel2\",\"data\":\"{}\"}")
69+
}
70+
)
6371
} else if stringContainsElements(string, elements: ["testkey123:6aae8814fabd5285245422096705abbed64ea59614648814ffb0bf2dc5d19168", "private-channel", "pusher:subscribe"]) {
6472
let _ = stubber.stub(
6573
functionName: "writeString",
@@ -144,6 +152,12 @@ open class MockWebSocket: WebSocket {
144152
args: [string],
145153
functionToCall: nil
146154
)
155+
} else if stringContainsElements(string, elements: ["test-channel2", "pusher:unsubscribe"]) {
156+
let _ = stubber.stub(
157+
functionName: "writeString",
158+
args: [string],
159+
functionToCall: nil
160+
)
147161
} else if stringContainsElements(string, elements: ["testkey123:736f0b19c2e56f985f3e6faa38db5b69d39305bc8519952c8f9f5595d69fcb3d", "presence-test", "user_id", "123", "pusher:subscribe", "user_info", "twitter", "hamchapman"]) {
148162
let _ = stubber.stub(
149163
functionName: "writeString",

Tests/PusherTopLevelAPITests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,24 @@ class PusherTopLevelApiTests: XCTestCase {
232232
XCTAssertTrue(parsedEqualsExpected)
233233
}
234234

235+
func testUnsubscribingFromAllChannelsRemovesTheChannels() {
236+
pusher.connect()
237+
let _ = pusher.subscribe("test-channel")
238+
let _ = pusher.subscribe("test-channel2")
239+
XCTAssertEqual(pusher.connection.channels.channels.count, 2, "should have 2 channels")
240+
XCTAssertEqual(socket.stubber.calls.last?.name, "writeString", "write function should have been called")
241+
pusher.unsubscribeAll()
242+
243+
XCTAssertEqual(socket.stubber.calls.last?.name, "writeString", "write function should have been called")
244+
245+
let parsedSubscribeArgs = convertStringToDictionary(socket.stubber.calls.last?.args!.first as! String)
246+
let expectedDict = ["data": ["channel": "test-channel2"], "event": "pusher:unsubscribe"] as [String: Any]
247+
let parsedEqualsExpected = NSDictionary(dictionary: parsedSubscribeArgs!).isEqual(to: NSDictionary(dictionary: expectedDict) as [NSObject: AnyObject])
248+
249+
XCTAssertTrue(parsedEqualsExpected)
250+
XCTAssertEqual(pusher.connection.channels.channels.count, 0, "should have no channels")
251+
}
252+
235253
/* global channel interactions */
236254

237255
func testBindingToEventsGloballyAddsACallbackToTheGlobalChannel() {

0 commit comments

Comments
 (0)