From 413683e1385fa72da6965d0fbb82308a1de1961f Mon Sep 17 00:00:00 2001 From: jguz-pubnub Date: Mon, 19 Feb 2024 11:12:04 +0100 Subject: [PATCH] PR fixes * Removed onSubscribeError closure * Added associaded type for .connectionError and .disconnectedUnexpectedly --- .../EventEngine/Subscribe/Subscribe.swift | 18 ++++++++++++++++-- .../Subscribe/SubscribeTransition.swift | 4 ++-- Sources/PubNub/Events/New/EventEmitter.swift | 2 -- .../PubNub/Events/New/SubscriptionSet.swift | 4 ++-- Sources/PubNub/PubNub.swift | 5 ----- .../PubNub/Subscription/ConnectionStatus.swift | 4 ++-- .../LegacySubscriptionSessionStrategy.swift | 4 +++- .../Subscription/SubscriptionSession.swift | 6 +----- .../Subscribe/SubscribeTransitionTests.swift | 8 ++++---- 9 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Sources/PubNub/EventEngine/Subscribe/Subscribe.swift b/Sources/PubNub/EventEngine/Subscribe/Subscribe.swift index c65d7408..62ea5499 100644 --- a/Sources/PubNub/EventEngine/Subscribe/Subscribe.swift +++ b/Sources/PubNub/EventEngine/Subscribe/Subscribe.swift @@ -56,7 +56,14 @@ extension Subscribe { let input: SubscribeInput let cursor: SubscribeCursor let error: PubNubError - let connectionStatus = ConnectionStatus.connectionError + let connectionStatus: ConnectionStatus + + init(input: SubscribeInput, cursor: SubscribeCursor, error: PubNubError) { + self.input = input + self.cursor = cursor + self.error = error + self.connectionStatus = .connectionError(error) + } } struct ReceivingState: SubscribeState { @@ -83,7 +90,14 @@ extension Subscribe { let input: SubscribeInput let cursor: SubscribeCursor let error: PubNubError - let connectionStatus = ConnectionStatus.disconnectedUnexpectedly + let connectionStatus: ConnectionStatus + + init(input: SubscribeInput, cursor: SubscribeCursor, error: PubNubError) { + self.input = input + self.cursor = cursor + self.error = error + self.connectionStatus = .disconnectedUnexpectedly(error) + } } struct UnsubscribedState: SubscribeState { diff --git a/Sources/PubNub/EventEngine/Subscribe/SubscribeTransition.swift b/Sources/PubNub/EventEngine/Subscribe/SubscribeTransition.swift index 3c2401df..10af338b 100644 --- a/Sources/PubNub/EventEngine/Subscribe/SubscribeTransition.swift +++ b/Sources/PubNub/EventEngine/Subscribe/SubscribeTransition.swift @@ -258,7 +258,7 @@ fileprivate extension SubscribeTransition { ), invocations: [ .regular(.emitStatus(change: Subscribe.ConnectionStatusChange( oldStatus: state.connectionStatus, - newStatus: .connectionError, + newStatus: .connectionError(error), error: error ))) ] @@ -329,7 +329,7 @@ fileprivate extension SubscribeTransition { ), invocations: [ .regular(.emitStatus(change: Subscribe.ConnectionStatusChange( oldStatus: state.connectionStatus, - newStatus: .disconnectedUnexpectedly, + newStatus: .disconnectedUnexpectedly(error), error: error ))) ] diff --git a/Sources/PubNub/Events/New/EventEmitter.swift b/Sources/PubNub/Events/New/EventEmitter.swift index 90704332..4dd7987e 100644 --- a/Sources/PubNub/Events/New/EventEmitter.swift +++ b/Sources/PubNub/Events/New/EventEmitter.swift @@ -16,8 +16,6 @@ import Foundation public protocol StatusEmitter: AnyObject { /// A closure to be called when the connection status changes. var onConnectionStateChange: ((ConnectionStatus) -> Void)? { get set } - /// A closure to be called when a subscription error occurs. - var onSubscribeError: ((PubNubError) -> Void)? { get set } } // MARK: - EventEmitter diff --git a/Sources/PubNub/Events/New/SubscriptionSet.swift b/Sources/PubNub/Events/New/SubscriptionSet.swift index ae728df2..ccb29c26 100644 --- a/Sources/PubNub/Events/New/SubscriptionSet.swift +++ b/Sources/PubNub/Events/New/SubscriptionSet.swift @@ -49,7 +49,7 @@ public final class SubscriptionSet: EventEmitter, SubscriptionDisposable { /// - options: Additional subscription options public init( queue: DispatchQueue = .main, - entities: any Collection, + entities: any Collection = [], options: SubscriptionOptions = SubscriptionOptions.empty() ) { self.queue = queue @@ -71,7 +71,7 @@ public final class SubscriptionSet: EventEmitter, SubscriptionDisposable { /// - options: Additional subscription options public init( queue: DispatchQueue = .main, - subscriptions: any Collection, + subscriptions: any Collection = [], options: SubscriptionOptions = SubscriptionOptions.empty() ) { self.queue = queue diff --git a/Sources/PubNub/PubNub.swift b/Sources/PubNub/PubNub.swift index dd653ca6..c8c01483 100644 --- a/Sources/PubNub/PubNub.swift +++ b/Sources/PubNub/PubNub.swift @@ -1514,9 +1514,4 @@ extension PubNub: StatusEmitter { get { subscription.onConnectionStateChange } set { subscription.onConnectionStateChange = newValue } } - - public var onSubscribeError: ((PubNubError) -> Void)? { - get { subscription.onSubscribeError } - set { subscription.onSubscribeError = newValue } - } } diff --git a/Sources/PubNub/Subscription/ConnectionStatus.swift b/Sources/PubNub/Subscription/ConnectionStatus.swift index 826f6c12..53128af4 100644 --- a/Sources/PubNub/Subscription/ConnectionStatus.swift +++ b/Sources/PubNub/Subscription/ConnectionStatus.swift @@ -23,9 +23,9 @@ public enum ConnectionStatus: Equatable { @available(*, deprecated, message: "This case will be removed in future versions") case reconnecting /// Unexpected disconnect from a remote system - case disconnectedUnexpectedly + case disconnectedUnexpectedly(PubNubError) /// Unable to establish initial connection. Applies if `enableEventEngine` in `PubNubConfiguration` is true. - case connectionError + case connectionError(PubNubError) /// If the connection is connected or attempting to connect public var isActive: Bool { diff --git a/Sources/PubNub/Subscription/Strategy/LegacySubscriptionSessionStrategy.swift b/Sources/PubNub/Subscription/Strategy/LegacySubscriptionSessionStrategy.swift index e4af884f..a649627b 100644 --- a/Sources/PubNub/Subscription/Strategy/LegacySubscriptionSessionStrategy.swift +++ b/Sources/PubNub/Subscription/Strategy/LegacySubscriptionSessionStrategy.swift @@ -279,7 +279,9 @@ class LegacySubscriptionSessionStrategy: SubscriptionSessionStrategy { // Repeat the request self?.performSubscribeLoop(at: cursor) } else { - self?.connectionStatus = .disconnectedUnexpectedly + self?.connectionStatus = .disconnectedUnexpectedly( + error.pubNubError ?? PubNubError(.unknown, underlying: error) + ) } } } diff --git a/Sources/PubNub/Subscription/SubscriptionSession.swift b/Sources/PubNub/Subscription/SubscriptionSession.swift index 4003da87..27f8c4fc 100644 --- a/Sources/PubNub/Subscription/SubscriptionSession.swift +++ b/Sources/PubNub/Subscription/SubscriptionSession.swift @@ -42,7 +42,6 @@ public class SubscriptionSession: EventEmitter, StatusEmitter { /// `StatusEmitter` conformance public var onConnectionStateChange: ((ConnectionStatus) -> Void)? - public var onSubscribeError: ((PubNubError) -> Void)? var previousTokenResponse: SubscribeCursor? { strategy.previousTokenResponse @@ -70,11 +69,8 @@ public class SubscriptionSession: EventEmitter, StatusEmitter { // Detects status changes and forwards events to the current instance // representing the Subscribe loop's status emitter statusListener.didReceiveStatus = { [weak self] statusChange in - switch statusChange { - case .success(let newStatus): + if case .success(let newStatus) = statusChange { self?.onConnectionStateChange?(newStatus) - case .failure(let error): - self?.onSubscribeError?(error) } } return statusListener diff --git a/Tests/PubNubTests/EventEngine/Subscribe/SubscribeTransitionTests.swift b/Tests/PubNubTests/EventEngine/Subscribe/SubscribeTransitionTests.swift index d3735da7..29462cd4 100644 --- a/Tests/PubNubTests/EventEngine/Subscribe/SubscribeTransitionTests.swift +++ b/Tests/PubNubTests/EventEngine/Subscribe/SubscribeTransitionTests.swift @@ -873,7 +873,7 @@ class SubscribeTransitionTests: XCTestCase { .cancel(.handshakeReconnect), .regular(.emitStatus(change: Subscribe.ConnectionStatusChange( oldStatus: .connecting, - newStatus: .connectionError, + newStatus: .connectionError(PubNubError(.unknown)), error: PubNubError(.unknown) ))) ] @@ -902,7 +902,7 @@ class SubscribeTransitionTests: XCTestCase { .cancel(.receiveReconnect), .regular(.emitStatus(change: Subscribe.ConnectionStatusChange( oldStatus: .connected, - newStatus: .disconnectedUnexpectedly, + newStatus: .disconnectedUnexpectedly(PubNubError(.unknown)), error: PubNubError(.unknown) ))) ] @@ -1262,7 +1262,7 @@ class SubscribeTransitionTests: XCTestCase { ) let expectedInvocations: [EffectInvocation] = [ .regular(.emitStatus(change: Subscribe.ConnectionStatusChange( - oldStatus: .connectionError, + oldStatus: .connectionError(PubNubError(.badRequest)), newStatus: .disconnected, error: nil ))) @@ -1348,7 +1348,7 @@ class SubscribeTransitionTests: XCTestCase { ) let expectedInvocations: [EffectInvocation] = [ .regular(.emitStatus(change: Subscribe.ConnectionStatusChange( - oldStatus: .disconnectedUnexpectedly, + oldStatus: .disconnectedUnexpectedly(PubNubError(.badRequest)), newStatus: .disconnected, error: nil )))