Skip to content

Commit dbd23a3

Browse files
committed
Remove legacy 'bind(…)' methods on 'Pusher' and 'PusherChannel'
1 parent e102d11 commit dbd23a3

9 files changed

+52
-230
lines changed

Sources/Models/PusherChannel.swift

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ open class PusherChannel: NSObject {
3535
}
3636
}
3737

38-
internal var shouldParseJSONForLegacyCallbacks: Bool {
39-
return connection?.options.attemptToReturnJSONObject ?? true
40-
}
41-
4238
/**
4339
Initializes a new PusherChannel with a given name and connection
4440

@@ -56,37 +52,6 @@ open class PusherChannel: NSObject {
5652
self.type = PusherChannelType(name: name)
5753
}
5854

59-
/**
60-
Binds a callback to a given event name, scoped to the PusherChannel the function is
61-
called on
62-
63-
- parameter eventName: The name of the event to bind to
64-
- parameter callback: The function to call when a new event is received. The
65-
callback receives the event's data payload
66-
67-
- returns: A unique callbackId that can be used to unbind the callback at a later time
68-
*/
69-
@available(*, deprecated, message: "This will be removed in v10.0.0. Use 'bind(eventName:eventCallback:)' instead.")
70-
@discardableResult open func bind(eventName: String, callback: @escaping (Any?) -> Void) -> String {
71-
return bind(eventName: eventName, eventCallback: { [weak self] (event: PusherEvent) -> Void in
72-
guard let self = self else { return }
73-
// Mimic the old parsing behavior for backwards compatibility
74-
let callbackData: Any?
75-
if self.shouldParseJSONForLegacyCallbacks {
76-
if let data = event.dataToJSONObject() {
77-
// Parsed data
78-
callbackData = data
79-
} else {
80-
// Unparsed string if we couldn't parse
81-
callbackData = event.data
82-
}
83-
} else {
84-
callbackData = event.raw[Constants.JSONKeys.data]
85-
}
86-
callback(callbackData)
87-
})
88-
}
89-
9055
/**
9156
Binds a callback to a given event name, scoped to the PusherChannel the function is
9257
called on

Sources/Models/PusherError.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ open class PusherError: NSObject {
1010
/// The error message.
1111
public let message: String
1212

13-
// The websocket payload which needs to passed to legacy callbacks for backwards compatibility
14-
@nonobjc internal let raw: [String: Any]
15-
1613
@nonobjc internal init?(jsonObject: [String: Any]) {
1714
guard let data = jsonObject[Constants.JSONKeys.data] as? [String: Any] else {
1815
return nil
@@ -24,6 +21,5 @@ open class PusherError: NSObject {
2421

2522
self.code = data[Constants.JSONKeys.code] as? Int
2623
self.message = message
27-
self.raw = jsonObject
2824
}
2925
}

Sources/Models/PusherGlobalChannel.swift

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import Foundation
33
@objcMembers
44
@objc open class GlobalChannel: PusherChannel {
55
open var globalCallbacks: [String: (PusherEvent) -> Void] = [:]
6-
open var globalLegacyCallbacks: [String: (Any?) -> Void] = [:]
76

87
/**
98
Initializes a new GlobalChannel instance
@@ -28,17 +27,6 @@ import Foundation
2827
}
2928
}
3029

31-
/**
32-
Calls the appropriate legacy callbacks for the given event name in the scope of the global channel
33-
34-
- parameter event: The JSON object received from the websocket
35-
*/
36-
internal func handleGlobalEventLegacy(event: [String: Any]) {
37-
for (_, callback) in self.globalLegacyCallbacks {
38-
callback(event)
39-
}
40-
}
41-
4230
/**
4331
Binds a callback to the global channel
4432

@@ -52,34 +40,19 @@ import Foundation
5240
return randomId
5341
}
5442

55-
/**
56-
Binds a callback to the global channel
57-
58-
- parameter callback: The function to call when a message is received
59-
60-
- returns: A unique callbackId that can be used to unbind the callback at a later time
61-
*/
62-
internal func bindLegacy(_ callback: @escaping (Any?) -> Void) -> String {
63-
let randomId = UUID().uuidString
64-
self.globalLegacyCallbacks[randomId] = callback
65-
return randomId
66-
}
67-
6843
/**
6944
Unbinds the callback with the given callbackId from the global channel
7045

7146
- parameter callbackId: The unique callbackId string used to identify which callback to unbind
7247
*/
7348
internal func unbind(callbackId: String) {
7449
globalCallbacks.removeValue(forKey: callbackId)
75-
globalLegacyCallbacks.removeValue(forKey: callbackId)
7650
}
7751

