diff --git a/Sources/PubNub/Subscription/Strategy/EventEngineSubscriptionSessionStrategy.swift b/Sources/PubNub/Subscription/Strategy/EventEngineSubscriptionSessionStrategy.swift index cba948e2..c7f3c20f 100644 --- a/Sources/PubNub/Subscription/Strategy/EventEngineSubscriptionSessionStrategy.swift +++ b/Sources/PubNub/Subscription/Strategy/EventEngineSubscriptionSessionStrategy.swift @@ -154,43 +154,42 @@ class EventEngineSubscriptionSessionStrategy: SubscriptionSessionStrategy { groups: [PubNubChannel], presenceGroupsOnly: [PubNubChannel] ) { - // Gets the current list of subscribed channels and channel groups + // Retrieve the current list of subscribed channels and channel groups let currentChannelsAndGroups = subscribeEngine.state.input - // Returns the result of updating channel and channel group list + // Provides the outcome after updating the list of channels and channel groups let removingResult = currentChannelsAndGroups.removing( - channels: channels, - presenceChannelsOnly: presenceChannelsOnly, - groups: groups, - presenceGroupsOnly: presenceGroupsOnly + channels: channels,presenceChannelsOnly: presenceChannelsOnly, + groups: groups, presenceGroupsOnly: presenceGroupsOnly ) - let newChannelsAndGroups = removingResult.newInput - if newChannelsAndGroups != currentChannelsAndGroups { - if configuration.maintainPresenceState { - presenceStateContainer.removeState(forChannels: removingResult.removedChannels.map { $0.id }) - } - // Dispatch local event first to guarantee the expected order of events. - // An event indicating unsubscribing from channels and channel groups - // should be emitted before an event related to disconnecting - // from the Subscribe loop, assuming you unsubscribed from all channels - // and channel groups - notify { - $0.emit(subscribe: .subscriptionChanged( - .unsubscribed( - channels: removingResult.removedChannels, - groups: removingResult.removedGroups - )) - ) - } - sendSubscribeEvent(event: .subscriptionChanged( - channels: newChannelsAndGroups.allSubscribedChannelNames, - groups: newChannelsAndGroups.allSubscribedGroupNames - )) - sendPresenceEvent(event: .left( - channels: removingResult.removedChannels.map { $0.id }, - groups: removingResult.removedGroups.map { $0.id } - )) + // Exits if there are no differences for channels or channel groups + guard removingResult.newInput != currentChannelsAndGroups else { + return + } + if configuration.maintainPresenceState { + presenceStateContainer.removeState(forChannels: removingResult.removedChannels.map { $0.id }) + } + // Dispatch local event first to guarantee the expected order of events. + // An event indicating unsubscribing from channels and channel groups + // should be emitted before an event related to disconnecting + // from the Subscribe loop, assuming you unsubscribed from all channels + // and channel groups + notify { + $0.emit(subscribe: .subscriptionChanged( + .unsubscribed( + channels: removingResult.removedChannels, + groups: removingResult.removedGroups + )) + ) } + sendSubscribeEvent(event: .subscriptionChanged( + channels: removingResult.newInput.allSubscribedChannelNames, + groups: removingResult.newInput.allSubscribedGroupNames + )) + sendPresenceEvent(event: .left( + channels: removingResult.removedChannels.map { $0.id }, + groups: removingResult.removedGroups.map { $0.id } + )) } func reconnect(at cursor: SubscribeCursor?) {