Skip to content

Commit 5fb6301

Browse files
Merge pull request #88 from ably/39-less-passing-LiveMap-delegate
[ECO-5495] Directly use `ObjectsPool` inside `LiveMap` getter methods
2 parents 5534d4f + 96799cb commit 5fb6301

File tree

9 files changed

+124
-97
lines changed

9 files changed

+124
-97
lines changed

Sources/AblyLiveObjects/Internal/InternalDefaultLiveMap.swift

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ internal import _AblyPluginSupportPrivate
22
import Ably
33

44
/// Protocol for accessing objects from the ObjectsPool. This is used by a LiveMap when it needs to return an object given an object ID.
5-
internal protocol LiveMapObjectPoolDelegate: AnyObject, Sendable {
6-
/// Fetches an object from the pool by its ID
7-
func nosync_getObjectFromPool(id: String) -> ObjectsPool.Entry?
5+
internal protocol LiveMapObjectsPoolDelegate: AnyObject, Sendable {
6+
/// A snapshot of the objects pool.
7+
var nosync_objectsPool: ObjectsPool { get }
88
}
99

1010
/// This provides the implementation behind ``PublicDefaultLiveMap``, via internal versions of the ``LiveMap`` API.
@@ -121,30 +121,40 @@ internal final class InternalDefaultLiveMap: Sendable {
121121
// MARK: - Internal methods that back LiveMap conformance
122122

123123
/// Returns the value associated with a given key, following RTLM5d specification.
124-
internal func get(key: String, coreSDK: CoreSDK, delegate: LiveMapObjectPoolDelegate) throws(ARTErrorInfo) -> InternalLiveMapValue? {
124+
internal func get(key: String, coreSDK: CoreSDK, delegate: LiveMapObjectsPoolDelegate) throws(ARTErrorInfo) -> InternalLiveMapValue? {
125125
try mutableStateMutex.withSync { mutableState throws(ARTErrorInfo) in
126-
try mutableState.nosync_get(key: key, coreSDK: coreSDK, delegate: delegate)
126+
try mutableState.nosync_get(
127+
key: key,
128+
coreSDK: coreSDK,
129+
objectsPool: delegate.nosync_objectsPool,
130+
)
127131
}
128132
}
129133

130-
internal func size(coreSDK: CoreSDK, delegate: LiveMapObjectPoolDelegate) throws(ARTErrorInfo) -> Int {
134+
internal func size(coreSDK: CoreSDK, delegate: LiveMapObjectsPoolDelegate) throws(ARTErrorInfo) -> Int {
131135
try mutableStateMutex.withSync { mutableState throws(ARTErrorInfo) in
132-
try mutableState.nosync_size(coreSDK: coreSDK, delegate: delegate)
136+
try mutableState.nosync_size(
137+
coreSDK: coreSDK,
138+
objectsPool: delegate.nosync_objectsPool,
139+
)
133140
}
134141
}
135142

136-
internal func entries(coreSDK: CoreSDK, delegate: LiveMapObjectPoolDelegate) throws(ARTErrorInfo) -> [(key: String, value: InternalLiveMapValue)] {
143+
internal func entries(coreSDK: CoreSDK, delegate: LiveMapObjectsPoolDelegate) throws(ARTErrorInfo) -> [(key: String, value: InternalLiveMapValue)] {
137144
try mutableStateMutex.withSync { mutableState throws(ARTErrorInfo) in
138-
try mutableState.nosync_entries(coreSDK: coreSDK, delegate: delegate)
145+
try mutableState.nosync_entries(
146+
coreSDK: coreSDK,
147+
objectsPool: delegate.nosync_objectsPool,
148+
)
139149
}
140150
}
141151

142-
internal func keys(coreSDK: CoreSDK, delegate: LiveMapObjectPoolDelegate) throws(ARTErrorInfo) -> [String] {
152+
internal func keys(coreSDK: CoreSDK, delegate: LiveMapObjectsPoolDelegate) throws(ARTErrorInfo) -> [String] {
143153
// RTLM12b: Identical to LiveMap#entries, except that it returns only the keys from the internal data map
144154
try entries(coreSDK: coreSDK, delegate: delegate).map(\.key)
145155
}
146156

147-
internal func values(coreSDK: CoreSDK, delegate: LiveMapObjectPoolDelegate) throws(ARTErrorInfo) -> [InternalLiveMapValue] {
157+
internal func values(coreSDK: CoreSDK, delegate: LiveMapObjectsPoolDelegate) throws(ARTErrorInfo) -> [InternalLiveMapValue] {
148158
// RTLM13b: Identical to LiveMap#entries, except that it returns only the values from the internal data map
149159
try entries(coreSDK: coreSDK, delegate: delegate).map(\.value)
150160
}
@@ -859,7 +869,7 @@ internal final class InternalDefaultLiveMap: Sendable {
859869
}
860870

861871
/// Returns the value associated with a given key, following RTLM5d specification.
862-
internal func nosync_get(key: String, coreSDK: CoreSDK, delegate: LiveMapObjectPoolDelegate) throws(ARTErrorInfo) -> InternalLiveMapValue? {
872+
internal func nosync_get(key: String, coreSDK: CoreSDK, objectsPool: ObjectsPool) throws(ARTErrorInfo) -> InternalLiveMapValue? {
863873
// RTLM5c: If the channel is in the DETACHED or FAILED state, the library should indicate an error with code 90001
864874
try coreSDK.nosync_validateChannelState(notIn: [.detached, .failed], operationDescription: "LiveMap.get")
865875

@@ -874,30 +884,30 @@ internal final class InternalDefaultLiveMap: Sendable {
874884
}
875885

876886
// RTLM5d2: If a ObjectsMapEntry exists at the key, convert it using the shared logic
877-
return nosync_convertEntryToLiveMapValue(entry, delegate: delegate)
887+
return nosync_convertEntryToLiveMapValue(entry, objectsPool: objectsPool)
878888
}
879889

880-
internal func nosync_size(coreSDK: CoreSDK, delegate: LiveMapObjectPoolDelegate) throws(ARTErrorInfo) -> Int {
890+
internal func nosync_size(coreSDK: CoreSDK, objectsPool: ObjectsPool) throws(ARTErrorInfo) -> Int {
881891
// RTLM10c: If the channel is in the DETACHED or FAILED state, the library should throw an ErrorInfo error with statusCode 400 and code 90001
882892
try coreSDK.nosync_validateChannelState(notIn: [.detached, .failed], operationDescription: "LiveMap.size")
883893

884894
// RTLM10d: Returns the number of non-tombstoned entries (per RTLM14) in the internal data map
885895
return data.values.count { entry in
886-
!Self.nosync_isEntryTombstoned(entry, delegate: delegate)
896+
!Self.nosync_isEntryTombstoned(entry, objectsPool: objectsPool)
887897
}
888898
}
889899

890-
internal func nosync_entries(coreSDK: CoreSDK, delegate: LiveMapObjectPoolDelegate) throws(ARTErrorInfo) -> [(key: String, value: InternalLiveMapValue)] {
900+
internal func nosync_entries(coreSDK: CoreSDK, objectsPool: ObjectsPool) throws(ARTErrorInfo) -> [(key: String, value: InternalLiveMapValue)] {
891901
// RTLM11c: If the channel is in the DETACHED or FAILED state, the library should throw an ErrorInfo error with statusCode 400 and code 90001
892902
try coreSDK.nosync_validateChannelState(notIn: [.detached, .failed], operationDescription: "LiveMap.entries")
893903

894904
// RTLM11d: Returns key-value pairs from the internal data map
895905
// RTLM11d1: Pairs with tombstoned entries (per RTLM14) are not returned
896906
var result: [(key: String, value: InternalLiveMapValue)] = []
897907

898-
for (key, entry) in data where !Self.nosync_isEntryTombstoned(entry, delegate: delegate) {
908+
for (key, entry) in data where !Self.nosync_isEntryTombstoned(entry, objectsPool: objectsPool) {
899909
// Convert entry to LiveMapValue using the same logic as get(key:)
900-
if let value = nosync_convertEntryToLiveMapValue(entry, delegate: delegate) {
910+
if let value = nosync_convertEntryToLiveMapValue(entry, objectsPool: objectsPool) {
901911
result.append((key: key, value: value))
902912
}
903913
}
@@ -908,15 +918,15 @@ internal final class InternalDefaultLiveMap: Sendable {
908918
// MARK: - Helper Methods
909919

910920
/// Returns whether a map entry should be considered tombstoned, per the check described in RTLM14.
911-
private static func nosync_isEntryTombstoned(_ entry: InternalObjectsMapEntry, delegate: LiveMapObjectPoolDelegate) -> Bool {
921+
private static func nosync_isEntryTombstoned(_ entry: InternalObjectsMapEntry, objectsPool: ObjectsPool) -> Bool {
912922
// RTLM14a
913923
if entry.tombstone {
914924
return true
915925
}
916926

917927
// RTLM14c
918928
if let objectId = entry.data?.objectId {
919-
if let poolEntry = delegate.nosync_getObjectFromPool(id: objectId), poolEntry.nosync_isTombstone {
929+
if let poolEntry = objectsPool.entries[objectId], poolEntry.nosync_isTombstone {
920930
return true
921931
}
922932
}
@@ -927,7 +937,7 @@ internal final class InternalDefaultLiveMap: Sendable {
927937

928938
/// Converts an InternalObjectsMapEntry to LiveMapValue using the same logic as get(key:)
929939
/// This is used by entries to ensure consistent value conversion
930-
private func nosync_convertEntryToLiveMapValue(_ entry: InternalObjectsMapEntry, delegate: LiveMapObjectPoolDelegate) -> InternalLiveMapValue? {
940+
private func nosync_convertEntryToLiveMapValue(_ entry: InternalObjectsMapEntry, objectsPool: ObjectsPool) -> InternalLiveMapValue? {
931941
// RTLM5d2a: If ObjectsMapEntry.tombstone is true, return undefined/null
932942
if entry.tombstone == true {
933943
return nil
@@ -968,7 +978,7 @@ internal final class InternalDefaultLiveMap: Sendable {
968978
// RTLM5d2f: If ObjectsMapEntry.data.objectId exists, get the object stored at that objectId from the internal ObjectsPool
969979
if let objectId = entry.data?.objectId {
970980
// RTLM5d2f1: If an object with id objectId does not exist, return undefined/null
971-
guard let poolEntry = delegate.nosync_getObjectFromPool(id: objectId) else {
981+
guard let poolEntry = objectsPool.entries[objectId] else {
972982
return nil
973983
}
974984

Sources/AblyLiveObjects/Internal/InternalDefaultRealtimeObjects.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ internal import _AblyPluginSupportPrivate
22
import Ably
33

44
/// This provides the implementation behind ``PublicDefaultRealtimeObjects``, via internal versions of the ``RealtimeObjects`` API.
5-
internal final class InternalDefaultRealtimeObjects: Sendable, LiveMapObjectPoolDelegate {
5+
internal final class InternalDefaultRealtimeObjects: Sendable, LiveMapObjectsPoolDelegate {
66
private let mutableStateMutex: DispatchQueueMutex<MutableState>
77

88
private let logger: Logger
@@ -139,11 +139,11 @@ internal final class InternalDefaultRealtimeObjects: Sendable, LiveMapObjectPool
139139
garbageCollectionTask.cancel()
140140
}
141141

142-
// MARK: - LiveMapObjectPoolDelegate
142+
// MARK: - LiveMapObjectsPoolDelegate
143143

144-
internal func nosync_getObjectFromPool(id: String) -> ObjectsPool.Entry? {
144+
internal var nosync_objectsPool: ObjectsPool {
145145
mutableStateMutex.withoutSync { mutableState in
146-
mutableState.objectsPool.entries[id]
146+
mutableState.objectsPool
147147
}
148148
}
149149

Sources/AblyLiveObjects/Public/Public Proxy Objects/InternalLiveMapValue+ToPublic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ internal extension InternalLiveMapValue {
55

66
struct PublicValueCreationArgs {
77
internal var coreSDK: CoreSDK
8-
internal var mapDelegate: LiveMapObjectPoolDelegate
8+
internal var mapDelegate: LiveMapObjectsPoolDelegate
99
internal var logger: Logger
1010

1111
internal var toCounterCreationArgs: PublicObjectsStore.CounterCreationArgs {

Sources/AblyLiveObjects/Public/Public Proxy Objects/PublicDefaultLiveMap.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ internal final class PublicDefaultLiveMap: LiveMap {
1010
// MARK: - Dependencies that hold a strong reference to `proxied`
1111

1212
private let coreSDK: CoreSDK
13-
private let delegate: LiveMapObjectPoolDelegate
13+
private let delegate: LiveMapObjectsPoolDelegate
1414
private let logger: Logger
1515

16-
internal init(proxied: InternalDefaultLiveMap, coreSDK: CoreSDK, delegate: LiveMapObjectPoolDelegate, logger: Logger) {
16+
internal init(proxied: InternalDefaultLiveMap, coreSDK: CoreSDK, delegate: LiveMapObjectsPoolDelegate, logger: Logger) {
1717
self.proxied = proxied
1818
self.coreSDK = coreSDK
1919
self.delegate = delegate

Sources/AblyLiveObjects/Public/Public Proxy Objects/PublicObjectsStore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal final class PublicObjectsStore: Sendable {
4545

4646
internal struct MapCreationArgs {
4747
internal var coreSDK: CoreSDK
48-
internal var delegate: LiveMapObjectPoolDelegate
48+
internal var delegate: LiveMapObjectsPoolDelegate
4949
internal var logger: Logger
5050
}
5151

0 commit comments

Comments
 (0)