Skip to content

Commit e730796

Browse files
committed
Add subscriptionCount delegate function
1 parent dd20929 commit e730796

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

Sources/Models/Constants.swift

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ enum Constants {
2323
static let unsubscribe = "pusher:unsubscribe"
2424
static let subscriptionError = "pusher:subscription_error"
2525
static let subscriptionSucceeded = "pusher:subscription_succeeded"
26+
static let subscriptionCount = "pusher:subscription_count"
2627
}
2728

2829
enum PusherInternal {
2930
static let memberAdded = "pusher_internal:member_added"
3031
static let memberRemoved = "pusher_internal:member_removed"
3132
static let subscriptionSucceeded = "pusher_internal:subscription_succeeded"
33+
static let subscriptionCount = "pusher_internal:subscription_count"
3234
}
3335
}
3436

@@ -39,21 +41,22 @@ enum Constants {
3941
}
4042

4143
enum JSONKeys {
42-
static let activityTimeout = "activity_timeout"
43-
static let auth = "auth"
44-
static let channel = "channel"
45-
static let channelData = "channel_data"
46-
static let ciphertext = "ciphertext"
47-
static let code = "code"
48-
static let data = "data"
49-
static let event = "event"
50-
static let hash = "hash"
51-
static let message = "message"
52-
static let nonce = "nonce"
53-
static let presence = "presence"
54-
static let socketId = "socket_id"
55-
static let sharedSecret = "shared_secret"
56-
static let userId = "user_id"
57-
static let userInfo = "user_info"
44+
static let activityTimeout = "activity_timeout"
45+
static let auth = "auth"
46+
static let channel = "channel"
47+
static let channelData = "channel_data"
48+
static let ciphertext = "ciphertext"
49+
static let code = "code"
50+
static let data = "data"
51+
static let event = "event"
52+
static let hash = "hash"
53+
static let message = "message"
54+
static let nonce = "nonce"
55+
static let presence = "presence"
56+
static let socketId = "socket_id"
57+
static let sharedSecret = "shared_secret"
58+
static let userId = "user_id"
59+
static let userInfo = "user_info"
60+
static let subscriptionCount = "subscription_count"
5861
}
5962
}

Sources/Models/PusherChannel.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ open class PusherChannel: NSObject {
2121
open var unsentEvents = [QueuedClientEvent]()
2222
public let type: PusherChannelType
2323
public var auth: PusherAuth?
24+
public var subscriptionCount: Int
2425

2526
// Wrap accesses to the decryption key in a serial queue because it will be accessed from multiple threads
2627
@nonobjc private var decryptionKeyQueue = DispatchQueue(label: "com.pusher.pusherswift-channel-decryption-key-\(UUID().uuidString)",
@@ -50,6 +51,7 @@ open class PusherChannel: NSObject {
5051
self.connection = connection
5152
self.auth = auth
5253
self.type = PusherChannelType(name: name)
54+
self.subscriptionCount = 0
5355
}
5456

5557
/**

Sources/Protocols/PusherDelegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ 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)
1112
}

Sources/Services/PusherConnection.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,22 @@ import NWWebSocket
549549
Logger.shared.debug(for: .unableToRemoveMemberFromChannel)
550550
}
551551
}
552+
553+
private func handleSubscriptionCountEvent(event: PusherEvent) {
554+
guard let channelName = event.channelName,
555+
let channel = self.channels.find(name: channelName),
556+
let subscriptionCountData = event.dataToJSONObject() as? [String: Any],
557+
let count = subscriptionCountData[Constants.JSONKeys.subscriptionCount] as? Int else {
558+
return
559+
}
560+
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)
567+
}
552568

553569
/**
554570
Handles incoming error
@@ -607,6 +623,9 @@ import NWWebSocket
607623

608624
case Constants.Events.PusherInternal.memberRemoved:
609625
handleMemberRemovedEvent(event: event)
626+
627+
case Constants.Events.PusherInternal.subscriptionCount:
628+
handleSubscriptionCountEvent(event: event)
610629

611630
default:
612631
callGlobalCallbacks(event: event)

0 commit comments

Comments
 (0)