Skip to content

Commit 040987b

Browse files
Implement tombstone() method
Based on [1] at 488e932. Haven't updated tests due to being in a bit of a rush; deferred to #52. [1] ably/specification#350
1 parent 59783b1 commit 040987b

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

Sources/AblyLiveObjects/Internal/InternalDefaultLiveCounter.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,5 +317,11 @@ internal final class InternalDefaultLiveCounter: Sendable {
317317
data += amount
318318
return .update(DefaultLiveCounterUpdate(amount: amount))
319319
}
320+
321+
/// Needed for ``InternalLiveObject`` conformance.
322+
mutating func resetDataToZeroValued() {
323+
// RTLC4
324+
data = 0
325+
}
320326
}
321327
}

Sources/AblyLiveObjects/Internal/InternalDefaultLiveMap.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,12 @@ internal final class InternalDefaultLiveMap: Sendable {
655655
let mapUpdate = DefaultLiveMapUpdate(update: previousData.mapValues { _ in .removed })
656656
liveObjectMutableState.emit(.update(mapUpdate), on: userCallbackQueue)
657657
}
658+
659+
/// Needed for ``InternalLiveObject`` conformance.
660+
mutating func resetDataToZeroValued() {
661+
// RTLM4
662+
data = [:]
663+
}
658664
}
659665

660666
// MARK: - Helper Methods
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,50 @@
1+
internal import AblyPlugin
2+
13
/// Provides RTLO spec point functionality common to all LiveObjects.
24
///
35
/// This exists in addition to ``LiveObjectMutableState`` to enable polymorphism.
46
internal protocol InternalLiveObject<Update> {
57
associatedtype Update: Sendable
68

79
var liveObjectMutableState: LiveObjectMutableState<Update> { get set }
10+
11+
/// Resets the LiveObject's internal data to that of a zero-value, per RTLO4e4.
12+
mutating func resetDataToZeroValued()
13+
}
14+
15+
internal extension InternalLiveObject {
16+
/// Convenience method for tombstoning a `LiveObject`, as specified in RTLO4e.
17+
mutating func tombstone(
18+
objectMessageSerialTimestamp: Date?,
19+
logger: Logger,
20+
clock: SimpleClock,
21+
) {
22+
// RTLO4e2, RTLO4e3
23+
if let objectMessageSerialTimestamp {
24+
// RTLO4e3a
25+
liveObjectMutableState.tombstonedAt = objectMessageSerialTimestamp
26+
} else {
27+
// RTLO4e3b1
28+
logger.log("serialTimestamp not found in ObjectMessage, using local clock for tombstone timestamp", level: .debug)
29+
// RTLO4e3b
30+
liveObjectMutableState.tombstonedAt = clock.now
31+
}
32+
33+
// RTLO4e4
34+
resetDataToZeroValued()
35+
}
36+
37+
/// Applies an `OBJECT_DELETE` operation, per RTLO5.
38+
mutating func applyObjectDeleteOperation(
39+
objectMessageSerialTimestamp: Date?,
40+
logger: Logger,
41+
clock: SimpleClock,
42+
) {
43+
// RTLO5b
44+
tombstone(
45+
objectMessageSerialTimestamp: objectMessageSerialTimestamp,
46+
logger: logger,
47+
clock: clock,
48+
)
49+
}
850
}

Sources/AblyLiveObjects/Internal/LiveObjectMutableState.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,26 @@ internal struct LiveObjectMutableState<Update: Sendable> {
1010
internal var siteTimeserials: [String: String] = [:]
1111
// RTLO3c
1212
internal var createOperationIsMerged = false
13+
// RTLO3d
14+
internal var isTombstone: Bool {
15+
// TODO: Confirm that we don't need to store this (https://github.com/ably/specification/pull/350/files#r2213895661)
16+
tombstonedAt != nil
17+
}
18+
19+
// RTLO3e
20+
internal var tombstonedAt: Date?
1321

1422
/// Internal bookkeeping for subscriptions.
1523
private var subscriptionsByID: [Subscription.ID: Subscription] = [:]
1624

1725
internal init(
1826
objectID: String,
1927
testsOnly_siteTimeserials siteTimeserials: [String: String]? = nil,
28+
testsOnly_tombstonedAt tombstonedAt: Date? = nil,
2029
) {
2130
self.objectID = objectID
2231
self.siteTimeserials = siteTimeserials ?? [:]
32+
self.tombstonedAt = tombstonedAt
2333
}
2434

2535
/// Represents parameters of an operation that `canApplyOperation` has decided can be applied to a `LiveObject`.

0 commit comments

Comments
 (0)