7852
/**
7953
Unbinds all callbacks from the channel
8054
*/
8155
override open func unbindAll() {
8256
globalCallbacks = [:]
83-
globalLegacyCallbacks = [:]
8457
}
8558
}

Sources/PusherSwift.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,6 @@ let CLIENT_NAME = "pusher-websocket-swift"
113113
self.connection.unsubscribeAll()
114114
}
115115

116-
/**
117-
Binds the client's global channel to all events
118-
119-
- parameter callback: The function to call when a new event is received. The callback
120-
receives the event's data payload
121-
122-
- returns: A unique string that can be used to unbind the callback from the client
123-
*/
124-
@available(*, deprecated, message: "This will be removed in v10.0.0. Use 'bind(eventCallback:)' instead.")
125-
@discardableResult open func bind(_ callback: @escaping (Any?) -> Void) -> String {
126-
return self.connection.addLegacyCallbackToGlobalChannel(callback)
127-
}
128-
129116
/**
130117
Binds the client's global channel to all events
131118

Sources/Services/PusherConnection.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,6 @@ import NWWebSocket
300300
return globalChannel.bind(callback)
301301
}
302302

303-
/**
304-
Add legacy callback to the connection's global channel
305-
306-
- parameter callback: The callback to be stored
307-
308-
- returns: A callbackId that can be used to remove the callback from the connection
309-
*/
310-
internal func addLegacyCallbackToGlobalChannel(_ callback: @escaping (Any?) -> Void) -> String {
311-
return globalChannel.bindLegacy(callback)
312-
}
313-
314303
/**
315304
Remove the callback with id of callbackId from the connection's global channel
316305

@@ -565,7 +554,6 @@ import NWWebSocket
565554
open func handleError(error: PusherError) {
566555
resetActivityTimeoutTimer()
567556
self.delegate?.receivedError?(error: error)
568-
self.globalChannel?.handleGlobalEventLegacy(event: error.raw)
569557
}
570558

571559
/**
@@ -633,7 +621,6 @@ import NWWebSocket
633621
*/
634622
private func callGlobalCallbacks(event: PusherEvent) {
635623
globalChannel?.handleGlobalEvent(event: event)
636-
globalChannel?.handleGlobalEventLegacy(event: event.raw)
637624
}
638625

