Skip to content

Commit 79c94da

Browse files
Extract LiveObject common mutable state into a new type
Based on TODO
1 parent 478bc73 commit 79c94da

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed

Sources/AblyLiveObjects/Internal/DefaultLiveCounter.swift

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ internal final class DefaultLiveCounter: LiveCounter {
1010

1111
internal var testsOnly_siteTimeserials: [String: String] {
1212
mutex.withLock {
13-
mutableState.siteTimeserials
13+
mutableState.liveObject.siteTimeserials
1414
}
1515
}
1616

1717
internal var testsOnly_createOperationIsMerged: Bool {
1818
mutex.withLock {
19-
mutableState.createOperationIsMerged
19+
mutableState.liveObject.createOperationIsMerged
2020
}
2121
}
2222

2323
internal var testsOnly_objectID: String {
2424
mutex.withLock {
25-
mutableState.objectID
25+
mutableState.liveObject.objectID
2626
}
2727
}
2828

@@ -43,7 +43,7 @@ internal final class DefaultLiveCounter: LiveCounter {
4343
objectID: String,
4444
coreSDK: CoreSDK
4545
) {
46-
mutableState = .init(data: data, objectID: objectID)
46+
mutableState = .init(liveObject: .init(objectID: objectID), data: data)
4747
self.coreSDK = coreSDK
4848
}
4949

@@ -119,25 +119,19 @@ internal final class DefaultLiveCounter: LiveCounter {
119119
// MARK: - Mutable state and the operations that affect it
120120

121121
private struct MutableState {
122+
/// The mutable state common to all LiveObjects.
123+
internal var liveObject: LiveObjectMutableState
124+
122125
/// The internal data that this map holds, per RTLC3.
123126
internal var data: Double
124127

125-
/// The site timeserials for this counter, per RTLC6a.
126-
internal var siteTimeserials: [String: String] = [:]
127-
128-
/// Whether the create operation has been merged, per RTLC6b and RTLC6d2.
129-
internal var createOperationIsMerged = false
130-
131-
/// The "private `objectId` field" of RTO5c1b1a.
132-
internal var objectID: String
133-
134128
/// Replaces the internal data of this counter with the provided ObjectState, per RTLC6.
135129
internal mutating func replaceData(using state: ObjectState) {
136130
// RTLC6a: Replace the private siteTimeserials with the value from ObjectState.siteTimeserials
137-
siteTimeserials = state.siteTimeserials
131+
liveObject.siteTimeserials = state.siteTimeserials
138132

139133
// RTLC6b: Set the private flag createOperationIsMerged to false
140-
createOperationIsMerged = false
134+
liveObject.createOperationIsMerged = false
141135

142136
// RTLC6c: Set data to the value of ObjectState.counter.count, or to 0 if it does not exist
143137
data = state.counter?.count?.doubleValue ?? 0
@@ -149,7 +143,7 @@ internal final class DefaultLiveCounter: LiveCounter {
149143
data += createOpCount
150144
}
151145
// RTLC6d2: Set the private flag createOperationIsMerged to true
152-
createOperationIsMerged = true
146+
liveObject.createOperationIsMerged = true
153147
}
154148
}
155149
}

Sources/AblyLiveObjects/Internal/DefaultLiveMap.swift

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal final class DefaultLiveMap: LiveMap {
2121

2222
internal var testsOnly_objectID: String {
2323
mutex.withLock {
24-
mutableState.objectID
24+
mutableState.liveObject.objectID
2525
}
2626
}
2727

@@ -33,13 +33,13 @@ internal final class DefaultLiveMap: LiveMap {
3333

3434
internal var testsOnly_siteTimeserials: [String: String] {
3535
mutex.withLock {
36-
mutableState.siteTimeserials
36+
mutableState.liveObject.siteTimeserials
3737
}
3838
}
3939

4040
internal var testsOnly_createOperationIsMerged: Bool {
4141
mutex.withLock {
42-
mutableState.createOperationIsMerged
42+
mutableState.liveObject.createOperationIsMerged
4343
}
4444
}
4545

@@ -76,15 +76,15 @@ internal final class DefaultLiveMap: LiveMap {
7676
delegate: LiveMapObjectPoolDelegate?,
7777
coreSDK: CoreSDK
7878
) {
79-
mutableState = .init(data: data, objectID: objectID, semantics: semantics)
79+
mutableState = .init(liveObject: .init(objectID: objectID), data: data, semantics: semantics)
8080
self.delegate = .init(referenced: delegate)
8181
self.coreSDK = coreSDK
8282
}
8383

8484
/// Creates a "zero-value LiveMap", per RTLM4.
8585
///
8686
/// - Parameters:
87-
/// - objectID: The value to use for the "private `objectId` field" of RTO5c1b1b.
87+
/// - objectID: The value to use for the RTLO3a `objectID` property.
8888
/// - semantics: The value to use for the "private `semantics` field" of RTO5c1b1b.
8989
internal static func createZeroValued(
9090
objectID: String,
@@ -271,21 +271,15 @@ internal final class DefaultLiveMap: LiveMap {
271271
// MARK: - Mutable state and the operations that affect it
272272

273273
private struct MutableState {
274+
/// The mutable state common to all LiveObjects.
275+
internal var liveObject: LiveObjectMutableState
276+
274277
/// The internal data that this map holds, per RTLM3.
275278
internal var data: [String: ObjectsMapEntry]
276279

277-
/// The "private `objectId` field" of RTO5c1b1b.
278-
internal var objectID: String
279-
280280
/// The "private `semantics` field" of RTO5c1b1b.
281281
internal var semantics: WireEnum<ObjectsMapSemantics>?
282282

283-
/// The site timeserials for this map, per RTLM6a.
284-
internal var siteTimeserials: [String: String] = [:]
285-
286-
/// Whether the create operation has been merged, per RTLM6b and RTLM6d2.
287-
internal var createOperationIsMerged = false
288-
289283
/// Replaces the internal data of this map with the provided ObjectState, per RTLM6.
290284
///
291285
/// - Parameters:
@@ -297,10 +291,10 @@ internal final class DefaultLiveMap: LiveMap {
297291
coreSDK: CoreSDK,
298292
) {
299293
// RTLM6a: Replace the private siteTimeserials with the value from ObjectState.siteTimeserials
300-
siteTimeserials = state.siteTimeserials
294+
liveObject.siteTimeserials = state.siteTimeserials
301295

302296
// RTLM6b: Set the private flag createOperationIsMerged to false
303-
createOperationIsMerged = false
297+
liveObject.createOperationIsMerged = false
304298

305299
// RTLM6c: Set data to ObjectState.map.entries, or to an empty map if it does not exist
306300
data = state.map?.entries ?? [:]
@@ -332,7 +326,7 @@ internal final class DefaultLiveMap: LiveMap {
332326
}
333327
}
334328
// RTLM6d2: Set the private flag createOperationIsMerged to true
335-
createOperationIsMerged = true
329+
liveObject.createOperationIsMerged = true
336330
}
337331
}
338332

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// This is the equivalent of the `LiveObject` abstract class described in RTLO.
2+
///
3+
/// ``DefaultLiveCounter`` and ``DefaultLiveMap`` include it by composition.
4+
internal struct LiveObjectMutableState {
5+
// RTLO3a
6+
internal var objectID: String
7+
// RTLO3b
8+
internal var siteTimeserials: [String: String] = [:]
9+
// RTLO3c
10+
internal var createOperationIsMerged = false
11+
}

0 commit comments

Comments
 (0)