Skip to content

Commit 836f502

Browse files
Use an abstract channel
This brings us in line with the AblyPlugin API changes that come in the accompanying ably-cocoa submodule update.
1 parent 22c71cf commit 836f502

3 files changed

Lines changed: 22 additions & 14 deletions

File tree

Sources/AblyLiveObjects/Internal/CoreSDK.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ internal protocol CoreSDK: AnyObject, Sendable {
1313

1414
internal final class DefaultCoreSDK: CoreSDK {
1515
// 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.
16-
private let weakChannel: WeakRef<ARTRealtimeChannel>
16+
private let weakChannel: WeakRef<AblyPlugin.RealtimeChannel>
1717
private let pluginAPI: PluginAPIProtocol
1818

1919
internal init(
20-
channel: ARTRealtimeChannel,
20+
channel: AblyPlugin.RealtimeChannel,
2121
pluginAPI: PluginAPIProtocol
2222
) {
2323
weakChannel = .init(referenced: channel)
@@ -26,7 +26,7 @@ internal final class DefaultCoreSDK: CoreSDK {
2626

2727
// MARK: - Fetching channel
2828

29-
private var channel: ARTRealtimeChannel {
29+
private var channel: AblyPlugin.RealtimeChannel {
3030
guard let channel = weakChannel.referenced else {
3131
// 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.
3232
preconditionFailure("Expected channel to not become deallocated during usage of LiveObjects SDK")

Sources/AblyLiveObjects/Internal/DefaultInternalPlugin.swift

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ internal final class DefaultInternalPlugin: NSObject, AblyPlugin.LiveObjectsInte
2121
///
2222
/// We expect this value to have been previously set by ``prepare(_:)``.
2323
internal static func objectsProperty(for channel: ARTRealtimeChannel, pluginAPI: AblyPlugin.PluginAPIProtocol) -> DefaultRealtimeObjects {
24+
let pluginChannel = pluginAPI.channel(forPublicRealtimeChannel: channel)
25+
return realtimeObjects(for: pluginChannel, pluginAPI: pluginAPI)
26+
}
27+
28+
/// Retrieves the `RealtimeObjects` for this channel.
29+
///
30+
/// We expect this value to have been previously set by ``prepare(_:)``.
31+
private static func realtimeObjects(for channel: AblyPlugin.RealtimeChannel, pluginAPI: AblyPlugin.PluginAPIProtocol) -> DefaultRealtimeObjects {
2432
guard let pluginData = pluginAPI.pluginDataValue(forKey: pluginDataKey, channel: channel) else {
2533
// InternalPlugin.prepare was not called
2634
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
3341
// MARK: - LiveObjectsInternalPluginProtocol
3442

3543
// Populates the channel's `objects` property.
36-
internal func prepare(_ channel: ARTRealtimeChannel) {
44+
internal func prepare(_ channel: AblyPlugin.RealtimeChannel) {
3745
let logger = pluginAPI.logger(for: channel)
3846

3947
logger.log("LiveObjects.DefaultInternalPlugin received prepare(_:)", level: .debug)
@@ -43,8 +51,8 @@ internal final class DefaultInternalPlugin: NSObject, AblyPlugin.LiveObjectsInte
4351
}
4452

4553
/// Retrieves the internally-typed `objects` property for the channel.
46-
private func objectsProperty(for channel: ARTRealtimeChannel) -> DefaultRealtimeObjects {
47-
Self.objectsProperty(for: channel, pluginAPI: pluginAPI)
54+
private func realtimeObjects(for channel: AblyPlugin.RealtimeChannel) -> DefaultRealtimeObjects {
55+
Self.realtimeObjects(for: channel, pluginAPI: pluginAPI)
4856
}
4957

5058
/// A class that wraps an object message.
@@ -94,30 +102,30 @@ internal final class DefaultInternalPlugin: NSObject, AblyPlugin.LiveObjectsInte
94102
return wireObjectMessage.toWireObject.toAblyPluginDataDictionary
95103
}
96104

97-
internal func onChannelAttached(_ channel: ARTRealtimeChannel, hasObjects: Bool) {
98-
objectsProperty(for: channel).onChannelAttached(hasObjects: hasObjects)
105+
internal func onChannelAttached(_ channel: AblyPlugin.RealtimeChannel, hasObjects: Bool) {
106+
realtimeObjects(for: channel).onChannelAttached(hasObjects: hasObjects)
99107
}
100108

101-
internal func handleObjectProtocolMessage(withObjectMessages publicObjectMessages: [any AblyPlugin.ObjectMessageProtocol], channel: ARTRealtimeChannel) {
109+
internal func handleObjectProtocolMessage(withObjectMessages publicObjectMessages: [any AblyPlugin.ObjectMessageProtocol], channel: AblyPlugin.RealtimeChannel) {
102110
guard let inboundObjectMessageBoxes = publicObjectMessages as? [ObjectMessageBox<InboundObjectMessage>] else {
103111
preconditionFailure("Expected to receive the same InboundObjectMessage type as we emit")
104112
}
105113

106114
let objectMessages = inboundObjectMessageBoxes.map(\.objectMessage)
107115

108-
objectsProperty(for: channel).handleObjectProtocolMessage(
116+
realtimeObjects(for: channel).handleObjectProtocolMessage(
109117
objectMessages: objectMessages,
110118
)
111119
}
112120

113-
internal func handleObjectSyncProtocolMessage(withObjectMessages publicObjectMessages: [any AblyPlugin.ObjectMessageProtocol], protocolMessageChannelSerial: String?, channel: ARTRealtimeChannel) {
121+
internal func handleObjectSyncProtocolMessage(withObjectMessages publicObjectMessages: [any AblyPlugin.ObjectMessageProtocol], protocolMessageChannelSerial: String?, channel: AblyPlugin.RealtimeChannel) {
114122
guard let inboundObjectMessageBoxes = publicObjectMessages as? [ObjectMessageBox<InboundObjectMessage>] else {
115123
preconditionFailure("Expected to receive the same InboundObjectMessage type as we emit")
116124
}
117125

118126
let objectMessages = inboundObjectMessageBoxes.map(\.objectMessage)
119127

120-
objectsProperty(for: channel).handleObjectSyncProtocolMessage(
128+
realtimeObjects(for: channel).handleObjectSyncProtocolMessage(
121129
objectMessages: objectMessages,
122130
protocolMessageChannelSerial: protocolMessageChannelSerial,
123131
)
@@ -127,7 +135,7 @@ internal final class DefaultInternalPlugin: NSObject, AblyPlugin.LiveObjectsInte
127135

128136
internal static func sendObject(
129137
objectMessages: [OutboundObjectMessage],
130-
channel: ARTRealtimeChannel,
138+
channel: AblyPlugin.RealtimeChannel,
131139
pluginAPI: PluginAPIProtocol,
132140
) async throws(InternalError) {
133141
let objectMessageBoxes: [ObjectMessageBox<OutboundObjectMessage>] = objectMessages.map { .init(objectMessage: $0) }

0 commit comments

Comments
 (0)