Skip to content

Commit

Permalink
Added protocols Client, ClientSubscription
Browse files Browse the repository at this point in the history
- CentrifugeClient implement Client protocol
- CentrifugeSubscription implement ClientSubscription protocol
- Client protocol adopted to use ClientSubscription instead of CentrifugeSubscription class
  • Loading branch information
shalom-aviv committed Jan 12, 2025
1 parent 2508c0e commit 79fb349
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 4 deletions.
3 changes: 1 addition & 2 deletions Sources/SwiftCentrifuge/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public enum CentrifugeClientState {
case connected
}

/// TODO: protocol
public class CentrifugeClient {
public class CentrifugeClient: Client {
public weak var delegate: CentrifugeClientDelegate?

//MARK -
Expand Down
129 changes: 129 additions & 0 deletions Sources/SwiftCentrifuge/ClientProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//
// Client.swift
// SwiftCentrifuge
//
// Created by shalom-aviv on 12/01/2025.
// Copyright © 2025 shalom-aviv. All rights reserved.
//

import Foundation

public protocol Client: AnyObject {
var delegate: CentrifugeClientDelegate? { get set }

var state: CentrifugeClientState { get }

func connect()

func disconnect()

func resetReconnectState()

func setToken(token: String)

func newSubscription(
channel: String,
delegate: CentrifugeSubscriptionDelegate,
config: CentrifugeSubscriptionConfig?
) throws -> CentrifugeSubscription

func getSubscription(channel: String) -> CentrifugeSubscription?

func removeSubscription(_ sub: CentrifugeSubscription)

func getSubscriptions() -> [String: CentrifugeSubscription]

func send(data: Data, completion: @escaping (Error?) -> Void)

func publish(channel: String, data: Data, completion: @escaping (Result<CentrifugePublishResult, Error>) -> Void)

func rpc(method: String, data: Data, completion: @escaping (Result<CentrifugeRpcResult, Error>) -> Void)

func presence(channel: String, completion: @escaping (Result<CentrifugePresenceResult, Error>) -> Void)

func presenceStats(channel: String, completion: @escaping (Result<CentrifugePresenceStatsResult, Error>) -> Void)

func history(
channel: String,
limit: Int32,
since: CentrifugeStreamPosition?,
reverse: Bool,
completion: @escaping (Result<CentrifugeHistoryResult, Error>) -> Void
)
}

public extension Client {
func newSubscription(channel: String, delegate: CentrifugeSubscriptionDelegate) throws -> CentrifugeSubscription {
try newSubscription(
channel: channel,
delegate: delegate,
config: nil
)
}

func history(
channel: String,
limit: Int32 = 0,
sincePosition: CentrifugeStreamPosition? = nil,
reverse: Bool = false,
completion: @escaping (Result<CentrifugeHistoryResult, Error>) -> Void
) {
history(
channel: channel,
limit: limit,
since: sincePosition,
reverse: reverse,
completion: completion
)
}
}

/// Adopt Client protocol to use protocol ClientSubscription instead of CentrifugeSubscription class
public extension Client {

func newClientSubscription(
channel: String,
delegate: CentrifugeSubscriptionDelegate,
config: CentrifugeSubscriptionConfig?
) throws -> CentrifugeSubscription {
try newSubscription(channel: channel, delegate: delegate, config: config)
}

func newClientSubscription(
channel: String,
delegate: CentrifugeSubscriptionDelegate
) throws -> CentrifugeSubscription {
try newClientSubscription(channel: channel, delegate: delegate, config: nil)
}

func getClientSubscription(channel: String) -> ClientSubscription? {
getSubscription(channel: channel)
}

func removeClientSubscription(_ sub: ClientSubscription) {
guard let sub = sub as? CentrifugeSubscription else { return }

removeSubscription(sub)
}

func getClientSubscription() -> [String: ClientSubscription] {
getSubscriptions()
}

}

public extension CentrifugeClient {
/// Static constructor that create CentrifugeClient and return it as Client protocol
///
/// - Parameters:
/// - url: protobuf URL endpoint of Centrifugo/Centrifuge.
/// - config: config object.
/// - delegate: delegate protocol implementation to react on client events.
static func newClient(endpoint: String, config: CentrifugeClientConfig, delegate: CentrifugeClientDelegate? = nil) -> Client {
CentrifugeClient(
endpoint: endpoint,
config: config,
delegate: delegate
)
}
}
48 changes: 48 additions & 0 deletions Sources/SwiftCentrifuge/ClientSubscriptionProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// ClientSubscription.swift
// SwiftCentrifuge
//
// Created by shalom-aviv on 12/01/2025.
// Copyright © 2025 shalom-aviv. All rights reserved.
//

import Foundation

public protocol ClientSubscription: AnyObject {
var channel: String { get }

var state: CentrifugeSubscriptionState { get set }

func subscribe()

func unsubscribe()

func publish(data: Data, completion: @escaping (Result<CentrifugePublishResult, Error>) -> Void)

func presence(completion: @escaping (Result<CentrifugePresenceResult, Error>) -> Void)

func presenceStats(completion: @escaping (Result<CentrifugePresenceStatsResult, Error>) -> Void)

func history(
limit: Int32,
since: CentrifugeStreamPosition?,
reverse: Bool,
completion: @escaping (Result<CentrifugeHistoryResult, Error>) -> Void
)
}

public extension ClientSubscription {
func history(
limit: Int32 = 0,
sincePosition: CentrifugeStreamPosition? = nil,
reverse: Bool = false,
completion: @escaping (Result<CentrifugeHistoryResult, Error>) -> Void
) {
history(
limit: limit,
since: sincePosition,
reverse: reverse,
completion: completion
)
}
}
3 changes: 1 addition & 2 deletions Sources/SwiftCentrifuge/Subscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public enum CentrifugeSubscriptionState {
case subscribed
}

/// TODO: protocol
public class CentrifugeSubscription {
public class CentrifugeSubscription: ClientSubscription {

public let channel: String

Expand Down

0 comments on commit 79fb349

Please sign in to comment.