Skip to content

Commit 9bd15c8

Browse files
Directly use ObjectsPool inside LiveMap getter methods
It's nicer to deal with value types instead of objects (the latter coming with synchronisation requirements to worry about). Resolves #39.
1 parent bb36453 commit 9bd15c8

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

Sources/AblyLiveObjects/Internal/InternalDefaultLiveMap.swift

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,29 @@ internal final class InternalDefaultLiveMap: Sendable {
123123
/// Returns the value associated with a given key, following RTLM5d specification.
124124
internal func get(key: String, coreSDK: CoreSDK, delegate: LiveMapObjectPoolDelegate) 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

130134
internal func size(coreSDK: CoreSDK, delegate: LiveMapObjectPoolDelegate) 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

136143
internal func entries(coreSDK: CoreSDK, delegate: LiveMapObjectPoolDelegate) 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

@@ -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_objectsPool.entries[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_objectsPool.entries[objectId] else {
981+
guard let poolEntry = objectsPool.entries[objectId] else {
972982
return nil
973983
}
974984

0 commit comments

Comments
 (0)