Skip to content

Commit 5e0438f

Browse files
Improve waitForMapClear to check expected removed keys
Instead of waiting for any update, waitForMapClear now takes an expectedRemovedKeys parameter and waits for an update where all specified keys have .removed status. This serves as a better proxy for the JS operation action check. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c44bccf commit 5e0438f

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

Tests/AblyLiveObjectsTests/JS Integration Tests/ObjectsIntegrationTests.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,17 @@ func waitForCounterUpdate(_ updates: AsyncStream<LiveCounterUpdate>) async {
9696
_ = await updates.first { _ in true }
9797
}
9898

99-
/// Waits for a MAP_CLEAR operation to be applied to a LiveMap.
99+
/// Waits for a MAP_CLEAR operation to be applied to a LiveMap, by waiting for an update where
100+
/// all of the specified keys have `.removed` status.
100101
///
101102
/// The JS equivalent subscribes and checks `message.operation.action === 'map.clear'`, but Swift's
102103
/// `LiveMapUpdate` doesn't expose the operation action — it only provides per-key changes via
103-
/// `update: [String: LiveMapUpdateAction]`. Since callers of this helper only send a MAP_CLEAR
104-
/// operation, waiting for any update on the stream is sufficient.
105-
func waitForMapClear(_ updates: AsyncStream<LiveMapUpdate>) async {
106-
_ = await updates.first { _ in true }
104+
/// `update: [String: LiveMapUpdateAction]`. So we use the per-key `.removed` entries as a proxy
105+
/// instead.
106+
func waitForMapClear(_ updates: AsyncStream<LiveMapUpdate>, expectedRemovedKeys: Set<String>) async {
107+
_ = await updates.first { update in
108+
expectedRemovedKeys.allSatisfy { update.update[$0] == .removed }
109+
}
107110
}
108111

109112
// Note that Cursor decided to implement this in a different way to the waitForObjectSync that I'd already implemented; TODO pick one of the two approaches (this one might be cleaner).
@@ -2597,7 +2600,7 @@ private struct ObjectsIntegrationTests {
25972600

25982601
// send MAP_CLEAR
25992602
let clearAppliedPromiseUpdates = try root.updates()
2600-
async let clearAppliedPromise: Void = waitForMapClear(clearAppliedPromiseUpdates)
2603+
async let clearAppliedPromise: Void = waitForMapClear(clearAppliedPromiseUpdates, expectedRemovedKeys: ["foo", "baz"])
26012604
try await objectsHelper.sendMapClearOnChannel(objects: objects, objectId: "root")
26022605
await clearAppliedPromise
26032606

0 commit comments

Comments
 (0)