fix: the autosave writes what is pending when the page is hidden

The 1 s debounce lost a tab closed inside it. Writes now queue behind each
other, so an older write finishing late can neither store an older document
nor sweep the pictures a newer one added; a page revived from the
back/forward cache writes again.
This commit is contained in:
Steffen Becker committed 2026-09-04 19:30:12 +02:00
1 parent 57b543806c
commit 137fce047a
3 files changed
+75 -15

No files matched your search

+28
View File
@@ -37,3 +37,31 @@ describe('autosave crash recovery', () => {
expect(await loadDocument()).toEqual({ type: 'doc' });
});
});
// The debounce is a second; a tab closed inside it would lose that second. Hiding
// the page writes what is pending at once, without the IndexedDB round trip.
describe('autosave flush on pagehide', () => {
beforeEach(() => {
localStorage.clear();
vi.stubGlobal('alert', () => {});
});
it('writes the pending document when the page is hidden', () => {
vi.useFakeTimers();
saveDocument({ type: 'doc', content: [{ type: 'paragraph' }] });
expect(localStorage.getItem('edentext-doc')).toBeNull();
window.dispatchEvent(new Event('pagehide'));
expect(JSON.parse(localStorage.getItem('edentext-doc')!)).toEqual({ type: 'doc', content: [{ type: 'paragraph' }] });
vi.useRealTimers();
});
it('writes nothing when nothing is pending', async () => {
vi.useFakeTimers();
saveDocument({ type: 'doc' });
await vi.runAllTimersAsync();
localStorage.removeItem('edentext-doc');
window.dispatchEvent(new Event('pagehide'));
expect(localStorage.getItem('edentext-doc')).toBeNull();
vi.useRealTimers();
});
});