[K4] S1: Projekt-Dokument im Rust-Kern (document.rs), TS-Spiegel ueber Ops (core/document.ts), Store haelt es aktuell
This commit is contained in:
1 parent
1c3f38e9ab
commit
8a120329ee
10 files changed
+546
-1
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { ProjectDocument } from "./document";
|
||||
import { sampleProject } from "../model/sampleProject";
|
||||
import type { Project } from "../model/types";
|
||||
|
||||
const plain = (p: Project): unknown => JSON.parse(JSON.stringify(p));
|
||||
|
||||
function mutate(p: Project, seed: number): Project {
|
||||
let x = seed;
|
||||
const rnd = () => ((x = (x * 1664525 + 1013904223) % 4294967296) / 4294967296);
|
||||
let out = { ...p };
|
||||
const walls = [...out.walls];
|
||||
if (rnd() < 0.5 && walls.length) walls.splice(Math.floor(rnd() * walls.length), 1);
|
||||
if (rnd() < 0.7 && walls.length) {
|
||||
const i = Math.floor(rnd() * walls.length);
|
||||
walls[i] = { ...walls[i], height: (walls[i].height ?? 2.6) + rnd() };
|
||||
}
|
||||
if (rnd() < 0.6) walls.splice(Math.floor(rnd() * (walls.length + 1)), 0, { ...p.walls[0], id: `new-${seed}-a` }, { ...p.walls[0], id: `new-${seed}-b` });
|
||||
if (rnd() < 0.4) walls.reverse();
|
||||
out = { ...out, walls };
|
||||
if (rnd() < 0.5) out = { ...out, name: `n${seed}` };
|
||||
if (rnd() < 0.3) out = { ...out, referenceElevationMasl: rnd() * 500 };
|
||||
if (rnd() < 0.3) out = { ...out, geoAnchor: undefined };
|
||||
if (rnd() < 0.5) out = { ...out, drawings2d: [] };
|
||||
return out;
|
||||
}
|
||||
|
||||
describe("ProjectDocument (Kern-Spiegel)", () => {
|
||||
it("öffnet das Projekt unverändert", () => {
|
||||
const d = new ProjectDocument(sampleProject);
|
||||
expect(JSON.parse(d.json())).toEqual(plain(sampleProject));
|
||||
d.close();
|
||||
});
|
||||
|
||||
it("folgt beliebigen Änderungen über Ops", () => {
|
||||
const d = new ProjectDocument(sampleProject);
|
||||
let cur = sampleProject;
|
||||
for (let seed = 1; seed <= 300; seed++) {
|
||||
cur = mutate(cur, seed);
|
||||
d.sync(cur);
|
||||
const got = JSON.parse(d.json()) as Record<string, unknown>;
|
||||
const want = plain(cur) as Record<string, unknown>;
|
||||
const bad = [...new Set([...Object.keys(got), ...Object.keys(want)])].filter((k) => JSON.stringify(got[k]) !== JSON.stringify(want[k]));
|
||||
expect(bad, `seed ${seed}`).toEqual([]);
|
||||
}
|
||||
d.close();
|
||||
});
|
||||
|
||||
it("heilt sich bei doppelten ids (Neuöffnen)", () => {
|
||||
const d = new ProjectDocument(sampleProject);
|
||||
const dup = { ...sampleProject, walls: [...sampleProject.walls, sampleProject.walls[0]] };
|
||||
d.sync(dup);
|
||||
expect(JSON.parse(d.json())).toEqual(plain(dup));
|
||||
d.close();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
// Projekt-Dokument im Rust-Kern ([K4], Stufe S1): spiegelt das TS-Projekt über Ops nach Rust
|
||||
// (`document.rs`). TS bleibt vorerst Quelle der Änderungen (`setProject` → `diffToOps`); Rust hält
|
||||
// den Stand, auf dem die Ableitungen später direkt rechnen, ohne dass TS das Projekt je Aufruf
|
||||
// flach neu sammeln muss. Bei jedem Widerspruch (abgelehnte Ops, Fehler) wird das Dokument aus dem
|
||||
// vollen Projekt neu geöffnet — der Spiegel heilt sich selbst.
|
||||
|
||||
import type { Project } from "../model/types";
|
||||
import { core } from "../engine/coreWasm";
|
||||
import { diffToOps } from "./ops";
|
||||
|
||||
export class ProjectDocument {
|
||||
private handle = 0;
|
||||
private mirror: Project;
|
||||
|
||||
constructor(project: Project) {
|
||||
this.mirror = project;
|
||||
this.reopen(project);
|
||||
}
|
||||
|
||||
/** Handle im Kern (0 = nicht offen). */
|
||||
get id(): number {
|
||||
return this.handle;
|
||||
}
|
||||
|
||||
private reopen(project: Project): void {
|
||||
if (this.handle) core().doc_close(this.handle);
|
||||
this.handle = core().doc_open(JSON.stringify(project));
|
||||
if (!this.handle) console.warn("Projekt-Dokument im Kern konnte nicht geöffnet werden.");
|
||||
}
|
||||
|
||||
/** Bringt das Dokument auf den Stand `next` (über die Differenz als Ops). */
|
||||
sync(next: Project): void {
|
||||
if (next === this.mirror) return;
|
||||
const ops = diffToOps(this.mirror, next);
|
||||
this.mirror = next;
|
||||
if (ops.length === 0) return;
|
||||
if (!this.handle) {
|
||||
this.reopen(next);
|
||||
return;
|
||||
}
|
||||
const problems = core().doc_apply(this.handle, JSON.stringify(ops));
|
||||
if (problems) {
|
||||
console.warn("Kern-Dokument weicht ab, wird neu geöffnet:", problems);
|
||||
this.reopen(next);
|
||||
}
|
||||
}
|
||||
|
||||
/** Das Dokument als JSON-Text (Test, Export). */
|
||||
json(): string {
|
||||
return this.handle ? core().doc_json(this.handle) : "";
|
||||
}
|
||||
|
||||
close(): void {
|
||||
if (this.handle) core().doc_close(this.handle);
|
||||
this.handle = 0;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
// Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md).
|
||||
|
||||
import { createStore } from "./store";
|
||||
import { ProjectDocument } from "../core/document";
|
||||
import type { StoreApi } from "./store";
|
||||
import { createProjectSlice } from "./projectSlice";
|
||||
import type { ProjectSlice } from "./projectSlice";
|
||||
@@ -63,3 +64,25 @@ const store = createStore<RootState>((api) => ({
|
||||
export const useStore = store.useStore;
|
||||
export const getState = store.getState;
|
||||
export const setState = store.setState;
|
||||
|
||||
// Kern-Dokument ([K4] S1): spiegelt jede Änderung von `project` (Setzen, Laden, Undo/Redo) als Ops
|
||||
// in den Rust-Kern. Ohne geladenen Kern (Start, manche Tests) bleibt es aus und holt sich beim
|
||||
// nächsten Zugriff den vollen Stand.
|
||||
let coreDoc: ProjectDocument | null = null;
|
||||
|
||||
/** Das Rust-Dokument zum aktuellen Projekt (synchron nachgezogen); `null`, solange der Kern fehlt. */
|
||||
export function projectDocument(): ProjectDocument | null {
|
||||
try {
|
||||
const p = store.getState().project;
|
||||
if (!coreDoc) coreDoc = new ProjectDocument(p);
|
||||
else coreDoc.sync(p);
|
||||
return coreDoc;
|
||||
} catch {
|
||||
coreDoc = null;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
store.subscribe(() => {
|
||||
void projectDocument();
|
||||
});
|
||||
Reference in new issue
Block a user