Skip to content

Commit a13770d

Browse files
committed
Better protection to the Pusher Channel subscription count attribute
1 parent e730796 commit a13770d

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

Sources/Models/PusherChannel.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ open class PusherChannel: NSObject {
1515
eventHandlersQueue.async(flags: .barrier) { self.eventHandlersInternal = newValue }
1616
}
1717
}
18+
19+
private var _subscriptionCount: Int? = nil
20+
public var subscriptionCount: Int? {
21+
get { return _subscriptionCount }
22+
}
23+
1824
open var subscribed = false
1925
public let name: String
2026
open weak var connection: PusherConnection?
2127
open var unsentEvents = [QueuedClientEvent]()
2228
public let type: PusherChannelType
2329
public var auth: PusherAuth?
24-
public var subscriptionCount: Int
2530

2631
// Wrap accesses to the decryption key in a serial queue because it will be accessed from multiple threads
2732
@nonobjc private var decryptionKeyQueue = DispatchQueue(label: "com.pusher.pusherswift-channel-decryption-key-\(UUID().uuidString)",
@@ -51,7 +56,12 @@ open class PusherChannel: NSObject {
5156
self.connection = connection
5257
self.auth = auth
5358
self.type = PusherChannelType(name: name)
54-
self.subscriptionCount = 0
59+
}
60+
61+
internal func updateSubscriptionCount(count: Int, event: PusherEvent) {
62+
self._subscriptionCount = count
63+
let subscriptionEvent = event.copy(withEventName: Constants.Events.Pusher.subscriptionCount)
64+
handleEvent(event: subscriptionEvent)
5565
}
5666

5767
/**

Sources/Protocols/PusherDelegate.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ import Foundation
88
@objc optional func failedToSubscribeToChannel(name: String, response: URLResponse?, data: String?, error: NSError?)
99
@objc optional func failedToDecryptEvent(eventName: String, channelName: String, data: String?)
1010
@objc(receivedError:) optional func receivedError(error: PusherError)
11-
@objc optional func subscriptionCountReceived(count: Int)
1211
}

Sources/Services/PusherConnection.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ import NWWebSocket
459459
}
460460
}
461461
}
462+
462463

463464
let subscriptionEvent = event.copy(withEventName: Constants.Events.Pusher.subscriptionSucceeded)
464465
callGlobalCallbacks(event: subscriptionEvent)
@@ -550,6 +551,12 @@ import NWWebSocket
550551
}
551552
}
552553

554+
/**
555+
Handle subscription count event
556+
557+
- parameter event: The event to be processed
558+
*/
559+
553560
private func handleSubscriptionCountEvent(event: PusherEvent) {
554561
guard let channelName = event.channelName,
555562
let channel = self.channels.find(name: channelName),
@@ -558,12 +565,7 @@ import NWWebSocket
558565
return
559566
}
560567

561-
channel.subscriptionCount = count
562-
563-
let subscriptionEvent = event.copy(withEventName: Constants.Events.Pusher.subscriptionCount)
564-
channel.handleEvent(event: subscriptionEvent)
565-
566-
self.delegate?.subscriptionCountReceived?(count: count)
568+
channel.updateSubscriptionCount(count: count, event: event)
567569
}
568570

569571
/**

0 commit comments

Comments
 (0)