From d4f2b0f7544d62ce3217571e7337cc172e177309 Mon Sep 17 00:00:00 2001 From: jguz-pubnub Date: Wed, 14 Aug 2024 11:44:38 +0200 Subject: [PATCH] Minor fixes for Subscriptions --- Sources/PubNub/Events/New/Subscription.swift | 1 - .../PubNub/Events/New/SubscriptionSet.swift | 2 +- .../Subscription/SubscriptionSession.swift | 31 +++++++------------ .../SubscriptionIntegrationTests.swift | 1 - 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/Sources/PubNub/Events/New/Subscription.swift b/Sources/PubNub/Events/New/Subscription.swift index 878649c7..885bc3e2 100644 --- a/Sources/PubNub/Events/New/Subscription.swift +++ b/Sources/PubNub/Events/New/Subscription.swift @@ -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) } diff --git a/Sources/PubNub/Events/New/SubscriptionSet.swift b/Sources/PubNub/Events/New/SubscriptionSet.swift index 749f37fe..36d2dd20 100644 --- a/Sources/PubNub/Events/New/SubscriptionSet.swift +++ b/Sources/PubNub/Events/New/SubscriptionSet.swift @@ -173,7 +173,6 @@ extension SubscriptionSet: SubscribeCapable { return } pubnub.registerAdapter(adapter) - currentSubscriptions.forEach { pubnub.registerAdapter($0.adapter) } let channels = currentSubscriptions.filter { $0.subscriptionType == .channel @@ -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 }, diff --git a/Sources/PubNub/Subscription/SubscriptionSession.swift b/Sources/PubNub/Subscription/SubscriptionSession.swift index d174f162..39acdee1 100644 --- a/Sources/PubNub/Subscription/SubscriptionSession.swift +++ b/Sources/PubNub/Subscription/SubscriptionSession.swift @@ -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() @@ -299,7 +299,8 @@ 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 @@ -307,15 +308,14 @@ extension SubscriptionSession { 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) } } @@ -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 { diff --git a/Tests/PubNubTests/Integration/SubscriptionIntegrationTests.swift b/Tests/PubNubTests/Integration/SubscriptionIntegrationTests.swift index f1a17ec2..c6d74e8e 100644 --- a/Tests/PubNubTests/Integration/SubscriptionIntegrationTests.swift +++ b/Tests/PubNubTests/Integration/SubscriptionIntegrationTests.swift @@ -206,7 +206,6 @@ class SubscriptionIntegrationTests: XCTestCase { firstSubscription = nil secondSubscription = nil pubnub.unsubscribe(from: [self.testChannel]) - subscriptionSet?.unsubscribe() subscriptionSet = nil default: break