[118] Verlauf übersteht Kern-Neustart: TS-Kopie der Schritte, nach Panic wieder eingesetzt
This commit is contained in:
7 files changed
+200
-27
No files matched your search
+29
-10
@@ -11,6 +11,7 @@ import { fromMarkedJson, toMarkedJson } from "./ops";
|
||||
import type { CoreOp } from "./ops";
|
||||
import { opsMutation } from "./opsBuild";
|
||||
import { StepStates } from "./stepStates";
|
||||
import { HistoryBackup } from "./historyBackup";
|
||||
import type { ProjectMutation } from "./opsBuild";
|
||||
import type { IfcImportResult } from "../io/ifcImport";
|
||||
import type { MeshImportReport } from "../io/meshImport";
|
||||
@@ -180,6 +181,8 @@ export class ProjectDocument {
|
||||
private limits: [number, number] | null = null;
|
||||
/** Stände grosser Verlaufsschritte (Undo/Redo ohne Ops-Rückgabe, s. `stepStates.ts`). */
|
||||
private steps = new StepStates();
|
||||
/** Kopie des Kern-Verlaufs für einen Neustart nach Panic ([118]). */
|
||||
private hist = new HistoryBackup();
|
||||
/** Gelieferte Einträge des 3D-Modells je Argument-Satz ([58], {@link model3dParts}). */
|
||||
private m3d = new Map<string, Model3dDeltaCache>();
|
||||
|
||||
@@ -198,14 +201,27 @@ export class ProjectDocument {
|
||||
/**
|
||||
* Nach einem Kern-Neustart ([118], `coreWasm.ts`): die alte Instanz samt Handle und Verlauf ist weg;
|
||||
* das Dokument öffnet sich aus der Leseansicht neu (die ist der letzte angenommene Stand — eine
|
||||
* Änderung, deren Aufruf den Panic auslöste, war noch nicht übernommen). Der Verlauf beginnt leer.
|
||||
* Änderung, deren Aufruf den Panic auslöste, war noch nicht übernommen). Der Verlauf kommt aus der
|
||||
* TS-Kopie zurück (`historyBackup.ts`), wenn sie zu dieser Leseansicht gehört; sonst beginnt er leer.
|
||||
*/
|
||||
restoreAfterRestart(): void {
|
||||
this.m3d.clear();
|
||||
const wasOpen = this.handle !== 0;
|
||||
this.handle = 0;
|
||||
this.steps.clear();
|
||||
if (wasOpen) this.reopen(this.mirror);
|
||||
this.historyKept = true;
|
||||
if (!wasOpen) return this.hist.clear();
|
||||
this.reopen(this.mirror);
|
||||
this.historyKept = this.hist.restore(this.handle, this.mirror);
|
||||
}
|
||||
|
||||
/** Ob beim letzten Kern-Neustart der Verlauf erhalten blieb ([118]; Meldung an den Benutzer). */
|
||||
historyKept = true;
|
||||
|
||||
/** Neue Leseansicht nach einer angenommenen Änderung bzw. Undo/Redo; Verlaufskopie nachführen ([118]). */
|
||||
private setMirror(project: Project): void {
|
||||
this.mirror = project;
|
||||
this.hist.sync(this.handle, project);
|
||||
}
|
||||
|
||||
private reopen(project: Project): void {
|
||||
@@ -293,6 +309,7 @@ export class ProjectDocument {
|
||||
this.steps.clear();
|
||||
this.m3d.clear();
|
||||
if (!this.handle || !core().doc_load_json(this.handle, JSON.stringify(project))) this.reopen(project);
|
||||
this.hist.sync(this.handle, project);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -308,7 +325,7 @@ export class ProjectDocument {
|
||||
| { ok: false; error: string };
|
||||
if (!r.ok) return r;
|
||||
const project = JSON.parse(core().doc_json(this.handle)) as Project;
|
||||
this.mirror = project;
|
||||
this.setMirror(project);
|
||||
return { ok: true, project, warnings: r.warnings, migratedFrom: r.migratedFrom };
|
||||
}
|
||||
|
||||
@@ -382,7 +399,7 @@ export class ProjectDocument {
|
||||
}
|
||||
const extra = this.lastExtra();
|
||||
const project = this.withExtra(applyOps(base, ops, core().doc_last_hits(this.handle)), extra);
|
||||
this.mirror = project;
|
||||
this.setMirror(project);
|
||||
this.rememberCommit(base, project, extra.length ? [...ops, ...extra] : ops);
|
||||
return { project };
|
||||
}
|
||||
@@ -403,7 +420,7 @@ export class ProjectDocument {
|
||||
if (r.ops.length === 0) return { project: base, ops: r.ops, notice: r.notice };
|
||||
const extra = this.lastExtra();
|
||||
const project = this.withExtra(applyOps(base, r.ops, core().doc_last_hits(this.handle)), extra);
|
||||
this.mirror = project;
|
||||
this.setMirror(project);
|
||||
return { project, ops: extra.length ? [...r.ops, ...extra] : r.ops, notice: r.notice };
|
||||
}
|
||||
|
||||
@@ -452,7 +469,7 @@ export class ProjectDocument {
|
||||
if (r.ops.length === 0) return { project: base, ops: r.ops };
|
||||
const extra = this.lastExtra();
|
||||
const project = this.withExtra(applyOps(base, r.ops), extra);
|
||||
this.mirror = project;
|
||||
this.setMirror(project);
|
||||
return { project, ops: extra.length ? [...r.ops, ...extra] : r.ops };
|
||||
}
|
||||
|
||||
@@ -470,7 +487,7 @@ export class ProjectDocument {
|
||||
if ("problems" in r) return r;
|
||||
const extra = r.ops.length ? this.lastExtra() : [];
|
||||
const project = r.ops.length ? this.withExtra(applyOps(base, r.ops), extra) : base;
|
||||
this.mirror = project;
|
||||
this.setMirror(project);
|
||||
const ops = extra.length ? [...r.ops, ...extra] : r.ops;
|
||||
this.rememberCommit(base, project, ops);
|
||||
return { ...r, ops, project };
|
||||
@@ -558,14 +575,14 @@ export class ProjectDocument {
|
||||
if (this.steps.has(top)) {
|
||||
if (!core().doc_jump_quiet(this.handle, !undo)) return null;
|
||||
const project = this.steps.jump(top, base, undo, this.historySeqs()[undo ? 1 : 0]);
|
||||
this.mirror = project;
|
||||
this.setMirror(project);
|
||||
return project;
|
||||
}
|
||||
const json = core()[fn](this.handle);
|
||||
if (!json) return null;
|
||||
const ops = JSON.parse(json) as CoreOp[];
|
||||
const project = applyOps(base, ops, core().doc_last_hits(this.handle));
|
||||
this.mirror = project;
|
||||
this.setMirror(project);
|
||||
// Gegenschritt (neu oben auf dem anderen Stapel) führt zwischen denselben Ständen.
|
||||
const next = this.historySeqs()[undo ? 1 : 0];
|
||||
if (undo) this.steps.remember(next, project, base, ops);
|
||||
@@ -582,7 +599,7 @@ export class ProjectDocument {
|
||||
const json = core().doc_history_jump(this.handle, target);
|
||||
if (!json) return null;
|
||||
const project = applyOps(base, JSON.parse(json) as CoreOp[], core().doc_last_hits(this.handle));
|
||||
this.mirror = project;
|
||||
this.setMirror(project);
|
||||
return project;
|
||||
}
|
||||
|
||||
@@ -630,6 +647,7 @@ export class ProjectDocument {
|
||||
clearHistory(): void {
|
||||
this.steps.clear();
|
||||
if (this.handle) core().doc_history_clear(this.handle);
|
||||
this.hist.sync(this.handle, this.mirror);
|
||||
}
|
||||
|
||||
/** Grenzen des Verlaufs (Schritte, ungefähre Bytes). */
|
||||
@@ -654,6 +672,7 @@ export class ProjectDocument {
|
||||
|
||||
endGroup(): void {
|
||||
if (this.handle) core().doc_group_end(this.handle);
|
||||
this.hist.sync(this.handle, this.mirror);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
// Kopie des Undo/Redo-Verlaufs über einen Kern-Neustart ([118]): Der Verlauf lebt im Rust-Dokument und
|
||||
// wäre nach einem Panic mit der alten WASM-Instanz weg. `ProjectDocument` gleicht diese Kopie nach jeder
|
||||
// angenommenen Änderung (und Undo/Redo) ab: der Kern liefert die Laufnummern beider Stapel
|
||||
// (`doc_history_index`), TS holt nur die Schritte, die es noch nicht kennt (`doc_history_step`, JSON je
|
||||
// Schritt, unverändert gehalten). Nach dem Neustart setzt `restore` sie in das frisch geöffnete Dokument ein
|
||||
// (`doc_history_import`) — aber nur, wenn die Kopie zur Leseansicht gehört, aus der das Dokument neu
|
||||
// geöffnet wurde. Eine Änderung, deren Aufruf den Panic auslöste, war nie angenommen: weder in der
|
||||
// Leseansicht noch im Verlauf, sie wird also nicht wiederholt.
|
||||
//
|
||||
// Speicher: nur Schritte, die der Kern selbst noch hält (gleiche Grenzen wie dort); einzelne Schritte über
|
||||
// MAX_STEP_BYTES und die ältesten über MAX_TOTAL_CHARS werden nicht kopiert — der Verlauf endet dann dort
|
||||
// (Undo bis vor den fehlenden Schritt, Redo ebenso).
|
||||
import { core } from "../engine/coreWasm";
|
||||
|
||||
/** Grösster kopierter Schritt (geschätzter Speicher im Kern, Bytes); grössere: Verlauf endet dort. */
|
||||
export const MAX_STEP_BYTES = 8 << 20;
|
||||
/** Höchstens so viele Zeichen JSON insgesamt; darüber fallen die ältesten Undo-Schritte weg. */
|
||||
export const MAX_TOTAL_CHARS = 64 << 20;
|
||||
|
||||
interface HistoryIndex {
|
||||
undo: number[];
|
||||
redo: number[];
|
||||
lastKey: string | null;
|
||||
lastAt: number;
|
||||
}
|
||||
|
||||
export class HistoryBackup {
|
||||
/** Leseansicht, zu der die Kopie gehört (Identität). */
|
||||
private at: object | null = null;
|
||||
private index: HistoryIndex | null = null;
|
||||
/** JSON je Laufnummer; `null` = nicht kopiert (zu gross). */
|
||||
private steps = new Map<number, string | null>();
|
||||
private chars = 0;
|
||||
|
||||
/** Kopie an den Verlauf von Dokument `handle` angleichen; `mirror` = dessen Leseansicht. */
|
||||
sync(handle: number, mirror: object): void {
|
||||
const json = handle ? core().doc_history_index(handle) : "";
|
||||
if (!json) return this.clear();
|
||||
const index = JSON.parse(json) as HistoryIndex;
|
||||
const live = new Set([...index.undo, ...index.redo]);
|
||||
for (const [seq, s] of this.steps)
|
||||
if (!live.has(seq)) {
|
||||
this.chars -= s?.length ?? 0;
|
||||
this.steps.delete(seq);
|
||||
}
|
||||
for (const seq of live)
|
||||
if (!this.steps.has(seq)) {
|
||||
const s = core().doc_history_step(handle, seq, MAX_STEP_BYTES) || null;
|
||||
this.chars += s?.length ?? 0;
|
||||
this.steps.set(seq, s);
|
||||
}
|
||||
for (const seq of index.undo) {
|
||||
if (this.chars <= MAX_TOTAL_CHARS) break;
|
||||
this.chars -= this.steps.get(seq)?.length ?? 0;
|
||||
this.steps.set(seq, null);
|
||||
}
|
||||
this.index = index;
|
||||
this.at = mirror;
|
||||
}
|
||||
|
||||
/**
|
||||
* Nach dem Neustart: Verlauf in Dokument `handle` einsetzen, wenn die Kopie zu `mirror` gehört (das
|
||||
* Dokument wurde aus `mirror` geöffnet). `true` = eingesetzt (oder nichts einzusetzen).
|
||||
*/
|
||||
restore(handle: number, mirror: object): boolean {
|
||||
const index = this.index;
|
||||
const ok = !index || (this.at === mirror && handle !== 0 && core().doc_history_import(handle, this.importJson(index)));
|
||||
this.clear();
|
||||
if (ok) this.sync(handle, mirror); // neue Laufnummern
|
||||
return ok;
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.at = null;
|
||||
this.index = null;
|
||||
this.steps.clear();
|
||||
this.chars = 0;
|
||||
}
|
||||
|
||||
/** Stapel bis zum jüngsten fehlenden Schritt abgeschnitten (über ihn führt kein Undo/Redo). */
|
||||
private importJson(index: HistoryIndex): string {
|
||||
const stack = (seqs: number[]) => {
|
||||
let from = 0;
|
||||
seqs.forEach((s, i) => {
|
||||
if (!this.steps.get(s)) from = i + 1;
|
||||
});
|
||||
return seqs.slice(from).map((s) => this.steps.get(s) as string);
|
||||
};
|
||||
return `{"undo":[${stack(index.undo).join(",")}],"redo":[${stack(index.redo).join(",")}],"lastKey":${JSON.stringify(index.lastKey)},"lastAt":${JSON.stringify(index.lastAt)}}`;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user