Skip to content
Closed
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
66 changes: 66 additions & 0 deletions pkgs/id/web/src/auto-save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
17 changes: 17 additions & 0 deletions pkgs/id/web/src/auto-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ 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<SaveResult>) {
this.saveFn = saveFn;
}

/** Called when user makes a local edit (editor:change event) */
onContentChange(): void {
if (this.disposed) return;

this.editGeneration++;

// Clear any pending timers
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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<void> {
if (this.disposed) 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 +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
Expand Down Expand Up @@ -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;

Expand Down
Loading