Skip to content

Commit da6e9d9

Browse files
committed
fix(web): skip stale close-time retry after a newer draft
1 parent c5ec681 commit da6e9d9

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,32 @@ describe("FileSaveCoordinator", () => {
270270
expect(onPendingChange.mock.calls.at(-1)).toEqual([false]);
271271
});
272272

273+
it("skips a closed editor retry after another edit replaces its draft", async () => {
274+
vi.useFakeTimers();
275+
let currentDraft = "old";
276+
const persist = vi
277+
.fn()
278+
.mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("write failed"))))
279+
.mockResolvedValue(AsyncResult.success(undefined));
280+
const shouldRetryAfterClose = vi.fn((contents: string) => contents === currentDraft);
281+
const coordinator = new FileSaveCoordinator({
282+
debounceMs: 500,
283+
persist,
284+
onPendingChange: vi.fn(),
285+
onConfirmed: vi.fn(),
286+
shouldRetryAfterClose,
287+
});
288+
289+
coordinator.change("old");
290+
coordinator.dispose();
291+
await vi.advanceTimersByTimeAsync(0);
292+
currentDraft = "new";
293+
await vi.advanceTimersByTimeAsync(500);
294+
295+
expect(shouldRetryAfterClose).toHaveBeenCalledWith("old");
296+
expect(persist).toHaveBeenCalledExactlyOnceWith("old");
297+
});
298+
273299
it("ignores editor changes emitted after disposal", async () => {
274300
vi.useFakeTimers();
275301
const persist = vi

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
33
export interface FileSaveCoordinatorOptions<A, E> {
44
readonly debounceMs: number;
55
readonly canPersist?: () => boolean;
6+
/** Refuse stale close-time retries after another editor replaces the draft. */
7+
readonly shouldRetryAfterClose?: (contents: string) => boolean;
68
readonly persist: (contents: string) => Promise<AtomCommandResult<A, E>>;
79
readonly onPendingChange: (pending: boolean) => void;
810
/** Return false when another editor has newer unsaved contents. */
@@ -17,6 +19,7 @@ export class FileSaveCoordinator<A = unknown, E = unknown> {
1719
private lastChangeAt = 0;
1820
private saving = false;
1921
private disposed = false;
22+
private closeRetryPending = false;
2023

2124
constructor(private readonly options: FileSaveCoordinatorOptions<A, E>) {}
2225

@@ -55,6 +58,14 @@ export class FileSaveCoordinator<A = unknown, E = unknown> {
5558
return;
5659
}
5760

61+
if (
62+
this.closeRetryPending &&
63+
this.options.shouldRetryAfterClose?.(this.latestContents) === false
64+
) {
65+
return;
66+
}
67+
this.closeRetryPending = false;
68+
5869
this.saving = true;
5970
const contents = this.latestContents;
6071
const revision = this.latestRevision;
@@ -73,6 +84,7 @@ export class FileSaveCoordinator<A = unknown, E = unknown> {
7384
} else if (!succeeded && this.disposed) {
7485
// Closing the editor must not discard an edit whose final save failed.
7586
// Retry after the normal debounce; a successful write stops the retry.
87+
this.closeRetryPending = true;
7688
this.schedule(this.options.debounceMs);
7789
}
7890
return;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ function createFileSaveSession({
8585
const coordinator = new FileSaveCoordinator({
8686
debounceMs: FILE_SAVE_DEBOUNCE_MS,
8787
canPersist: () => readEnvironmentScope(environmentId, AuthFilesystemWriteScope),
88+
shouldRetryAfterClose: (contents) =>
89+
getUnsavedProjectFileQueryData(environmentId, cwd, relativePath)?.contents === contents,
8890
onPendingChange: (pending) => onPendingChange(relativePath, pending),
8991
persist: (nextContents) =>
9092
writeFile({

0 commit comments

Comments
 (0)