[B10][13] Autosave: nach dem Loeschen der Kopie wird ein bereits gesicherter, aber ungespeicherter Stand wieder gesichert

IndexedDB-Reihenfolge geprueft (eine Verbindung, Transaktionen in Aufrufreihenfolge): keine Race. Gefunden:
Aenderung waehrend des Speichern-Dialogs, vom Autosave gesichert, danach Kopie geloescht -> blieb ungesichert,
weil "schon geschrieben" das erneute Schreiben verhinderte. Entscheidung in createAutosaveTracker (getestet).

Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
Claude committed 2026-10-27 20:42:42 +00:00
1 parent 53e189a8e4
commit 3693378439
3 files changed
+56 -4

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
import { describe, expect, it } from "vitest";
import { createAutosaveTracker } from "./autosave";
import { sampleProject } from "../model/sampleProject";
import type { Project } from "../model/types";
const edit = (p: Project, name: string): Project => ({ ...p, name });
const empty = { ...sampleProject, walls: [], drawings2d: [], openings: [], ceilings: [], roofs: [], stairs: [], rooms: [], columns: [], extrudedSolids: [], context: [] } as Project;
describe("Autosave-Entscheidung", () => {
it("schreibt nur ungespeicherte, noch nicht gesicherte Stände mit Inhalt", () => {
const tr = createAutosaveTracker();
const saved = sampleProject;
expect(tr.shouldWrite(saved, saved)).toBe(false);
const a = edit(saved, "A");
expect(tr.shouldWrite(a, saved)).toBe(true);
expect(tr.shouldWrite(a, saved)).toBe(false); // schon gesichert
expect(tr.shouldWrite(empty, saved)).toBe(false);
});
it("nach dem Löschen der Kopie wird derselbe ungespeicherte Stand wieder gesichert", () => {
// Ablauf: Speichern beginnt mit Stand S, währenddessen Änderung A (vom Autosave gesichert),
// Speichern endet → Kopie gelöscht, gespeichert ist aber nur S. A muss wieder gesichert werden.
const tr = createAutosaveTracker();
const s = edit(sampleProject, "S");
const a = edit(s, "A");
expect(tr.shouldWrite(a, sampleProject)).toBe(true);
tr.cleared();
expect(tr.shouldWrite(a, s)).toBe(true);
});
});
+21
View File
@@ -47,3 +47,24 @@ export function hasContent(p: Project): boolean {
(p.context?.length ?? 0) > 0
);
}
/**
* Merkt sich, welcher Stand zuletzt gesichert wurde, und entscheidet je Intervall, ob geschrieben
* wird. Nach dem Löschen der Kopie (Speichern, Verwerfen) MUSS `cleared()` laufen: sonst bliebe
* ein Stand, der während des Speicherns entstand und schon einmal gesichert war, ungeschützt
* (gespeichert wurde der ältere Stand, die Kopie ist weg, und „schon gesichert" verhinderte das
* erneute Schreiben).
*/
export function createAutosaveTracker() {
let lastWritten: Project | null = null;
return {
shouldWrite(current: Project, lastSaved: Project | null): boolean {
if (current === lastSaved || current === lastWritten || !hasContent(current)) return false;
lastWritten = current;
return true;
},
cleared(): void {
lastWritten = null;
},
};
}
+6 -4
View File
@@ -9,7 +9,7 @@
import { useStore } from "./appStore";
import type { NotifyKind } from "./notifySlice";
import { parseProjectFile, parseProjectObject, serializeProject } from "../io/projectFormat";
import { AUTOSAVE_INTERVAL_MS, clearAutosave, hasContent, readAutosave, writeAutosave } from "./autosave";
import { AUTOSAVE_INTERVAL_MS, clearAutosave, createAutosaveTracker, readAutosave, writeAutosave } from "./autosave";
import type { AutosaveRecovery } from "./autosave";
import { useEffect, useRef, useState } from "react";
import type { RefObject } from "react";
@@ -198,6 +198,7 @@ export function useProjectFileActions(
const path = openedProjectPathRef.current;
setPendingDiscard(() => () => {
void clearAutosave(path);
autosaveTracker.cleared();
action();
});
} else action();
@@ -206,13 +207,12 @@ export function useProjectFileActions(
// ── Autosave + Wiederherstellen (s. state/autosave.ts) ─────────────────────
const projectRef = useRef(project);
projectRef.current = project;
const lastAutosavedRef = useRef<Project | null>(null);
const autosaveTracker = useRef(createAutosaveTracker()).current;
const [recovery, setRecovery] = useState<AutosaveRecovery | null>(null);
useEffect(() => {
const id = window.setInterval(() => {
const p = projectRef.current;
if (p === lastSavedProjectRef.current || p === lastAutosavedRef.current || !hasContent(p)) return;
lastAutosavedRef.current = p;
if (!autosaveTracker.shouldWrite(p, lastSavedProjectRef.current)) return;
void writeAutosave(openedProjectPathRef.current, p);
}, AUTOSAVE_INTERVAL_MS);
return () => window.clearInterval(id);
@@ -233,6 +233,7 @@ export function useProjectFileActions(
};
const onRecoverDiscard = () => {
if (recovery) void clearAutosave(recovery.path);
autosaveTracker.cleared();
setRecovery(null);
};
const onRecoverLater = () => setRecovery(null);
@@ -339,6 +340,7 @@ export function useProjectFileActions(
lastSavedProjectRef.current = project;
void clearAutosave(openedProjectPathRef.current);
void clearAutosave(null);
autosaveTracker.cleared();
}
return saved;
});