Skip to content

Commit dc19c82

Browse files
committed
fix(web): roll back a failed save even after the editor unmounts
A write that failed after dispose left the next surface showing the optimistic text and still marked unsaved. Rollback now still runs. If a newer overlay is already there, keep that text and mark the file pending again.
1 parent a58ea12 commit dc19c82

3 files changed

Lines changed: 16 additions & 12 deletions

File tree

‎apps/web/src/components/files/FilePreviewPanel.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ function useFileSaveCoordinator({
429429
onRollback: ({ failedContents, result }) => {
430430
const overlay = getOptimisticProjectFileQueryData(environmentId, cwd, relativePath);
431431
if (overlay !== null && overlay.contents !== failedContents) {
432+
onPendingChange(relativePath, true);
432433
return;
433434
}
434435
clearProjectFileQueryData(environmentId, cwd, relativePath);

‎apps/web/src/components/files/fileSaveCoordinator.test.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,12 @@ describe("FileSaveCoordinator", () => {
151151
expect(onPendingChange).not.toHaveBeenCalledWith(false);
152152
});
153153

154-
it("does not roll back or clear pending after dispose", async () => {
154+
it("still rolls back a failed write that finishes after dispose", async () => {
155155
vi.useFakeTimers();
156156
const write = deferred();
157157
const onRollback = vi.fn();
158158
const onPendingChange = vi.fn();
159+
const failure = AsyncResult.failure(Cause.fail(new Error("write failed")));
159160
const coordinator = new FileSaveCoordinator({
160161
debounceMs: 500,
161162
initialContents: "disk",
@@ -168,10 +169,14 @@ describe("FileSaveCoordinator", () => {
168169
coordinator.change("latest");
169170
await vi.advanceTimersByTimeAsync(500);
170171
coordinator.dispose();
171-
write.resolve(AsyncResult.failure(Cause.fail(new Error("write failed"))));
172+
write.resolve(failure);
172173
await Promise.resolve();
173-
expect(onRollback).not.toHaveBeenCalled();
174-
expect(onPendingChange).not.toHaveBeenCalledWith(false);
174+
expect(onRollback).toHaveBeenCalledWith({
175+
failedContents: "latest",
176+
confirmedContents: "disk",
177+
result: failure,
178+
});
179+
expect(onPendingChange.mock.calls.at(-1)).toEqual([false]);
175180
});
176181

177182
it("does not persist a discarded failed edit on dispose", async () => {

‎apps/web/src/components/files/fileSaveCoordinator.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,12 @@ export class FileSaveCoordinator<A = unknown, E = unknown> {
9595
if (!succeeded) {
9696
this.latestRevision = 0;
9797
this.latestContents = this.lastConfirmedContents;
98-
if (!this.disposed) {
99-
this.options.onRollback({
100-
failedContents: contents,
101-
confirmedContents: this.lastConfirmedContents,
102-
result,
103-
});
104-
this.options.onPendingChange(false);
105-
}
98+
this.options.onRollback({
99+
failedContents: contents,
100+
confirmedContents: this.lastConfirmedContents,
101+
result,
102+
});
103+
this.options.onPendingChange(false);
106104
return;
107105
}
108106
if (!this.disposed) this.options.onPendingChange(false);

0 commit comments

Comments
 (0)