Skip to content

Commit

Permalink
Minor fixes for Subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jguz-pubnub committed Aug 14, 2024
1 parent dacd7ad commit d4f2b0f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 22 deletions.
1 change: 0 additions & 1 deletion Sources/PubNub/Events/New/Subscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ extension Subscription: SubscribeCapable {
let channels = subscriptionType == .channel ? [self] : []
let channelGroups = subscriptionType == .channelGroup ? [self] : []

pubnub.registerAdapter(adapter)
pubnub.internalSubscribe(with: channels, and: channelGroups, at: timetoken)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/PubNub/Events/New/SubscriptionSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ extension SubscriptionSet: SubscribeCapable {
return
}
pubnub.registerAdapter(adapter)
currentSubscriptions.forEach { pubnub.registerAdapter($0.adapter) }

let channels = currentSubscriptions.filter {
$0.subscriptionType == .channel
Expand All @@ -200,6 +199,7 @@ extension SubscriptionSet: SubscribeCapable {
guard let pubnub = currentSubscriptions.first?.pubnub, !isDisposed else {
return
}
pubnub.subscription.remove(adapter)
pubnub.internalUnsubscribe(
from: currentSubscriptions.filter { $0.subscriptionType == .channel },
and: currentSubscriptions.filter { $0.subscriptionType == .channelGroup },
Expand Down
31 changes: 12 additions & 19 deletions Sources/PubNub/Subscription/SubscriptionSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ class SubscriptionSession: EventListenerInterface, StatusListenerInterface {
withPresence: Bool = false,
using pubnub: PubNub
) {
let channelSubscriptions = channels.compactMap {
let channelSubscriptions = Set(channels).compactMap {
pubnub.channel($0).subscription(
queue: queue,
options: withPresence ? ReceivePresenceEvents() : SubscriptionOptions.empty()
)
}
let channelGroupSubscriptions = groups.compactMap {
let channelGroupSubscriptions = Set(groups).compactMap {
pubnub.channelGroup($0).subscription(
queue: queue,
options: withPresence ? ReceivePresenceEvents() : SubscriptionOptions.empty()
Expand Down Expand Up @@ -299,23 +299,23 @@ extension SubscriptionSession {
)
}

// Returns an array of subscriptions that subscribe to at least one name in common with the given Subscription
// Returns an array of subscriptions, excluding the given subscription and the global listener,
// that subscribe to at least one name in common with the given subscription
func matchingSubscriptions(for subscription: Subscription, presenceOnly: Bool) -> [SubscribeMessagesReceiver] {
let allSubscriptions = strategy.listeners.compactMap {
$0 as? BaseSubscriptionListenerAdapter
}
let namesToFind = subscription.subscriptionNames.filter {
presenceOnly ? $0.isPresenceChannelName : true
}

return allSubscriptions.filter {
$0.uuid != subscription.uuid && $0.uuid != globalEventsListener.uuid
}.compactMap {
$0.receiver
}.filter {
($0.subscriptionTopology[subscription.subscriptionType] ?? [String]()).contains {
namesToFind.contains($0)
return allSubscriptions.compactMap {
if $0.uuid != subscription.uuid && $0.uuid != globalEventsListener.uuid {
return $0.receiver
} else {
return nil
}
}.filter {
!(Set($0.subscriptionTopology[subscription.subscriptionType] ?? []).intersection(namesToFind).isEmpty)
}
}

Expand All @@ -342,14 +342,7 @@ extension SubscriptionSession {
}

let channels = presenceItemsOnly ? [] : Set(subscriptions.filter {
matchingSubscriptions(
for: $0,
presenceOnly: false
).isEmpty &&
matchingSubscriptions(
for: $0,
presenceOnly: true
).isEmpty
matchingSubscriptions(for: $0, presenceOnly: false).isEmpty && matchingSubscriptions(for: $0, presenceOnly: true).isEmpty
}.flatMap {
$0.subscriptionNames
}).symmetricDifference(presenceItems.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ class SubscriptionIntegrationTests: XCTestCase {
firstSubscription = nil
secondSubscription = nil
pubnub.unsubscribe(from: [self.testChannel])
subscriptionSet?.unsubscribe()
subscriptionSet = nil
default:
break
Expand Down

0 comments on commit d4f2b0f

Please sign in to comment.