639626
/**

Tests/Integration/AuthenticationTests.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,12 @@ class AuthenticationTests: XCTestCase {
125125
let chan = pusher.subscribe("private-test-channel")
126126
XCTAssertFalse(chan.subscribed, "the channel should not be subscribed")
127127

128-
_ = pusher.bind({ (data: Any?) -> Void in
129-
guard let data = data as? [String: AnyObject],
130-
let eventName = data["event"] as? String,
131-
eventName == "pusher:subscription_error" else {
132-
return
133-
}
134-
135-
XCTAssertEqual("private-test-channel", data["channel"] as? String)
128+
pusher.bind { event in
129+
XCTAssertEqual("pusher:subscription_error", event.eventName)
130+
XCTAssertEqual("private-test-channel", event.channelName)
136131
XCTAssertTrue(Thread.isMainThread)
137132
ex.fulfill()
138-
})
133+
}
139134

140135
pusher.connect()
141136

Tests/Integration/PusherIncomingEventHandlingTests.swift

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class HandlingIncomingEventsTests: XCTestCase {
2222
func testCallbacksOnGlobalChannelShouldBeCalled() {
2323
let ex = expectation(description: "Callback should be called")
2424
_ = pusher.subscribe(channelName: "my-channel")
25-
_ = pusher.bind { (_: Any?) -> Void in
25+
pusher.bind { event in
2626
ex.fulfill()
2727
}
2828

@@ -41,7 +41,7 @@ class HandlingIncomingEventsTests: XCTestCase {
4141
func testCallbacksOnRelevantChannelsShouldBeCalled() {
4242
let ex = expectation(description: "Callback should be called")
4343
let chan = pusher.subscribe("my-channel")
44-
_ = chan.bind(eventName: "test-event") { (_: Any?) -> Void in
44+
chan.bind(eventName: "test-event") { event in
4545
ex.fulfill()
4646
}
4747

@@ -61,11 +61,13 @@ class HandlingIncomingEventsTests: XCTestCase {
6161
let globalEx = expectation(description: "Global callback should be called")
6262
let channelEx = expectation(description: "Channel callback should be called")
6363

64-
let callback = { (data: Any?) -> Void in globalEx.fulfill() }
65-
_ = pusher.bind(callback)
64+
pusher.bind { event in
65+
globalEx.fulfill()
66+
}
6667
let chan = pusher.subscribe("my-channel")
67-
let callbackForChannel = { (data: Any?) -> Void in channelEx.fulfill() }
68-
_ = chan.bind(eventName: "test-event", callback: callbackForChannel)
68+
chan.bind(eventName: "test-event") { event in
69+
channelEx.fulfill()
70+
}
6971

7072
let jsonDict = """
7173
{
@@ -81,12 +83,13 @@ class HandlingIncomingEventsTests: XCTestCase {
8183

8284
func testGlobalCallbackReturnsEventData() {
8385
let ex = expectation(description: "Callback should be called")
84-
let callback = { (data: Any?) -> Void in
85-
XCTAssertEqual(data as! [String: String], ["event": "test-event", "channel": "my-channel", "data": "{\"test\":\"test string\",\"and\":\"another\"}"])
86+
_ = pusher.subscribe("my-channel")
87+
pusher.bind { event in
88+
XCTAssertEqual(event.channelName, "my-channel")
89+
XCTAssertEqual(event.eventName, "test-event")
90+
XCTAssertEqual(event.dataToJSONObject() as! [String: String], ["test": "test string", "and": "another"])
8691
ex.fulfill()
8792
}
88-
_ = pusher.subscribe("my-channel")
89-
_ = pusher.bind(callback)
9093

9194
let jsonDict = """
9295
{
@@ -108,12 +111,13 @@ class HandlingIncomingEventsTests: XCTestCase {
108111
*/
109112
func testGlobalCallbackReturnsEventDataWithoutChannelName() {
110113
let ex = expectation(description: "Callback should be called")
111-
let callback = { (data: Any?) -> Void in
112-
XCTAssertEqual(data as! [String: String], ["event": "test-event", "data": "{\"test\":\"test string\",\"and\":\"another\"}"])
114+
_ = pusher.subscribe("my-channel")
115+
pusher.bind { event in
116+
XCTAssertNil(event.channelName)
117+
XCTAssertEqual(event.eventName, "test-event")
118+
XCTAssertEqual(event.dataToJSONObject() as! [String: String], ["test": "test string", "and": "another"])
113119
ex.fulfill()
114120
}
115-
_ = pusher.subscribe("my-channel")
116-
_ = pusher.bind(callback)
117121

118122
let jsonDict = """
119123
{
@@ -162,14 +166,13 @@ class HandlingIncomingEventsTests: XCTestCase {
162166
func testReturningAJSONObjectToCallbacksIfTheStringCanBeParsed() {
163167
let ex = expectation(description: "Callback should be called")
164168

165-
let callback = { (data: Any?) -> Void in
166-
XCTAssertEqual(data as! [String: String], ["test": "test string", "and": "another"])
169+
let chan = pusher.subscribe("my-channel")
170+
chan.bind(eventName: "test-event") { event in
171+
event.dataToJSONObject() as! [String: String]
172+
XCTAssertEqual(event.dataToJSONObject() as! [String: String], ["test": "test string", "and": "another"])
167173
ex.fulfill()
168174
}
169175

170-
let chan = pusher.subscribe("my-channel")
171-
_ = chan.bind(eventName: "test-event", callback: callback)
172-
173176
let jsonDict = """
174177
{
175178
"event": "test-event",
@@ -185,14 +188,12 @@ class HandlingIncomingEventsTests: XCTestCase {
185188
func testReturningAJSONStringToCallbacksIfTheStringCannotBeParsed() {
186189
let ex = expectation(description: "Callback should be called")
187190

188-
let callback = { (data: Any?) -> Void in
189-
XCTAssertEqual(data as? String, "test")
191+
let chan = pusher.subscribe("my-channel")
192+
chan.bind(eventName: "test-event") { event in
193+
XCTAssertEqual(event.data, "test")
190194
ex.fulfill()
191195
}
192196

193-
let chan = pusher.subscribe("my-channel")
194-
_ = chan.bind(eventName: "test-event", callback: callback)
195-
196197
let jsonDict = """
197198
{
198199
"event": "test-event",
@@ -212,12 +213,11 @@ class HandlingIncomingEventsTests: XCTestCase {
212213
pusher = Pusher(key: key, options: options)
213214
socket.delegate = pusher.connection
214215
pusher.connection.socket = socket
215-
let callback = { (data: Any?) -> Void in
216-
XCTAssertEqual(data as? String, "{\"test\":\"test string\",\"and\":\"another\"}")
216+
let chan = pusher.subscribe("my-channel")
217+
chan.bind(eventName: "test-event") { event in
218+
XCTAssertEqual(event.data, "{\"test\":\"test string\",\"and\":\"another\"}")
217219
ex.fulfill()
218220
}
219-
let chan = pusher.subscribe("my-channel")
220-
_ = chan.bind(eventName: "test-event", callback: callback)
221221

222222
let jsonDict = """
223223
{
@@ -257,7 +257,8 @@ class HandlingIncomingEventsTests: XCTestCase {
257257
func testEventObjectReturnedToChannelCallback() {
258258
let ex = expectation(description: "Callback should be called")
259259

260-
let callback = { (event: PusherEvent) -> Void in
260+
let chan = pusher.subscribe("my-channel")
261+
chan.bind(eventName: "test-event") { event in
261262
XCTAssertEqual(event.eventName, "test-event")
262263
XCTAssertEqual(event.channelName!, "my-channel")
263264
XCTAssertEqual(event.data!, "{\"test\":\"test string\",\"and\":\"another\"}")
@@ -272,8 +273,6 @@ class HandlingIncomingEventsTests: XCTestCase {
272273

273274
ex.fulfill()
274275
}
275-
let chan = pusher.subscribe("my-channel")
276-
_ = chan.bind(eventName: "test-event", eventCallback: callback)
277276

278277
let jsonDict = """
279278
{
@@ -290,7 +289,8 @@ class HandlingIncomingEventsTests: XCTestCase {
290289
func testEventObjectReturnedToGlobalCallback() {
291290
let ex = expectation(description: "Callback should be called")
292291

293-
let callback = { (event: PusherEvent) -> Void in
292+
_ = pusher.subscribe("my-channel")
293+
pusher.bind { event in
294294
XCTAssertEqual(event.eventName, "test-event")
295295
XCTAssertEqual(event.channelName!, "my-channel")
296296
XCTAssertEqual(event.data!, "{\"test\":\"test string\",\"and\":\"another\"}")
@@ -305,8 +305,6 @@ class HandlingIncomingEventsTests: XCTestCase {
305305

306306
ex.fulfill()
307307
}
308-
_ = pusher.subscribe("my-channel")
309-
_ = pusher.bind(eventCallback: callback)
310308

311309
XCTAssertNil(socket.eventGivenToCallback)
312310
let jsonDict = """

0 commit comments

Comments
 (0)