Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/AblyLiveObjects/Internal/CoreSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<ARTRealtimeChannel>
private let weakChannel: WeakRef<AblyPlugin.RealtimeChannel>
private let pluginAPI: PluginAPIProtocol

internal init(
channel: ARTRealtimeChannel,
channel: AblyPlugin.RealtimeChannel,
pluginAPI: PluginAPIProtocol
) {
weakChannel = .init(referenced: channel)
Expand All @@ -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")
Expand Down
28 changes: 18 additions & 10 deletions Sources/AblyLiveObjects/Internal/DefaultInternalPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]`")
Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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<InboundObjectMessage>] 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<InboundObjectMessage>] 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,
)
Expand All @@ -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<OutboundObjectMessage>] = objectMessages.map { .init(objectMessage: $0) }
Expand Down