Skip to content

Commit

Permalink
wip switch to using proxy client
Browse files Browse the repository at this point in the history
  • Loading branch information
lawrence-forooghian committed Jan 16, 2025
1 parent fbe6950 commit 79890bf
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
6 changes: 5 additions & 1 deletion Sources/AblyChat/AblyCocoaExtensions/Ably+Dependencies.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import Ably

extension ARTRealtime: RealtimeClientProtocol {}
extension ARTRealtime: SuppliedRealtimeClientProtocol {}

extension ARTRealtimeWrapperSDKProxy: RealtimeClientProtocol {}

extension ARTRealtimeChannels: RealtimeChannelsProtocol {}
extension ARTRealtimeWrapperSDKChannelsProxy: RealtimeChannelsProtocol {}

extension ARTRealtimeChannel: RealtimeChannelProtocol {}
extension ARTRealtimeWrapperSDKChannelProxy: RealtimeChannelProtocol {}

extension ARTRealtimePresence: RealtimePresenceProtocol {}

Expand Down
7 changes: 5 additions & 2 deletions Sources/AblyChat/ChatClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ public actor DefaultChatClient: ChatClient {
* - realtime: The Ably Realtime client.
* - clientOptions: The client options.
*/
public init(realtime: RealtimeClient, clientOptions: ClientOptions?) {
self.realtime = realtime
public init(realtime suppliedRealtime: any SuppliedRealtimeClientProtocol, clientOptions: ClientOptions?) {
self.realtime = suppliedRealtime
self.clientOptions = clientOptions ?? .init()

let realtime = suppliedRealtime.createWrapperSDKProxy(with: .init(agents: agents))

logger = DefaultInternalLogger(logHandler: self.clientOptions.logHandler, logLevel: self.clientOptions.logLevel)
let roomFactory = DefaultRoomFactory()
rooms = DefaultRooms(realtime: realtime, clientOptions: self.clientOptions, logger: logger, roomFactory: roomFactory)
Expand Down
16 changes: 8 additions & 8 deletions Sources/AblyChat/Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import Ably
/// Expresses the requirements of the Ably realtime client that is supplied to the Chat SDK.
///
/// The `ARTRealtime` class from the ably-cocoa SDK implements this protocol.
public protocol SuppliedRealtimeClientProtocol: Sendable, RealtimeClientProtocol {
associatedtype ProxyClient: RealtimeClientProtocol

func createWrapperSDKProxy(with options:ARTWrapperProxyOptions) -> ProxyClient
}

/// Expresses the requirements of the object returned by ``SuppliedRealtimeClientProtocol/createWrapperSDKProxy(with:)``.
public protocol RealtimeClientProtocol: ARTRealtimeProtocol, Sendable {
associatedtype Channels: RealtimeChannelsProtocol
associatedtype Connection: ConnectionProtocol
Expand Down Expand Up @@ -55,17 +62,10 @@ internal struct RealtimeChannelOptions {
}

internal extension RealtimeClientProtocol {
// Function to get the channel with merged options
// Function to get the channel with Chat's default options
func getChannel(_ name: String, opts: RealtimeChannelOptions? = nil) -> any RealtimeChannelProtocol {
var resolvedOptions = opts ?? .init()

// Add in the default params
resolvedOptions.params = (resolvedOptions.params ?? [:]).merging(
defaultChannelParams
) { _, new
in new
}

// CHA-GP2a
resolvedOptions.attachOnSubscribe = false

Expand Down
4 changes: 1 addition & 3 deletions Sources/AblyChat/Version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ import Ably
// Version information
internal let version = "0.1.1"

internal let channelOptionsAgentString = "chat-swift/\(version)"

internal let defaultChannelParams = ["agent": channelOptionsAgentString]
internal let agents = ["chat-swift": version]
6 changes: 5 additions & 1 deletion Tests/AblyChatTests/Mocks/MockRealtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AblyChat
import Foundation

/// A mock implementation of `ARTRealtimeProtocol`. We’ll figure out how to do mocking in tests properly in https://github.com/ably-labs/ably-chat-swift/issues/5.
final class MockRealtime: NSObject, RealtimeClientProtocol, @unchecked Sendable {
final class MockRealtime: NSObject, SuppliedRealtimeClientProtocol, @unchecked Sendable {
let connection: MockConnection
let channels: MockChannels
let paginatedCallback: (@Sendable () -> (ARTHTTPPaginatedResponse?, ARTErrorInfo?))?
Expand Down Expand Up @@ -103,4 +103,8 @@ final class MockRealtime: NSObject, RealtimeClientProtocol, @unchecked Sendable
mutex.unlock()
return result
}

func createWrapperSDKProxy(with options: ARTWrapperProxyOptions) -> some RealtimeClientProtocol {
return self
}
}

0 comments on commit 79890bf

Please sign in to comment.