Skip to content

Commit

Permalink
PR fixes
Browse files Browse the repository at this point in the history
* Removed onSubscribeError closure
* Added associaded type for .connectionError and .disconnectedUnexpectedly
  • Loading branch information
jguz-pubnub committed Feb 19, 2024
1 parent bdbcaf5 commit 413683e
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 25 deletions.
18 changes: 16 additions & 2 deletions Sources/PubNub/EventEngine/Subscribe/Subscribe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ fileprivate extension SubscribeTransition {
), invocations: [
.regular(.emitStatus(change: Subscribe.ConnectionStatusChange(
oldStatus: state.connectionStatus,
newStatus: .connectionError,
newStatus: .connectionError(error),
error: error
)))
]
Expand Down Expand Up @@ -329,7 +329,7 @@ fileprivate extension SubscribeTransition {
), invocations: [
.regular(.emitStatus(change: Subscribe.ConnectionStatusChange(
oldStatus: state.connectionStatus,
newStatus: .disconnectedUnexpectedly,
newStatus: .disconnectedUnexpectedly(error),
error: error
)))
]
Expand Down
2 changes: 0 additions & 2 deletions Sources/PubNub/Events/New/EventEmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Sources/PubNub/Events/New/SubscriptionSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final class SubscriptionSet: EventEmitter, SubscriptionDisposable {
/// - options: Additional subscription options
public init(
queue: DispatchQueue = .main,
entities: any Collection<Subscribable>,
entities: any Collection<Subscribable> = [],
options: SubscriptionOptions = SubscriptionOptions.empty()
) {
self.queue = queue
Expand All @@ -71,7 +71,7 @@ public final class SubscriptionSet: EventEmitter, SubscriptionDisposable {
/// - options: Additional subscription options
public init(
queue: DispatchQueue = .main,
subscriptions: any Collection<Subscription>,
subscriptions: any Collection<Subscription> = [],
options: SubscriptionOptions = SubscriptionOptions.empty()
) {
self.queue = queue
Expand Down
5 changes: 0 additions & 5 deletions Sources/PubNub/PubNub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}
4 changes: 2 additions & 2 deletions Sources/PubNub/Subscription/ConnectionStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions Sources/PubNub/Subscription/SubscriptionSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)))
]
Expand Down Expand Up @@ -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)
)))
]
Expand Down Expand Up @@ -1262,7 +1262,7 @@ class SubscribeTransitionTests: XCTestCase {
)
let expectedInvocations: [EffectInvocation<Subscribe.Invocation>] = [
.regular(.emitStatus(change: Subscribe.ConnectionStatusChange(
oldStatus: .connectionError,
oldStatus: .connectionError(PubNubError(.badRequest)),
newStatus: .disconnected,
error: nil
)))
Expand Down Expand Up @@ -1348,7 +1348,7 @@ class SubscribeTransitionTests: XCTestCase {
)
let expectedInvocations: [EffectInvocation<Subscribe.Invocation>] = [
.regular(.emitStatus(change: Subscribe.ConnectionStatusChange(
oldStatus: .disconnectedUnexpectedly,
oldStatus: .disconnectedUnexpectedly(PubNubError(.badRequest)),
newStatus: .disconnected,
error: nil
)))
Expand Down

0 comments on commit 413683e

Please sign in to comment.