Skip to content

Commit

Permalink
Replacing closures names
Browse files Browse the repository at this point in the history
  • Loading branch information
jguz-pubnub committed Feb 15, 2024
1 parent d0ec8a3 commit 2a618a7
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 156 deletions.
56 changes: 28 additions & 28 deletions Sources/PubNub/Events/New/EventEmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import Foundation
/// A protocol for types that emit PubNub status events from the Subscribe loop.
public protocol StatusEmitter: AnyObject {
/// A closure to be called when the connection status changes.
var didReceiveConnectionStatusChange: ((ConnectionStatus) -> Void)? { get set }
var onConnectionStateChange: ((ConnectionStatus) -> Void)? { get set }
/// A closure to be called when a subscription error occurs.
var didReceiveSubscribeError: ((PubNubError) -> Void)? { get set }
var onSubscribeError: ((PubNubError) -> Void)? { get set }
}

// MARK: - EventEmitter
Expand All @@ -31,24 +31,24 @@ public protocol EventEmitter: AnyObject {
/// A unique emitter's identifier
var uuid: UUID { get }
/// Receiver for a single event
var eventStream: ((PubNubEvent) -> Void)? { get set }
/// Receiver for multiple events. This will also emit individual events to `eventStream:`
var eventsStream: (([PubNubEvent]) -> Void)? { get set }
var onEvent: ((PubNubEvent) -> Void)? { get set }
/// Receiver for multiple events. This will also emit individual events to `onEvent:`
var onEvents: (([PubNubEvent]) -> Void)? { get set }
/// Receiver for Message events
var messagesStream: ((PubNubMessage) -> Void)? { get set }
var onMessage: ((PubNubMessage) -> Void)? { get set }
/// Receiver for Signal events
var signalsStream: ((PubNubMessage) -> Void)? { get set }
var onSignal: ((PubNubMessage) -> Void)? { get set }
/// Receiver for Presence events
var presenceStream: ((PubNubPresenceChange) -> Void)? { get set }
var onPresence: ((PubNubPresenceChange) -> Void)? { get set }
/// Receiver for Message Action events
var messageActionsStream: ((PubNubMessageActionEvent) -> Void)? { get set }
var onMessageAction: ((PubNubMessageActionEvent) -> Void)? { get set }
/// Receiver for File Upload events
var filesStream: ((PubNubFileEvent) -> Void)? { get set }
var onFileEvent: ((PubNubFileEvent) -> Void)? { get set }
/// Receiver for App Context events
var appContextStream: ((PubNubAppContextEvent) -> Void)? { get set }
var onAppContext: ((PubNubAppContextEvent) -> Void)? { get set }
}

