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
2 changes: 1 addition & 1 deletion src/client/editor/autosave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class PageAutosave {
this.publish({ remote: page });
return;
}
if (this.changed || this.state.status === 'conflict') {
if (this.dirty) {
clearTimeout(this.timer);
this.publish({
remote: page,
Expand Down
35 changes: 35 additions & 0 deletions tests/autosave.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,38 @@ it('keeps navigation guarded when typing reverts to the old content during a sav
expect(autosave.getSnapshot().status).toBe('saved');
autosave.dispose();
});
it('preserves a reverted draft when polling finds a save whose response was lost', async () => {
vi.useFakeTimers();
const pending = deferred<Page>();
const save = vi.fn<SavePage>(() => pending.promise);
const autosave = new PageAutosave(save);
autosave.receive(page);
autosave.edit({ content: 'Temporary' });
await vi.advanceTimersByTimeAsync(800);
autosave.edit({ content: 'Original' });
pending.reject(new Error('Connection lost after the server committed'));
await vi.advanceTimersByTimeAsync(0);
expect(autosave.getSnapshot()).toMatchObject({
status: 'error',
draft: { content: 'Original' },
});

autosave.receive({ ...page, content: 'Temporary', revision: 2 });
expect(autosave.getSnapshot()).toMatchObject({
status: 'conflict',
draft: { content: 'Original' },
remote: { content: 'Temporary', revision: 2 },
});
expect(autosave.dirty).toBe(true);
await vi.advanceTimersByTimeAsync(2000);
expect(save).toHaveBeenCalledTimes(1);

autosave.useLatest();
expect(autosave.getSnapshot()).toMatchObject({
status: 'saved',
draft: { content: 'Temporary' },
page: { revision: 2 },
});
expect(autosave.dirty).toBe(false);
autosave.dispose();
});
Loading