Skip to content

Commit dfaa568

Browse files
Implement increment / decrement / set / delete
haven't done mode checking of RTLC12b Based on [1] at cb11ba8. [1] ably/specification#353
1 parent 9e386de commit dfaa568

4 files changed

Lines changed: 96 additions & 12 deletions

File tree

Sources/AblyLiveObjects/Internal/InternalDefaultLiveCounter.swift

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,43 @@ internal final class InternalDefaultLiveCounter: Sendable {
8989
}
9090
}
9191

92-
internal func increment(amount _: Double) async throws(ARTErrorInfo) {
93-
notYetImplemented()
92+
internal func increment(amount: Double, coreSDK: CoreSDK) async throws(ARTErrorInfo) {
93+
do throws(InternalError) {
94+
// RTLC12c
95+
do {
96+
try coreSDK.validateChannelState(notIn: [.detached, .failed, .suspended], operationDescription: "RealtimeObjects.createMap")
97+
} catch {
98+
throw error.toInternalError()
99+
}
100+
101+
// RTLC12e1
102+
if !amount.isFinite {
103+
throw LiveObjectsError.counterIncrementAmountInvalid(amount: amount).toARTErrorInfo().toInternalError()
104+
}
105+
106+
let objectMessage = OutboundObjectMessage(
107+
operation: .init(
108+
// RTLC12e2
109+
action: .known(.counterInc),
110+
// RTLC12e3
111+
objectId: objectID,
112+
counterOp: .init(
113+
// RTLC12e4
114+
amount: .init(value: amount),
115+
),
116+
),
117+
)
118+
119+
// RTLC12f
120+
try await coreSDK.publish(objectMessages: [objectMessage])
121+
} catch {
122+
throw error.toARTErrorInfo()
123+
}
94124
}
95125

96-
internal func decrement(amount _: Double) async throws(ARTErrorInfo) {
97-
notYetImplemented()
126+
internal func decrement(amount: Double, coreSDK: CoreSDK) async throws(ARTErrorInfo) {
127+
// RTLC13b
128+
try await increment(amount: -amount, coreSDK: coreSDK)
98129
}
99130

100131
@discardableResult

Sources/AblyLiveObjects/Internal/InternalDefaultLiveMap.swift

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,65 @@ internal final class InternalDefaultLiveMap: Sendable {
173173
try entries(coreSDK: coreSDK, delegate: delegate).map(\.value)
174174
}
175175

176-
internal func set(key _: String, value _: LiveMapValue) async throws(ARTErrorInfo) {
177-
notYetImplemented()
176+
internal func set(key: String, value: LiveMapValue, coreSDK: CoreSDK) async throws(ARTErrorInfo) {
177+
do throws(InternalError) {
178+
// RTLM20c
179+
do {
180+
try coreSDK.validateChannelState(notIn: [.detached, .failed, .suspended], operationDescription: "RealtimeObjects.createMap")
181+
} catch {
182+
throw error.toInternalError()
183+
}
184+
185+
let internalEntry = InternalLiveMapValue(liveMapValue: value)
186+
187+
let objectMessage = OutboundObjectMessage(
188+
operation: .init(
189+
// RTLM20e2
190+
action: .known(.mapSet),
191+
// RTLM20e3
192+
objectId: objectID,
193+
mapOp: .init(
194+
// RTLM20e4
195+
key: key,
196+
// RTLM20e5
197+
data: internalEntry.toObjectData,
198+
),
199+
),
200+
)
201+
202+
try await coreSDK.publish(objectMessages: [objectMessage])
203+
} catch {
204+
throw error.toARTErrorInfo()
205+
}
178206
}
179207

180-
internal func remove(key _: String) async throws(ARTErrorInfo) {
181-
notYetImplemented()
208+
internal func remove(key: String, coreSDK: CoreSDK) async throws(ARTErrorInfo) {
209+
do throws(InternalError) {
210+
// RTLM21c
211+
do {
212+
try coreSDK.validateChannelState(notIn: [.detached, .failed, .suspended], operationDescription: "RealtimeObjects.createMap")
213+
} catch {
214+
throw error.toInternalError()
215+
}
216+
217+
let objectMessage = OutboundObjectMessage(
218+
operation: .init(
219+
// RTLM21e2
220+
action: .known(.mapRemove),
221+
// RTLM21e3
222+
objectId: objectID,
223+
mapOp: .init(
224+
// RTLM21e4
225+
key: key,
226+
),
227+
),
228+
)
229+
230+
// RTLM21f
231+
try await coreSDK.publish(objectMessages: [objectMessage])
232+
} catch {
233+
throw error.toARTErrorInfo()
234+
}
182235
}
183236

184237
@discardableResult

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ internal final class PublicDefaultLiveCounter: LiveCounter {
2727
}
2828

2929
internal func increment(amount: Double) async throws(ARTErrorInfo) {
30-
try await proxied.increment(amount: amount)
30+
try await proxied.increment(amount: amount, coreSDK: coreSDK)
3131
}
3232

3333
internal func decrement(amount: Double) async throws(ARTErrorInfo) {
34-
try await proxied.decrement(amount: amount)
34+
try await proxied.decrement(amount: amount, coreSDK: coreSDK)
3535
}
3636

3737
internal func subscribe(listener: @escaping LiveObjectUpdateCallback<LiveCounterUpdate>) throws(ARTErrorInfo) -> any SubscribeResponse {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ internal final class PublicDefaultLiveMap: LiveMap {
7676
}
7777

7878
internal func set(key: String, value: LiveMapValue) async throws(ARTErrorInfo) {
79-
try await proxied.set(key: key, value: value)
79+
try await proxied.set(key: key, value: value, coreSDK: coreSDK)
8080
}
8181

8282
internal func remove(key: String) async throws(ARTErrorInfo) {
83-
try await proxied.remove(key: key)
83+
try await proxied.remove(key: key, coreSDK: coreSDK)
8484
}
8585

8686
internal func subscribe(listener: @escaping LiveObjectUpdateCallback<LiveMapUpdate>) throws(ARTErrorInfo) -> any SubscribeResponse {

0 commit comments

Comments
 (0)