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
32 changes: 32 additions & 0 deletions pkgs/id/web/src/auto-save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,38 @@ describe("AutoSaveManager serialization (#1440)", () => {
expect(save.calls).toBe(2);
});

it("runs exactly one follow-up when an edit lands during a generically failing save (#1616)", async () => {
const save = deferredSaveFn();
const mgr = new AutoSaveManager(save.fn);

mgr.onContentChange();
await vi.advanceTimersByTimeAsync(AUTOSAVE_DEBOUNCE_MS);
expect(save.calls).toBe(1);
expect(mgr.state).toBe("saving");

// Edit during the flight; its debounce fires mid-flight too, so the
// follow-up is both requested and visible in the edit generation.
mgr.onContentChange();
await vi.advanceTimersByTimeAsync(AUTOSAVE_DEBOUNCE_MS);
expect(save.calls).toBe(1); // still serialized

// The save fails generically: the coalesced edit must not be stranded —
// exactly one follow-up runs.
save.resolveNext({ ok: false });
await flush();
expect(save.calls).toBe(2);
expect(save.maxInFlight).toBe(1);

// The follow-up also fails generically, with no new edits: it must stop
// in a terminal unsaved/error state — no third call, no retry loop.
save.resolveNext({ ok: false });
await flush();
expect(mgr.state).toBe("error");
await vi.advanceTimersByTimeAsync(10_000);
expect(save.calls).toBe(2);
expect(mgr.state).toBe("unsaved");
});

it("saveNow saves immediately when idle and is a no-op while saving", async () => {
const save = deferredSaveFn();
const mgr = new AutoSaveManager(save.fn);
Expand Down
16 changes: 14 additions & 2 deletions pkgs/id/web/src/auto-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ export class AutoSaveManager {
} else if (result.retryAfterMs) {
// Rate limited — schedule retry. The retry captures the document when
// it fires, so it also serves as the follow-up for any edits that
// arrived during the failed save.
// arrived during the failed save: consume the follow-up request here.
this.followUpRequested = false;
this.state = "rate-limited";
this.updateIndicator();

Expand All @@ -189,7 +190,14 @@ export class AutoSaveManager {
this.triggerSave();
}, result.retryAfterMs);
} else {
// Generic error — don't auto-retry (prevents infinite loops on network outage)
// Generic error — don't auto-retry a save that covered everything
// (prevents infinite loops on network outage). But an edit coalesced
// during this save is not covered by it: consume the request and run
// exactly one follow-up (#1616). The follow-up starts with the request
// cleared, so if it also fails generically with no new edits, it stops
// in the error state below — no retry loop.
const followUp = this.followUpRequested || this.editGeneration !== startGeneration;
this.followUpRequested = false;
this.state = "error";
this.updateIndicator();

Expand All @@ -201,6 +209,10 @@ export class AutoSaveManager {
this.updateIndicator();
}
}, 2000);

if (followUp) {
void this.triggerSave();
}
}
}

Expand Down
Loading