Skip to content

Commit 9a8b6ee

Browse files
Mark subscribe methods as throwing
Motivation as in 3f6de86; the new spec points in [1] tell us these can throw. [1] ably/specification#346
1 parent 727b918 commit 9a8b6ee

2 files changed

Lines changed: 29 additions & 21 deletions

File tree

Sources/AblyLiveObjects/Public/PublicTypes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public protocol LiveObject: AnyObject, Sendable {
342342
/// - Parameter listener: An event listener function that is called with an update object whenever this LiveObject is updated.
343343
/// - Returns: A ``SubscribeResponse`` object that allows the provided listener to be deregistered from future updates.
344344
@discardableResult
345-
func subscribe(listener: @escaping LiveObjectUpdateCallback<Update>) -> SubscribeResponse
345+
func subscribe(listener: @escaping LiveObjectUpdateCallback<Update>) throws(ARTErrorInfo) -> SubscribeResponse
346346

347347
/// Deregisters all listeners from updates for this LiveObject.
348348
func unsubscribeAll()

Tests/AblyLiveObjectsTests/JS Integration Tests/ObjectsIntegrationTests.swift

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,30 @@ func waitFixtureChannelIsReady(_: ARTRealtime) async throws {
8888
try await Task.sleep(nanoseconds: 5 * NSEC_PER_SEC)
8989
}
9090

91-
func waitForMapKeyUpdate(_ map: any LiveMap, _ key: String) async {
92-
await withCheckedContinuation { (continuation: CheckedContinuation<Void, _>) in
93-
map.subscribe { update, subscription in
94-
if update.update[key] != nil {
95-
subscription.unsubscribe()
96-
continuation.resume()
91+
func waitForMapKeyUpdate(_ map: any LiveMap, _ key: String) async throws {
92+
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, _>) in
93+
do {
94+
try map.subscribe { update, subscription in
95+
if update.update[key] != nil {
96+
subscription.unsubscribe()
97+
continuation.resume()
98+
}
9799
}
100+
} catch {
101+
continuation.resume(throwing: error)
98102
}
99103
}
100104
}
101105

102-
func waitForCounterUpdate(_ counter: any LiveCounter) async {
103-
await withCheckedContinuation { (continuation: CheckedContinuation<Void, _>) in
104-
counter.subscribe { _, subscription in
105-
subscription.unsubscribe()
106-
continuation.resume()
106+
func waitForCounterUpdate(_ counter: any LiveCounter) async throws {
107+
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, _>) in
108+
do {
109+
try counter.subscribe { _, subscription in
110+
subscription.unsubscribe()
111+
continuation.resume()
112+
}
113+
} catch {
114+
continuation.resume(throwing: error)
107115
}
108116
}
109117
}
@@ -310,10 +318,10 @@ private struct ObjectsIntegrationTests {
310318
// Create the promise first, before the operations that will trigger it
311319
async let objectsCreatedPromise: Void = withThrowingTaskGroup(of: Void.self) { group in
312320
group.addTask {
313-
await waitForMapKeyUpdate(root, "counter")
321+
try await waitForMapKeyUpdate(root, "counter")
314322
}
315323
group.addTask {
316-
await waitForMapKeyUpdate(root, "map")
324+
try await waitForMapKeyUpdate(root, "map")
317325
}
318326
while try await group.next() != nil {}
319327
}
@@ -331,13 +339,13 @@ private struct ObjectsIntegrationTests {
331339
// Create the promise first, before the operations that will trigger it
332340
async let operationsAppliedPromise: Void = withThrowingTaskGroup(of: Void.self) { group in
333341
group.addTask {
334-
await waitForMapKeyUpdate(map, "anotherKey")
342+
try await waitForMapKeyUpdate(map, "anotherKey")
335343
}
336344
group.addTask {
337-
await waitForMapKeyUpdate(map, "shouldDelete")
345+
try await waitForMapKeyUpdate(map, "shouldDelete")
338346
}
339347
group.addTask {
340-
await waitForCounterUpdate(counter)
348+
try await waitForCounterUpdate(counter)
341349
}
342350
while try await group.next() != nil {}
343351
}
@@ -382,10 +390,10 @@ private struct ObjectsIntegrationTests {
382390
// Create the promise first, before the operations that will trigger it
383391
async let objectsCreatedPromise: Void = withThrowingTaskGroup(of: Void.self) { group in
384392
group.addTask {
385-
await waitForMapKeyUpdate(root, "counter")
393+
try await waitForMapKeyUpdate(root, "counter")
386394
}
387395
group.addTask {
388-
await waitForMapKeyUpdate(root, "map")
396+
try await waitForMapKeyUpdate(root, "map")
389397
}
390398
while try await group.next() != nil {}
391399
}
@@ -582,7 +590,7 @@ private struct ObjectsIntegrationTests {
582590
key: "counter",
583591
createOp: objectsHelper.counterCreateRestOp(number: 1),
584592
)
585-
_ = await counterCreatedPromise
593+
_ = try await counterCreatedPromise
586594

587595
#expect(try root.get(key: "counter") != nil, "Check counter exists on root before OBJECT_SYNC sequence with \"tombstone=true\"")
588596

@@ -636,7 +644,7 @@ private struct ObjectsIntegrationTests {
636644
key: "counter",
637645
createOp: objectsHelper.counterCreateRestOp(number: 1),
638646
)
639-
_ = await counterCreatedPromise
647+
_ = try await counterCreatedPromise
640648

641649
async let counterSubPromise: Void = withCheckedThrowingContinuation { continuation in
642650
do {

0 commit comments

Comments
 (0)