diff --git a/pkgs/id/web/src/auto-save.test.ts b/pkgs/id/web/src/auto-save.test.ts index 46d142f6e7..6cdd3db36b 100644 --- a/pkgs/id/web/src/auto-save.test.ts +++ b/pkgs/id/web/src/auto-save.test.ts @@ -184,6 +184,72 @@ describe("AutoSaveManager serialization (#1440)", () => { }); }); +describe("AutoSaveManager cancellation (#2437)", () => { + beforeEach(() => { + vi.useFakeTimers(); + }); + afterEach(() => { + vi.useRealTimers(); + }); + + it("does not retry when an in-flight rate-limited save settles after cancel", async () => { + const save = deferredSaveFn(); + const mgr = new AutoSaveManager(save.fn); + const updateIndicator = vi.spyOn(mgr, "updateIndicator"); + + mgr.saveNow(); + expect(save.calls).toBe(1); + mgr.cancel(); + updateIndicator.mockClear(); + + save.resolveNext({ ok: false, retryAfterMs: 5000 }); + await flush(); + await vi.advanceTimersByTimeAsync(10_000); + + expect(save.calls).toBe(1); + expect(mgr.state).toBe("idle"); + expect(updateIndicator).not.toHaveBeenCalled(); + }); + + it("does not run a success follow-up when an edit and cancel happen during a save", async () => { + const save = deferredSaveFn(); + const mgr = new AutoSaveManager(save.fn); + const updateIndicator = vi.spyOn(mgr, "updateIndicator"); + + mgr.saveNow(); + mgr.onContentChange(); + mgr.cancel(); + updateIndicator.mockClear(); + + save.resolveNext({ ok: true }); + await flush(); + await vi.advanceTimersByTimeAsync(10_000); + + expect(save.calls).toBe(1); + expect(mgr.state).toBe("idle"); + expect(updateIndicator).not.toHaveBeenCalled(); + }); + + it("does not run a generic-error follow-up when an edit and cancel happen during a save", async () => { + const save = deferredSaveFn(); + const mgr = new AutoSaveManager(save.fn); + const updateIndicator = vi.spyOn(mgr, "updateIndicator"); + + mgr.saveNow(); + mgr.onContentChange(); + mgr.cancel(); + updateIndicator.mockClear(); + + save.resolveNext({ ok: false }); + await flush(); + await vi.advanceTimersByTimeAsync(10_000); + + expect(save.calls).toBe(1); + expect(mgr.state).toBe("idle"); + expect(updateIndicator).not.toHaveBeenCalled(); + }); +}); + describe("AutoSaveManager.onNewVersion vs a pending rate-limit retry (#2466)", () => { beforeEach(() => { vi.useFakeTimers(); diff --git a/pkgs/id/web/src/auto-save.ts b/pkgs/id/web/src/auto-save.ts index 3617713231..f6245e9db7 100644 --- a/pkgs/id/web/src/auto-save.ts +++ b/pkgs/id/web/src/auto-save.ts @@ -27,6 +27,8 @@ 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; + /** The editor owning this manager closed; late completions must be ignored. */ + private disposed = false; constructor(saveFn: () => Promise) { this.saveFn = saveFn; @@ -34,6 +36,8 @@ export class AutoSaveManager { /** Called when user makes a local edit (editor:change event) */ onContentChange(): void { + if (this.disposed) return; + this.editGeneration++; // Clear any pending timers @@ -58,6 +62,8 @@ export class AutoSaveManager { /** Called when another client saves (NewVersion received) */ onNewVersion(): void { + if (this.disposed) return; + // A pending rate-limit retry means a save of THIS client's newer // snapshot was refused — with the #2466 in-flight exclusion, by the // very save whose broadcast this is — so the broadcast version cannot @@ -99,6 +105,8 @@ export class AutoSaveManager { /** Manual save — Ctrl+S or button click. Cancels debounce and saves immediately. */ saveNow(): void { + if (this.disposed) return; + // Cancel debounce timer — we're saving right now if (this.debounceTimer !== null) { clearTimeout(this.debounceTimer); @@ -113,6 +121,8 @@ export class AutoSaveManager { /** Clean up all timers (called when editor closes) */ cancel(): void { + if (this.disposed) return; + if (this.debounceTimer !== null) { clearTimeout(this.debounceTimer); this.debounceTimer = null; @@ -128,10 +138,13 @@ export class AutoSaveManager { this.followUpRequested = false; this.state = "idle"; this.updateIndicator(); + this.disposed = true; } /** Execute the save and handle the result, serialized against other saves */ private async triggerSave(): Promise { + if (this.disposed) return; + if (this.saveInFlight) { // Coalesce into exactly one follow-up once the in-flight save settles. this.followUpRequested = true; @@ -169,6 +182,8 @@ export class AutoSaveManager { /** Transition state based on save outcome */ private onSaveResult(result: SaveResult, startGeneration: number): void { + if (this.disposed) return; + if (result.ok) { if (this.followUpRequested || this.editGeneration !== startGeneration) { // Edits arrived after this save started (or a save was requested @@ -229,6 +244,8 @@ export class AutoSaveManager { /** Update the save button text/state to reflect current state */ updateIndicator(): void { + if (this.disposed) return; + const saveBtn = document.getElementById("save-btn") as HTMLButtonElement | null; if (!saveBtn) return;