Skip to content

Commit a57357e

Browse files
committed
Create a new handler for subscription count event
1 parent 46c12f2 commit a57357e

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

Sources/Models/PusherChannel.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ open class PusherChannel: NSObject {
2727
open var unsentEvents = [QueuedClientEvent]()
2828
public let type: PusherChannelType
2929
public var auth: PusherAuth?
30+
31+
private let onSubscriptionCountChanged: ((Int) -> Void)?
3032

3133
// Wrap accesses to the decryption key in a serial queue because it will be accessed from multiple threads
3234
@nonobjc private var decryptionKeyQueue = DispatchQueue(label: "com.pusher.pusherswift-channel-decryption-key-\(UUID().uuidString)",
@@ -51,17 +53,17 @@ open class PusherChannel: NSObject {
5153

5254
- returns: A new PusherChannel instance
5355
*/
54-
public init(name: String, connection: PusherConnection, auth: PusherAuth? = nil) {
56+
public init(name: String, connection: PusherConnection, auth: PusherAuth? = nil, onSubscriptionCountChanged: ((Int) -> Void)? = nil) {
5557
self.name = name
5658
self.connection = connection
5759
self.auth = auth
5860
self.type = PusherChannelType(name: name)
61+
self.onSubscriptionCountChanged = onSubscriptionCountChanged
5962
}
6063

61-
internal func updateSubscriptionCount(count: Int, event: PusherEvent) {
64+
internal func updateSubscriptionCount(count: Int) {
6265
self._subscriptionCount = count
63-
let subscriptionEvent = event.copy(withEventName: Constants.Events.Pusher.subscriptionCount)
64-
handleEvent(event: subscriptionEvent)
66+
self.onSubscriptionCountChanged?(count)
6567
}
6668

6769
/**

Sources/Models/PusherChannels.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import Foundation
3636
connection: PusherConnection,
3737
auth: PusherAuth? = nil,
3838
onMemberAdded: ((PusherPresenceChannelMember) -> Void)? = nil,
39-
onMemberRemoved: ((PusherPresenceChannelMember) -> Void)? = nil
39+
onMemberRemoved: ((PusherPresenceChannelMember) -> Void)? = nil,
40+
onSubscriptionCountChanged: ((Int) -> Void)? = nil
4041
) -> PusherChannel {
4142
if let channel = self.channels[name] {
4243
return channel
@@ -51,7 +52,10 @@ import Foundation
5152
onMemberRemoved: onMemberRemoved
5253
)
5354
} else {
54-
newChannel = PusherChannel(name: name, connection: connection, auth: auth)
55+
newChannel = PusherChannel(name: name,
56+
connection: connection,
57+
auth: auth,
58+
onSubscriptionCountChanged: onSubscriptionCountChanged)
5559
}
5660
self.channels[name] = newChannel
5761
return newChannel

Sources/ObjC/Pusher+ObjectiveC.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ import Foundation
88
func subscribe(
99
channelName: String,
1010
onMemberAdded: ((PusherPresenceChannelMember) -> Void)? = nil,
11-
onMemberRemoved: ((PusherPresenceChannelMember) -> Void)? = nil
11+
onMemberRemoved: ((PusherPresenceChannelMember) -> Void)? = nil,
12+
onSubscriptionCountChanged: ((Int) -> Void)? = nil
1213
) -> PusherChannel {
13-
return self.subscribe(channelName, auth: nil, onMemberAdded: onMemberAdded, onMemberRemoved: onMemberRemoved)
14+
return self.subscribe(channelName,
15+
auth: nil,
16+
onMemberAdded: onMemberAdded,
17+
onMemberRemoved: onMemberRemoved,
18+
onSubscriptionCountChanged: onSubscriptionCountChanged)
1419
}
1520

1621
func subscribeToPresenceChannel(channelName: String) -> PusherPresenceChannel {

Sources/PusherSwift.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ let CLIENT_NAME = "pusher-websocket-swift"
5151
_ channelName: String,
5252
auth: PusherAuth? = nil,
5353
onMemberAdded: ((PusherPresenceChannelMember) -> Void)? = nil,
54-
onMemberRemoved: ((PusherPresenceChannelMember) -> Void)? = nil
54+
onMemberRemoved: ((PusherPresenceChannelMember) -> Void)? = nil,
55+
onSubscriptionCountChanged: ((Int) -> Void)? = nil
5556
) -> PusherChannel {
5657

5758
let isEncryptedChannel = PusherChannel.isEncrypted(name: channelName)
@@ -64,7 +65,8 @@ let CLIENT_NAME = "pusher-websocket-swift"
6465
channelName: channelName,
6566
auth: auth,
6667
onMemberAdded: onMemberAdded,
67-
onMemberRemoved: onMemberRemoved
68+
onMemberRemoved: onMemberRemoved,
69+
onSubscriptionCountChanged: onSubscriptionCountChanged
6870
)
6971
}
7072

Sources/Services/PusherConnection.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,16 @@ import NWWebSocket
104104
channelName: String,
105105
auth: PusherAuth? = nil,
106106
onMemberAdded: ((PusherPresenceChannelMember) -> Void)? = nil,
107-
onMemberRemoved: ((PusherPresenceChannelMember) -> Void)? = nil
107+
onMemberRemoved: ((PusherPresenceChannelMember) -> Void)? = nil,
108+
onSubscriptionCountChanged: ((Int) -> Void)? = nil
108109
) -> PusherChannel {
109110
let newChannel = channels.add(
110111
name: channelName,
111112
connection: self,
112113
auth: auth,
113114
onMemberAdded: onMemberAdded,
114-
onMemberRemoved: onMemberRemoved
115+
onMemberRemoved: onMemberRemoved,
116+
onSubscriptionCountChanged: onSubscriptionCountChanged
115117
)
116118

117119
guard self.connectionState == .connected else { return newChannel }
@@ -564,7 +566,7 @@ import NWWebSocket
564566
return
565567
}
566568

567-
channel.updateSubscriptionCount(count: count, event: event)
569+
channel.updateSubscriptionCount(count: count)
568570
}
569571

570572
/**

0 commit comments

Comments
 (0)