Native: a failed edit the server turns out to hold is saved after all; Revert reads the server
Review #72 consideration 8. When an edit's answer was lost but the server applied it, the cell stayed marked as not saved, and Revert painted the value from before the edit while the server held the new one. Failed edits whose value the server's cell now holds (from the live stream or a refetch) are dropped as saved, and Revert refetches the sheet instead of trusting the copy from before the edit. Tests: aFailedEditTheServerHoldsIsSavedAfterAll, revertReadsTheServerBackInCaseTheEditLanded.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+41
−0
native/apple/Sources/SheetsKit/SheetModel.swift+2 −0native/apple/Sources/SheetsKit/SheetWrites.swift+12 −0native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift+27 −0
diff --git a/native/apple/Sources/SheetsKit/SheetModel.swift b/native/apple/Sources/SheetsKit/SheetModel.swift| index 4d189dc..0844a9c 100644 |
| --- a/native/apple/Sources/SheetsKit/SheetModel.swift |
| +++ b/native/apple/Sources/SheetsKit/SheetModel.swift |
| @@ -137,6 +137,7 @@ import RastrilloApple |
| cells = updated |
| styles = formats |
| sizes = fetchedSizes |
| + reconcileFailedEdits() |
| refreshDraftIfClean() |
| } |
| } catch is CancellationError { return } catch { self.error = error.localizedDescription } |
| @@ -166,6 +167,7 @@ import RastrilloApple |
| } |
| version = response.version |
| if let sizes = response.sizes { self.sizes = sizes } |
| + reconcileFailedEdits() |
| refreshDraftIfClean() |
| } |
| func refreshDraftIfClean() { |
diff --git a/native/apple/Sources/SheetsKit/SheetWrites.swift b/native/apple/Sources/SheetsKit/SheetWrites.swift| index bd95c18..20646c8 100644 |
| --- a/native/apple/Sources/SheetsKit/SheetWrites.swift |
| +++ b/native/apple/Sources/SheetsKit/SheetWrites.swift |
| @@ -107,12 +107,24 @@ struct PendingEdit: Sendable, Equatable { |
| } |
| /// Drops failed edits and shows the server's value again. Edits still being |
| /// sent are not touched: they may already have landed. |
| + /// Drops failed edits and shows the server's value again. An edit whose |
| + /// answer was lost may have landed after all, so the cells are read back |
| + /// from the server rather than trusted from before the edit. |
| func revertWrites(_ points: [Coordinate]? = nil) { |
| pending = pending.filter { point, edit in |
| edit.state != .failed || !(points?.contains(point) ?? true) |
| } |
| if !hasFailedEdits { error = "" } |
| if !dirty { discard() } |
| + Task { await refresh() } |
| + } |
| + |
| + /// A failed edit the server turns out to hold (its answer was lost, then |
| + /// the live stream or a refetch brought the cell back) is saved after all. |
| + func reconcileFailedEdits() { |
| + let landed = pending.filter { point, edit in edit.state == .failed && cells[point]?.raw == edit.write.raw } |
| + guard !landed.isEmpty else { return } |
| + pending = pending.filter { landed[$0.key] == nil } |
| } |
| /// Waits until nothing is queued or in flight. Failed edits stay failed. |
diff --git a/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift b/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift| index ceb0cac..297f5de 100644 |
| --- a/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift |
| +++ b/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift |
| @@ -298,3 +298,30 @@ private let b1 = Coordinate(r: 0, c: 1) |
| #expect(model.editState(a1) == .failed) |
| #expect(server.batches.count == 3) |
| } |
| + |
| +// Review #72, finding 8: an edit whose answer was lost may have landed. |
| +@MainActor @Test func aFailedEditTheServerHoldsIsSavedAfterAll() async { |
| + let server = SheetServerFixture() |
| + server.loseNextResponse() |
| + let model = fixtureModel(server) |
| + type(model, a1, "landed") |
| + await model.settle() |
| + #expect(model.editState(a1) == .failed) |
| + #expect(server.value(0, 0) == "landed", "the server applied it; only the answer was lost") |
| + // The live stream delivers the cell, as it does every edit. |
| + await model.accept(GridResponse(version: server.version, cells: [Cell(r: 0, c: 0, raw: "landed", display: "landed")], stamps: 1)) |
| + #expect(model.pending.isEmpty, "no longer marked as not saved") |
| + #expect(model.displayCell(a1)?.raw == "landed") |
| +} |
| + |
| +@MainActor @Test func revertReadsTheServerBackInCaseTheEditLanded() async { |
| + let server = SheetServerFixture() |
| + server.loseNextResponse() |
| + let model = fixtureModel(server) |
| + type(model, a1, "landed") |
| + await model.settle() |
| + #expect(model.editState(a1) == .failed) |
| + model.revertWrites([a1]) |
| + #expect(await eventually { model.cells[a1]?.raw == "landed" }, "the server's value, not the one from before the edit") |
| + #expect(model.displayCell(a1)?.raw == "landed") |
| +} |