/// A protocol representing a type that can be used to dispose of subscriptions.
/// A protocol representing a type that can be utilized to dispose of a conforming object.
public protocol SubscriptionDisposable {
/// Determines whether current emitter is disposed
var isDisposed: Bool { get }
Expand All @@ -60,23 +60,23 @@ extension EventEmitter {
func emit(events: [PubNubEvent]) {
queue.async { [weak self] in
if !events.isEmpty {
self?.eventsStream?(events)
self?.onEvents?(events)
}
for event in events {
self?.eventStream?(event)
self?.onEvent?(event)
switch event {
case let .messageReceived(message):
self?.messagesStream?(message)
self?.onMessage?(message)
case let .signalReceived(signal):
self?.signalsStream?(signal)
self?.onSignal?(signal)
case let .presenceChange(presence):
self?.presenceStream?(presence)
self?.onPresence?(presence)
case let .appContextEvent(appContextEvent):
self?.appContextStream?(appContextEvent)
self?.onAppContext?(appContextEvent)
case let .messageActionEvent(messageActionEvent):
self?.messageActionsStream?(messageActionEvent)
self?.onMessageAction?(messageActionEvent)
case let .fileUploadEvent(fileEvent):
self?.filesStream?(fileEvent)
self?.onFileEvent?(fileEvent)
}
}
}
Expand All @@ -85,14 +85,14 @@ extension EventEmitter {

extension EventEmitter {
func clearCallbacks() {
eventStream = nil
eventsStream = nil
messagesStream = nil
signalsStream = nil
presenceStream = nil
messageActionsStream = nil
filesStream = nil
appContextStream = nil
onEvent = nil
onEvents = nil
onMessage = nil
onSignal = nil
onPresence = nil
onMessageAction = nil
onFileEvent = nil
onAppContext = nil
}
}

Expand Down
16 changes: 8 additions & 8 deletions Sources/PubNub/Events/New/Subscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public final class Subscription: EventEmitter, SubscriptionDisposable {
// Stores the timetoken the user subscribed with
private(set) var timetoken: Timetoken?

public var eventStream: ((PubNubEvent) -> Void)?
public var eventsStream: (([PubNubEvent]) -> Void)?
public var messagesStream: ((PubNubMessage) -> Void)?
public var signalsStream: ((PubNubMessage) -> Void)?
public var presenceStream: ((PubNubPresenceChange) -> Void)?
public var messageActionsStream: ((PubNubMessageActionEvent) -> Void)?
public var filesStream: ((PubNubFileEvent) -> Void)?
public var appContextStream: ((PubNubAppContextEvent) -> Void)?
public var onEvent: ((PubNubEvent) -> Void)?
public var onEvents: (([PubNubEvent]) -> Void)?
public var onMessage: ((PubNubMessage) -> Void)?
public var onSignal: ((PubNubMessage) -> Void)?
public var onPresence: ((PubNubPresenceChange) -> Void)?
public var onMessageAction: ((PubNubMessageActionEvent) -> Void)?
public var onFileEvent: ((PubNubFileEvent) -> Void)?
public var onAppContext: ((PubNubAppContextEvent) -> Void)?

// Intercepts messages from the Subscribe loop and forwards them to the current `Subscription`
lazy var adapter = BaseSubscriptionListenerAdapter(
Expand Down
29 changes: 17 additions & 12 deletions Sources/PubNub/Events/New/SubscriptionSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import Foundation
///
/// Use this class to manage multiple `Subscription` concurrently.
public final class SubscriptionSet: EventEmitter, SubscriptionDisposable {
public var eventStream: ((PubNubEvent) -> Void)?
public var eventsStream: (([PubNubEvent]) -> Void)?
public var messagesStream: ((PubNubMessage) -> Void)?
public var signalsStream: ((PubNubMessage) -> Void)?
public var presenceStream: ((PubNubPresenceChange) -> Void)?
public var messageActionsStream: ((PubNubMessageActionEvent) -> Void)?
public var filesStream: ((PubNubFileEvent) -> Void)?
public var appContextStream: ((PubNubAppContextEvent) -> Void)?
public var onEvent: ((PubNubEvent) -> Void)?
public var onEvents: (([PubNubEvent]) -> Void)?
public var onMessage: ((PubNubMessage) -> Void)?
public var onSignal: ((PubNubMessage) -> Void)?
public var onPresence: ((PubNubPresenceChange) -> Void)?
public var onMessageAction: ((PubNubMessageActionEvent) -> Void)?
public var onFileEvent: ((PubNubFileEvent) -> Void)?
public var onAppContext: ((PubNubAppContextEvent) -> Void)?

public let queue: DispatchQueue
/// Additional subscription options
Expand Down Expand Up @@ -67,8 +67,8 @@ public final class SubscriptionSet: EventEmitter, SubscriptionDisposable {
///
/// - Parameters:
/// - queue: The dispatch queue on which the subscription events should be handled
/// - subscriptions: A collection of existing `Subscription` instances to include in the Subscribe loop.
/// - options: Additional subscription options for configuring the subscription set ///
/// - subscriptions: A collection of existing `Subscription` instances to include in the Subscribe loop
/// - options: Additional subscription options
public init(
queue: DispatchQueue = .main,
subscriptions: any Collection<Subscription>,
Expand Down Expand Up @@ -157,8 +157,13 @@ extension SubscriptionSet: SubscribeCapable {
receiver.registerAdapter(adapter)
currentSubscriptions.forEach { receiver.registerAdapter($0.adapter) }

let channels = currentSubscriptions.filter { $0.subscriptionType == .channel }.allObjects
let groups = currentSubscriptions.filter { $0.subscriptionType == .channelGroup }.allObjects
let channels = currentSubscriptions.filter {
$0.subscriptionType == .channel
}.allObjects

let groups = currentSubscriptions.filter {
$0.subscriptionType == .channelGroup
}.allObjects

receiver.internalSubscribe(
with: channels,
Expand Down
101 changes: 30 additions & 71 deletions Sources/PubNub/PubNub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1462,102 +1462,61 @@ extension PubNub: EventEmitter {
public var queue: DispatchQueue {
subscription.queue
}

public var uuid: UUID {
subscription.uuid
}

public var eventStream: ((PubNubEvent) -> Void)? {
get {
subscription.eventStream
}
set {
subscription.eventStream = newValue
}
public var onEvent: ((PubNubEvent) -> Void)? {
get { subscription.onEvent }
set { subscription.onEvent = newValue }
}

public var eventsStream: (([PubNubEvent]) -> Void)? {
get {
subscription.eventsStream
}
set {
subscription.eventsStream = newValue
}
public var onEvents: (([PubNubEvent]) -> Void)? {
get { subscription.onEvents }
set { subscription.onEvents = newValue }
}

public var messagesStream: ((PubNubMessage) -> Void)? {
get {
subscription.messagesStream
}
set {
subscription.messagesStream = newValue
}
public var onMessage: ((PubNubMessage) -> Void)? {
get { subscription.onMessage }
set { subscription.onMessage = newValue }
}

public var signalsStream: ((PubNubMessage) -> Void)? {
get {
subscription.signalsStream
}
set {
subscription.signalsStream = newValue
}
public var onSignal: ((PubNubMessage) -> Void)? {
get { subscription.onSignal }
set { subscription.onSignal = newValue }
}

public var presenceStream: ((PubNubPresenceChange) -> Void)? {
get {
subscription.presenceStream
}
set {
subscription.presenceStream = newValue
}
public var onPresence: ((PubNubPresenceChange) -> Void)? {
get { subscription.onPresence }
set { subscription.onPresence = newValue }
}

public var messageActionsStream: ((PubNubMessageActionEvent) -> Void)? {
get {
subscription.messageActionsStream
}
set {
subscription.messageActionsStream = newValue
}
public var onMessageAction: ((PubNubMessageActionEvent) -> Void)? {
get { subscription.onMessageAction }
set { subscription.onMessageAction = newValue }
}

public var filesStream: ((PubNubFileEvent) -> Void)? {
get {
subscription.filesStream
}
set {
subscription.filesStream = newValue
}
public var onFileEvent: ((PubNubFileEvent) -> Void)? {
get { subscription.onFileEvent }
set { subscription.onFileEvent = newValue }
}

public var appContextStream: ((PubNubAppContextEvent) -> Void)? {
get {
subscription.appContextStream
}
set {
subscription.appContextStream = newValue
}
public var onAppContext: ((PubNubAppContextEvent) -> Void)? {
get { subscription.onAppContext }
set { subscription.onAppContext = newValue }
}
}

/// An extension to the `PubNub` class, making it conform to the `StatusEmitter` protocol and serving
/// as a global listener for connection changes and possible errors along the way.
extension PubNub: StatusEmitter {
public var didReceiveConnectionStatusChange: ((ConnectionStatus) -> Void)? {
get {
subscription.didReceiveConnectionStatusChange
}
set {
subscription.didReceiveConnectionStatusChange = newValue
}
public var onConnectionStateChange: ((ConnectionStatus) -> Void)? {
get { subscription.onConnectionStateChange }
set { subscription.onConnectionStateChange = newValue }
}

public var didReceiveSubscribeError: ((PubNubError) -> Void)? {
get {
subscription.didReceiveSubscribeError
}
set {
subscription.didReceiveSubscribeError = newValue
}
public var onSubscribeError: ((PubNubError) -> Void)? {
get { subscription.onSubscribeError }
set { subscription.onSubscribeError = newValue }
}
}
28 changes: 15 additions & 13 deletions Sources/PubNub/Subscription/SubscriptionSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import Foundation

@available(*, deprecated, message: "Subscribe and unsubscribe using methods from a PubNub object")
public class SubscriptionSession: EventEmitter {
public class SubscriptionSession: EventEmitter, StatusEmitter {
/// A unique identifier for subscription session
public var uuid: UUID {
strategy.uuid
Expand All @@ -31,16 +31,18 @@ public class SubscriptionSession: EventEmitter {
}

/// `EventEmitter` conformance
public var eventStream: ((PubNubEvent) -> Void)?
public var eventsStream: (([PubNubEvent]) -> Void)?
public var messagesStream: ((PubNubMessage) -> Void)?
public var signalsStream: ((PubNubMessage) -> Void)?
public var presenceStream: ((PubNubPresenceChange) -> Void)?
public var messageActionsStream: ((PubNubMessageActionEvent) -> Void)?
public var filesStream: ((PubNubFileEvent) -> Void)?
public var appContextStream: ((PubNubAppContextEvent) -> Void)?
public var didReceiveConnectionStatusChange: ((ConnectionStatus) -> Void)?
public var didReceiveSubscribeError: ((PubNubError) -> Void)?
public var onEvent: ((PubNubEvent) -> Void)?
public var onEvents: (([PubNubEvent]) -> Void)?
public var onMessage: ((PubNubMessage) -> Void)?
public var onSignal: ((PubNubMessage) -> Void)?
public var onPresence: ((PubNubPresenceChange) -> Void)?
public var onMessageAction: ((PubNubMessageActionEvent) -> Void)?
public var onFileEvent: ((PubNubFileEvent) -> Void)?
public var onAppContext: ((PubNubAppContextEvent) -> Void)?

/// `StatusEmitter` conformance
public var onConnectionStateChange: ((ConnectionStatus) -> Void)?
public var onSubscribeError: ((PubNubError) -> Void)?

var previousTokenResponse: SubscribeCursor? {
strategy.previousTokenResponse
Expand Down Expand Up @@ -70,9 +72,9 @@ public class SubscriptionSession: EventEmitter {
statusListener.didReceiveStatus = { [weak self] statusChange in
switch statusChange {
case .success(let newStatus):
self?.didReceiveConnectionStatusChange?(newStatus)
self?.onConnectionStateChange?(newStatus)
case .failure(let error):
self?.didReceiveSubscribeError?(error)
self?.onSubscribeError?(error)
}
}
return statusListener
Expand Down
14 changes: 7 additions & 7 deletions Tests/PubNubTests/Events/New/SubscriptionSetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,30 @@ class SubscriptionSetTests: XCTestCase {
pubnub.channel("c2")
])

subscription.eventsStream = { _ in
subscription.onEvents = { _ in
allEventsExpectation.fulfill()
singleEventExpectation.fulfill()
}
subscription.messagesStream = { _ in
subscription.onMessage = { _ in
messagesExpectation.fulfill()
singleEventExpectation.fulfill()
}
subscription.signalsStream = { _ in
subscription.onSignal = { _ in
signalExpectation.fulfill()
singleEventExpectation.fulfill()
}
subscription.messageActionsStream = { _ in
subscription.onMessageAction = { _ in
messageAction.fulfill()
singleEventExpectation.fulfill()
}
subscription.presenceStream = { _ in
subscription.onPresence = { _ in
presenceChangeExpectation.fulfill()
}
subscription.appContextStream = { _ in
subscription.onAppContext = { _ in
appContextExpectation.fulfill()
singleEventExpectation.fulfill()
}
subscription.filesStream = { _ in
subscription.onFileEvent = { _ in
fileExpectation.fulfill()
singleEventExpectation.fulfill()
}
Expand Down
Loading

0 comments on commit 2a618a7

Please sign in to comment.