Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions pkgs/id/web/src/auto-save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,70 @@ describe("AutoSaveManager.onNewVersion vs a pending rate-limit retry (#2466)", (
expect(mgr.state).toBe("idle");
});
});

describe("AutoSaveManager cancel during an in-flight save (#2437)", () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});

it("schedules no retry when a rate-limited save completes after cancel()", async () => {
const save = deferredSaveFn();
const mgr = new AutoSaveManager(save.fn);

mgr.saveNow();
await flush();
expect(save.calls).toBe(1);
expect(mgr.state).toBe("saving");

mgr.cancel();
expect(mgr.state).toBe("idle");
const indicatorWrites = vi.spyOn(mgr, "updateIndicator");

save.resolveNext({ ok: false, retryAfterMs: 200 });
await flush();
expect(mgr.state).toBe("idle"); // not "rate-limited" — the completion was dropped
expect(indicatorWrites).not.toHaveBeenCalled();

await vi.advanceTimersByTimeAsync(10_000);
expect(save.calls).toBe(1); // the retry never fired a save for the closed editor
});

it("runs no follow-up when an ok save with an edit during flight completes after cancel()", async () => {
const save = deferredSaveFn();
const mgr = new AutoSaveManager(save.fn);

mgr.saveNow();
await flush();
expect(save.calls).toBe(1);

mgr.onContentChange(); // edit during the flight — would normally coalesce a follow-up
mgr.cancel();

save.resolveNext({ ok: true });
await flush();
await vi.advanceTimersByTimeAsync(10_000);
expect(save.calls).toBe(1); // no follow-up save after cancel
expect(mgr.state).toBe("idle");
});

it("runs no follow-up when a generically failing save with a coalesced edit completes after cancel()", async () => {
const save = deferredSaveFn();
const mgr = new AutoSaveManager(save.fn);

mgr.saveNow();
await flush();
expect(save.calls).toBe(1);

mgr.onContentChange();
mgr.cancel();

save.resolveNext({ ok: false });
await flush();
await vi.advanceTimersByTimeAsync(10_000);
expect(save.calls).toBe(1); // the #1616 follow-up must not run for a cancelled manager
expect(mgr.state).toBe("idle"); // not "error"/"unsaved" — no state churn after cancel
});
});
20 changes: 19 additions & 1 deletion pkgs/id/web/src/auto-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export class AutoSaveManager {
private saveInFlight = false;
/** A save was requested while one was in flight or a retry was pending; run one follow-up. */
private followUpRequested = false;
/** Set by cancel(): the owner closed the editor and dropped this
* manager, so a save completing afterwards must schedule nothing —
* no retry, no follow-up, no indicator writes (#2437). */
private disposed = false;

constructor(saveFn: () => Promise<SaveResult>) {
this.saveFn = saveFn;
Expand Down Expand Up @@ -111,8 +115,11 @@ export class AutoSaveManager {
this.triggerSave();
}

/** Clean up all timers (called when editor closes) */
/** Clean up all timers (called when editor closes). Disposes the
* manager: a save already in flight may still resolve, but its
* completion is dropped by onSaveResult (#2437). */
cancel(): void {
this.disposed = true;
if (this.debounceTimer !== null) {
clearTimeout(this.debounceTimer);
this.debounceTimer = null;
Expand All @@ -132,6 +139,11 @@ export class AutoSaveManager {

/** Execute the save and handle the result, serialized against other saves */
private async triggerSave(): Promise<void> {
if (this.disposed) {
// Cancelled while a timer callback or completion was still
// queued: this manager is dropped, so never start a save (#2437).
return;
}
if (this.saveInFlight) {
// Coalesce into exactly one follow-up once the in-flight save settles.
this.followUpRequested = true;
Expand Down Expand Up @@ -169,6 +181,12 @@ export class AutoSaveManager {

/** Transition state based on save outcome */
private onSaveResult(result: SaveResult, startGeneration: number): void {
if (this.disposed) {
// The editor closed while this save was in flight: its completion
// must not schedule a retry or follow-up, flip state, or write
// the (now another document's) save indicator (#2437).
return;
}
if (result.ok) {
if (this.followUpRequested || this.editGeneration !== startGeneration) {
// Edits arrived after this save started (or a save was requested
Expand Down
Loading