From 836f502c976f259760f27f88bfc4d1cecc9257f2 Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Thu, 10 Jul 2025 10:43:02 -0300 Subject: [PATCH] Use an abstract channel This brings us in line with the AblyPlugin API changes that come in the accompanying ably-cocoa submodule update. --- .../AblyLiveObjects/Internal/CoreSDK.swift | 6 ++-- .../Internal/DefaultInternalPlugin.swift | 28 ++++++++++++------- ably-cocoa | 2 +- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Sources/AblyLiveObjects/Internal/CoreSDK.swift b/Sources/AblyLiveObjects/Internal/CoreSDK.swift index e43410e4..9f4f623a 100644 --- a/Sources/AblyLiveObjects/Internal/CoreSDK.swift +++ b/Sources/AblyLiveObjects/Internal/CoreSDK.swift @@ -13,11 +13,11 @@ internal protocol CoreSDK: AnyObject, Sendable { internal final class DefaultCoreSDK: CoreSDK { // We hold a weak reference to the channel so that `DefaultLiveObjects` can hold a strong reference to us without causing a strong reference cycle. We'll revisit this in https://github.com/ably/ably-cocoa-liveobjects-plugin/issues/9. - private let weakChannel: WeakRef + private let weakChannel: WeakRef private let pluginAPI: PluginAPIProtocol internal init( - channel: ARTRealtimeChannel, + channel: AblyPlugin.RealtimeChannel, pluginAPI: PluginAPIProtocol ) { weakChannel = .init(referenced: channel) @@ -26,7 +26,7 @@ internal final class DefaultCoreSDK: CoreSDK { // MARK: - Fetching channel - private var channel: ARTRealtimeChannel { + private var channel: AblyPlugin.RealtimeChannel { guard let channel = weakChannel.referenced else { // It's currently completely possible that the channel _does_ become deallocated during the usage of the LiveObjects SDK; in https://github.com/ably/ably-cocoa-liveobjects-plugin/issues/9 we'll figure out how to prevent this. preconditionFailure("Expected channel to not become deallocated during usage of LiveObjects SDK") diff --git a/Sources/AblyLiveObjects/Internal/DefaultInternalPlugin.swift b/Sources/AblyLiveObjects/Internal/DefaultInternalPlugin.swift index 5cfd4616..caeaf216 100644 --- a/Sources/AblyLiveObjects/Internal/DefaultInternalPlugin.swift +++ b/Sources/AblyLiveObjects/Internal/DefaultInternalPlugin.swift @@ -21,6 +21,14 @@ internal final class DefaultInternalPlugin: NSObject, AblyPlugin.LiveObjectsInte /// /// We expect this value to have been previously set by ``prepare(_:)``. internal static func objectsProperty(for channel: ARTRealtimeChannel, pluginAPI: AblyPlugin.PluginAPIProtocol) -> DefaultRealtimeObjects { + let pluginChannel = pluginAPI.channel(forPublicRealtimeChannel: channel) + return realtimeObjects(for: pluginChannel, pluginAPI: pluginAPI) + } + + /// Retrieves the `RealtimeObjects` for this channel. + /// + /// We expect this value to have been previously set by ``prepare(_:)``. + private static func realtimeObjects(for channel: AblyPlugin.RealtimeChannel, pluginAPI: AblyPlugin.PluginAPIProtocol) -> DefaultRealtimeObjects { guard let pluginData = pluginAPI.pluginDataValue(forKey: pluginDataKey, channel: channel) else { // InternalPlugin.prepare was not called fatalError("To access LiveObjects functionality, you must pass the LiveObjects plugin in the client options when creating the ARTRealtime instance: `clientOptions.plugins = [.liveObjects: AblyLiveObjects.Plugin.self]`") @@ -33,7 +41,7 @@ internal final class DefaultInternalPlugin: NSObject, AblyPlugin.LiveObjectsInte // MARK: - LiveObjectsInternalPluginProtocol // Populates the channel's `objects` property. - internal func prepare(_ channel: ARTRealtimeChannel) { + internal func prepare(_ channel: AblyPlugin.RealtimeChannel) { let logger = pluginAPI.logger(for: channel) logger.log("LiveObjects.DefaultInternalPlugin received prepare(_:)", level: .debug) @@ -43,8 +51,8 @@ internal final class DefaultInternalPlugin: NSObject, AblyPlugin.LiveObjectsInte } /// Retrieves the internally-typed `objects` property for the channel. - private func objectsProperty(for channel: ARTRealtimeChannel) -> DefaultRealtimeObjects { - Self.objectsProperty(for: channel, pluginAPI: pluginAPI) + private func realtimeObjects(for channel: AblyPlugin.RealtimeChannel) -> DefaultRealtimeObjects { + Self.realtimeObjects(for: channel, pluginAPI: pluginAPI) } /// A class that wraps an object message. @@ -94,30 +102,30 @@ internal final class DefaultInternalPlugin: NSObject, AblyPlugin.LiveObjectsInte return wireObjectMessage.toWireObject.toAblyPluginDataDictionary } - internal func onChannelAttached(_ channel: ARTRealtimeChannel, hasObjects: Bool) { - objectsProperty(for: channel).onChannelAttached(hasObjects: hasObjects) + internal func onChannelAttached(_ channel: AblyPlugin.RealtimeChannel, hasObjects: Bool) { + realtimeObjects(for: channel).onChannelAttached(hasObjects: hasObjects) } - internal func handleObjectProtocolMessage(withObjectMessages publicObjectMessages: [any AblyPlugin.ObjectMessageProtocol], channel: ARTRealtimeChannel) { + internal func handleObjectProtocolMessage(withObjectMessages publicObjectMessages: [any AblyPlugin.ObjectMessageProtocol], channel: AblyPlugin.RealtimeChannel) { guard let inboundObjectMessageBoxes = publicObjectMessages as? [ObjectMessageBox] else { preconditionFailure("Expected to receive the same InboundObjectMessage type as we emit") } let objectMessages = inboundObjectMessageBoxes.map(\.objectMessage) - objectsProperty(for: channel).handleObjectProtocolMessage( + realtimeObjects(for: channel).handleObjectProtocolMessage( objectMessages: objectMessages, ) } - internal func handleObjectSyncProtocolMessage(withObjectMessages publicObjectMessages: [any AblyPlugin.ObjectMessageProtocol], protocolMessageChannelSerial: String?, channel: ARTRealtimeChannel) { + internal func handleObjectSyncProtocolMessage(withObjectMessages publicObjectMessages: [any AblyPlugin.ObjectMessageProtocol], protocolMessageChannelSerial: String?, channel: AblyPlugin.RealtimeChannel) { guard let inboundObjectMessageBoxes = publicObjectMessages as? [ObjectMessageBox] else { preconditionFailure("Expected to receive the same InboundObjectMessage type as we emit") } let objectMessages = inboundObjectMessageBoxes.map(\.objectMessage) - objectsProperty(for: channel).handleObjectSyncProtocolMessage( + realtimeObjects(for: channel).handleObjectSyncProtocolMessage( objectMessages: objectMessages, protocolMessageChannelSerial: protocolMessageChannelSerial, ) @@ -127,7 +135,7 @@ internal final class DefaultInternalPlugin: NSObject, AblyPlugin.LiveObjectsInte internal static func sendObject( objectMessages: [OutboundObjectMessage], - channel: ARTRealtimeChannel, + channel: AblyPlugin.RealtimeChannel, pluginAPI: PluginAPIProtocol, ) async throws(InternalError) { let objectMessageBoxes: [ObjectMessageBox] = objectMessages.map { .init(objectMessage: $0) } diff --git a/ably-cocoa b/ably-cocoa index 4a21e87f..291758b8 160000 --- a/ably-cocoa +++ b/ably-cocoa @@ -1 +1 @@ -Subproject commit 4a21e87f988be4741f19d74f13cc4c040eb92121 +Subproject commit 291758b843f80b088136a985f0ef8dd0c5627d05