[195b] Objekteinstellungen-Fenster: Ops-Strom statt Neuladen des Projekts
Das Hauptfenster sammelt die vom Kern-Dokument angenommenen Ops (ProjectDocument.onChange: Commit, Griff, Befehl, Import, Undo/Redo, Sprung) und schickt sie mit laufender Nummer; das Fenster führt sie ohne Verlauf nach (applyRemote = doc_apply + applyOps). Rückfall Feld-Differenz mit Vollabgleich bei Laden/Neu oder fehlenden Ops; Lücke oder abgelehnte Ops fordern den ganzen Stand an. Grossgebäude: 0.4 ms statt 56 ms je Wandänderung (Linux, vitest).
This commit is contained in:
6 files changed
+403
-71
No files matched your search
@@ -15,9 +15,10 @@ import { setSettingsArea } from "../panels/objectSettings/areaState";
|
||||
import { expandAndFocusInfoGroup } from "../panels/objectInfo/infoGroupState";
|
||||
import { setState } from "../state/appStore";
|
||||
import { setChangeSink } from "../state/changeSink";
|
||||
import { storeDocument } from "../state/historySlice";
|
||||
import {
|
||||
OBJECT_SETTINGS_PREFIX as PREFIX,
|
||||
applyObjectSettingsSync,
|
||||
ObjectSettingsReceiver,
|
||||
mirrorIntoStore,
|
||||
remoteHost,
|
||||
type ObjectSettingsSnapshot,
|
||||
@@ -50,9 +51,13 @@ export function ObjectSettingsWindowApp() {
|
||||
void (async () => {
|
||||
const { listen, emitTo } = await import("@tauri-apps/api/event");
|
||||
if (disposed) return;
|
||||
const receiver = new ObjectSettingsReceiver(storeDocument);
|
||||
const ready = () => emitTo("main", `${PREFIX}-window-ready`).catch(() => {});
|
||||
const off = await listen<ObjectSettingsSync>(`${PREFIX}-sync`, (e) => {
|
||||
const next = applyObjectSettingsSync(snap, e.payload);
|
||||
if (!next) return;
|
||||
const next = receiver.receive(e.payload);
|
||||
// Lücke in der Folge bzw. Ops abgelehnt: ganzen Stand neu anfordern.
|
||||
if (next === "resync") void ready();
|
||||
if (!next || next === "resync") return;
|
||||
mirrorIntoStore(snap, next);
|
||||
snap = next;
|
||||
const f = next.ui.focus;
|
||||
@@ -65,7 +70,7 @@ export function ObjectSettingsWindowApp() {
|
||||
});
|
||||
if (disposed) return off();
|
||||
unlisten.push(off);
|
||||
await emitTo("main", `${PREFIX}-window-ready`).catch(() => {});
|
||||
await ready();
|
||||
})();
|
||||
|
||||
const onKey = (e: KeyboardEvent) => {
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
// den Abfluss (`setChangeSink`).
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { getState, setState } from "../state/appStore";
|
||||
import { storeDocument } from "../state/historySlice";
|
||||
import { ProjectDocument } from "../core/document";
|
||||
import { buildBigSampleProject } from "../model/sampleProjectBig";
|
||||
import { applyProjectDelta, projectDelta } from "./projectDelta";
|
||||
import { setChangeSink } from "../state/changeSink";
|
||||
import { selectionInfoFor } from "../state/useSelectionInfo";
|
||||
import { buildComplexSampleProject } from "../model/sampleProjectComplex";
|
||||
@@ -10,13 +14,15 @@ import { patchItem } from "../core/opsBuild";
|
||||
import type { CoreOp } from "../core/ops";
|
||||
import { EMPTY_SELECTION } from "../state/selection";
|
||||
import {
|
||||
applyObjectSettingsSync,
|
||||
ObjectSettingsReceiver,
|
||||
ObjectSettingsSender,
|
||||
applyForwardedChange,
|
||||
hostSnapshot,
|
||||
mirrorIntoStore,
|
||||
objectSettingsSync,
|
||||
remoteHost,
|
||||
type ObjectSettingsSnapshot,
|
||||
type ObjectSettingsSync,
|
||||
} from "./objectSettingsSync";
|
||||
|
||||
const st = () => getState();
|
||||
@@ -34,46 +40,77 @@ beforeEach(() => {
|
||||
st().loadProject(buildComplexSampleProject());
|
||||
setState({ selection: EMPTY_SELECTION });
|
||||
});
|
||||
afterEach(() => setChangeSink(null));
|
||||
afterEach(() => {
|
||||
setChangeSink(null);
|
||||
for (const fn of cleanup.splice(0)) fn();
|
||||
});
|
||||
const cleanup: Array<() => void> = [];
|
||||
|
||||
/** Über IPC: JSON hin und zurück (neue Identität, `undefined` fällt weg). */
|
||||
const wire = <T>(v: T): T => JSON.parse(JSON.stringify(v)) as T;
|
||||
|
||||
/**
|
||||
* Hauptfenster (Store + Store-Dokument) und Nebenfenster (eigenes Kern-Dokument + Empfänger) über die
|
||||
* Nachrichten verbunden. `sync()` = ein gebündelter Sync wie `flush` im Hauptfenster; `lose()` verliert ihn.
|
||||
*/
|
||||
function link() {
|
||||
const sender = new ObjectSettingsSender();
|
||||
cleanup.push(storeDocument(st().project).onChange((b, a, ops) => sender.record(b, a, ops)));
|
||||
let winDoc: ProjectDocument | null = null;
|
||||
cleanup.push(() => winDoc?.close());
|
||||
const receiver = new ObjectSettingsReceiver((p) => (winDoc ??= new ProjectDocument(p)));
|
||||
const msgs: ObjectSettingsSync[] = [];
|
||||
const sync = (): ObjectSettingsSync | null => {
|
||||
const msg = sender.next(snapshot());
|
||||
if (!msg) return null;
|
||||
msgs.push(msg);
|
||||
if (receiver.receive(wire(msg)) === "resync") {
|
||||
// Fenster meldet „ready" erneut → ganzer Stand.
|
||||
sender.reset();
|
||||
return sync();
|
||||
}
|
||||
return msg;
|
||||
};
|
||||
const lose = () => sender.next(snapshot());
|
||||
/** Fenster gleicht dem Hauptfenster: Leseansicht und Kern-Dokument. */
|
||||
const expectSame = () => {
|
||||
expect(receiver.current!.project).toEqual(wire(st().project));
|
||||
expect(JSON.parse(winDoc!.json())).toEqual(JSON.parse(storeDocument(st().project).json()));
|
||||
};
|
||||
sync();
|
||||
return { sender, receiver, sync, lose, expectSame, msgs, doc: () => winDoc! };
|
||||
}
|
||||
|
||||
describe("Sync-Nachrichten [195b]", () => {
|
||||
it("erst der ganze Stand, dann nur Auswahl-Patch bei Auswahlwechsel", () => {
|
||||
const a = snapshot();
|
||||
const first = objectSettingsSync(null, a);
|
||||
expect(first && "full" in first).toBe(true);
|
||||
expect(objectSettingsSync(a, snapshot())).toBeNull();
|
||||
const sender = new ObjectSettingsSender();
|
||||
const first = sender.next(snapshot())!;
|
||||
expect("full" in first).toBe(true);
|
||||
expect(sender.next(snapshot())).toBeNull();
|
||||
|
||||
const wall = st().project.walls[0].id;
|
||||
setState({ selection: { ...EMPTY_SELECTION, walls: [wall] } });
|
||||
const b = snapshot();
|
||||
const msg = objectSettingsSync(a, b);
|
||||
expect(msg).not.toBeNull();
|
||||
expect(msg && "project" in msg ? msg.project : undefined).toBeUndefined();
|
||||
expect(msg && "ui" in msg ? Object.keys(msg.ui!.set) : []).toEqual(["selection"]);
|
||||
const msg = sender.next(snapshot())!;
|
||||
expect(msg.seq).toBe(first.seq + 1);
|
||||
expect("project" in msg || "ops" in msg).toBe(false);
|
||||
expect("ui" in msg ? Object.keys(msg.ui!.set) : []).toEqual(["selection"]);
|
||||
|
||||
// Fensterseite: aus ganzem Stand + Patch entsteht derselbe Stand.
|
||||
const mirrored = applyObjectSettingsSync(applyObjectSettingsSync(null, first!), JSON.parse(JSON.stringify(msg)));
|
||||
expect(mirrored!.ui.selection.walls).toEqual([wall]);
|
||||
const receiver = new ObjectSettingsReceiver(storeDocument);
|
||||
receiver.receive(wire(first));
|
||||
const mirrored = receiver.receive(wire(msg));
|
||||
expect(mirrored !== "resync" && mirrored!.ui.selection.walls).toEqual([wall]);
|
||||
});
|
||||
|
||||
it("Projektänderung schickt nur die geänderten Felder; Undo ebenso", () => {
|
||||
it("Rückfall ohne Ops: Projekt als Feld-Differenz (nur die geänderten Felder)", () => {
|
||||
const a = snapshot();
|
||||
const w = st().project.walls[0];
|
||||
st().applyChange((p) => patchItem(p, "walls", w.id, { height: 2.333 }));
|
||||
const b = snapshot();
|
||||
const msg = objectSettingsSync(a, b)!;
|
||||
// Wände und davon abhängige Felder (z. B. angehängte Masse), nicht das ganze Projekt.
|
||||
const msg = objectSettingsSync(a, snapshot())!;
|
||||
const fields = "project" in msg ? Object.keys(msg.project!.set) : [];
|
||||
expect(fields).toContain("walls");
|
||||
expect(fields.length).toBeLessThan(4);
|
||||
expect("ui" in msg ? msg.ui : undefined).toBeUndefined();
|
||||
|
||||
st().undo();
|
||||
const c = snapshot();
|
||||
const back = objectSettingsSync(b, c)!;
|
||||
expect("project" in back ? Object.keys(back.project!.set) : []).toContain("walls");
|
||||
const mirrored = applyObjectSettingsSync(b, JSON.parse(JSON.stringify(back)))!;
|
||||
expect(mirrored.project.walls.find((x) => x.id === w.id)!.height).toBe(w.height);
|
||||
});
|
||||
|
||||
it("Fokus-Anfrage (Taste T) geht als UI-Patch", () => {
|
||||
@@ -83,6 +120,118 @@ describe("Sync-Nachrichten [195b]", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("Ops-Strom [195b]", () => {
|
||||
it("Änderung, Undo, Redo gehen als Ops; Fenster lädt nie neu und gleicht dem Hauptfenster", () => {
|
||||
const l = link();
|
||||
const w = st().project.walls[0];
|
||||
st().applyChange((p) => patchItem(p, "walls", w.id, { height: 2.333 }));
|
||||
const m1 = l.sync()!;
|
||||
expect("ops" in m1 && !("project" in m1)).toBe(true);
|
||||
l.expectSame();
|
||||
|
||||
st().updateWall(w.id, { height: 2.4 });
|
||||
l.sync();
|
||||
st().undo();
|
||||
expect("ops" in l.sync()!).toBe(true);
|
||||
l.expectSame();
|
||||
st().undo();
|
||||
l.sync();
|
||||
expect(l.receiver.current!.project.walls.find((x) => x.id === w.id)!.height).toBe(w.height);
|
||||
st().redo();
|
||||
st().redo(); // zwei Schritte in einem Sync: Ops beider Schritte hintereinander
|
||||
expect("ops" in l.sync()!).toBe(true);
|
||||
l.expectSame();
|
||||
expect(l.receiver.fullLoads).toBe(0);
|
||||
});
|
||||
|
||||
it("gruppierte Änderungen (ein Undo-Schritt, mehrere Commits) und deren Undo", () => {
|
||||
const l = link();
|
||||
const [a, b] = st().project.walls;
|
||||
st().groupChanges(() => {
|
||||
st().updateWall(a.id, { height: 2.61 });
|
||||
st().updateWall(b.id, { height: 2.62 });
|
||||
st().applyChange((p) => patchItem(p, "walls", a.id, { height: 2.63 }));
|
||||
});
|
||||
expect("ops" in l.sync()!).toBe(true);
|
||||
l.expectSame();
|
||||
st().undo();
|
||||
l.sync();
|
||||
l.expectSame();
|
||||
expect(l.receiver.current!.project.walls.find((x) => x.id === a.id)!.height).toBe(a.height);
|
||||
expect(l.receiver.fullLoads).toBe(0);
|
||||
});
|
||||
|
||||
it("Lücke in der Folge → Fenster fordert den ganzen Stand an (Vollabgleich)", () => {
|
||||
const l = link();
|
||||
const w = st().project.walls[0];
|
||||
st().updateWall(w.id, { height: 2.71 });
|
||||
l.lose(); // Nachricht geht verloren
|
||||
st().updateWall(w.id, { height: 2.72 });
|
||||
const before = l.msgs.length;
|
||||
l.sync();
|
||||
expect(l.msgs.length).toBe(before + 2); // abgelehnte Differenz + ganzer Stand
|
||||
expect("full" in l.msgs[l.msgs.length - 1]).toBe(true);
|
||||
expect(l.receiver.fullLoads).toBe(1);
|
||||
l.expectSame();
|
||||
// danach wieder Ops
|
||||
st().updateWall(w.id, { height: 2.73 });
|
||||
expect("ops" in l.sync()!).toBe(true);
|
||||
l.expectSame();
|
||||
});
|
||||
|
||||
it("vom Fenster abgelehnte Ops → ganzen Stand anfordern (einmal)", () => {
|
||||
const l = link();
|
||||
const w = st().project.walls[0];
|
||||
st().updateWall(w.id, { height: 2.81 });
|
||||
const msg = l.sender.next(snapshot())!;
|
||||
// Fenster-Dokument spiegelt einen anderen Stand (z. B. nach Neustart dort neu geladen): Ops passen nicht.
|
||||
l.doc().load(wire(l.receiver.current!.project));
|
||||
expect(l.receiver.receive(wire(msg))).toBe("resync");
|
||||
expect(l.receiver.receive(wire({ ...msg, seq: msg.seq + 1 }))).toBeNull(); // nur einmal anfordern
|
||||
});
|
||||
|
||||
it("Projekt neu laden → Feld-Differenz mit Vollabgleich, danach wieder Ops", () => {
|
||||
const l = link();
|
||||
st().loadProject(buildComplexSampleProject());
|
||||
const msg = l.sync()!;
|
||||
expect("project" in msg && !("ops" in msg)).toBe(true);
|
||||
expect(l.receiver.fullLoads).toBe(1);
|
||||
l.expectSame();
|
||||
st().updateWall(st().project.walls[1].id, { height: 2.91 });
|
||||
expect("ops" in l.sync()!).toBe(true);
|
||||
expect(l.receiver.fullLoads).toBe(1);
|
||||
l.expectSame();
|
||||
});
|
||||
|
||||
it("Messung grosses Projekt: Ops-Strom statt Neuladen", () => {
|
||||
st().loadProject(buildBigSampleProject());
|
||||
const l = link();
|
||||
const w = st().project.walls[42];
|
||||
const runs = 5;
|
||||
let tOps = 0;
|
||||
let tFull = 0;
|
||||
for (let i = 0; i < runs; i++) {
|
||||
const prev = l.receiver.current!.project;
|
||||
st().updateWall(w.id, { height: 2.5 + i / 100 });
|
||||
let t0 = performance.now();
|
||||
l.sync();
|
||||
tOps += performance.now() - t0;
|
||||
// Bisheriger Weg: Feld-Differenz über IPC + ganzes Projekt ins Kern-Dokument laden.
|
||||
const doc = new ProjectDocument(prev);
|
||||
t0 = performance.now();
|
||||
doc.load(applyProjectDelta(prev, wire(projectDelta(prev, st().project)!)));
|
||||
tFull += performance.now() - t0;
|
||||
doc.close();
|
||||
}
|
||||
l.expectSame();
|
||||
expect(l.receiver.fullLoads).toBe(0);
|
||||
console.info(
|
||||
`[195b] ${st().project.walls.length} Wände: Sync per Ops ${(tOps / runs).toFixed(2)} ms, bisher Vollabgleich ${(tFull / runs).toFixed(2)} ms`,
|
||||
);
|
||||
expect(tOps).toBeLessThan(tFull);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Spiegel im Nebenfenster [195b]", () => {
|
||||
it("Spiegel trägt Projekt, Auswahl und Kern-Dokument (Objekt-Info rechnet darauf)", () => {
|
||||
const wall = st().project.walls[0].id;
|
||||
|
||||
@@ -5,9 +5,13 @@
|
||||
// Hauptfensters (Projekt-Leseansicht + eigenes Kern-Dokument, Auswahl, aktive Ebene) und stellt einen Panel-Host
|
||||
// bereit, dessen Wert-Felder gespiegelt sind und dessen Handler ans Hauptfenster gehen.
|
||||
//
|
||||
// Hauptfenster → Nebenfenster: „object-settings-sync" mit dem ganzen Stand (`{ full }`, nach „ready") bzw. danach
|
||||
// nur den Änderungen: Projekt als Feld-Differenz der obersten Ebene (`projectDelta`, Referenzvergleich — die
|
||||
// Leseansicht teilt unveränderte Listen), UI-Teil als `StateDelta` (Strukturvergleich, kleine Werte).
|
||||
// Hauptfenster → Nebenfenster: „object-settings-sync" mit laufender Nummer `seq` und dem ganzen Stand (`{ full }`,
|
||||
// nach „ready") bzw. danach nur den Änderungen: Projekt als Ops-Strom (`ops`, die vom Kern-Dokument des
|
||||
// Hauptfensters angenommenen Ops seit dem letzten Sync — Commit, Griff, Befehl, Import, Undo/Redo; das Fenster
|
||||
// führt sie per `doc_apply` ohne Verlauf und `applyOps` nach, `ProjectDocument.applyRemote`), nur wenn die
|
||||
// Ops-Kette fehlt (Laden/Neu, grosser Verlaufsschritt ohne Ops, Änderung vorbei am Dokument) als Feld-Differenz
|
||||
// der obersten Ebene (`projectDelta`) mit Vollabgleich des Kern-Dokuments im Fenster; UI-Teil als `StateDelta`.
|
||||
// Lücke in `seq` oder abgelehnte Ops: das Fenster meldet erneut „ready" und bekommt den ganzen Stand.
|
||||
// Nebenfenster → Hauptfenster: „object-settings-command" mit `{ name, args }`:
|
||||
// - `change` [ops, coalesceKey]: Projekt-Änderung (Store-Aktionen im Nebenfenster rechnen Ops auf dem Spiegel
|
||||
// und geben sie über `changeSink.ts` ab) → `applyChange` im Hauptfenster (Prüfung, Undo je Änderung);
|
||||
@@ -16,7 +20,8 @@
|
||||
// Der neue Stand kommt in beiden Fällen über den nächsten Sync zurück (auch nach Undo im Hauptfenster).
|
||||
|
||||
import type { Project } from "../model/types";
|
||||
import type { CoreOp } from "../core/ops";
|
||||
import { fromMarkedJson, opsToJson, type CoreOp } from "../core/ops";
|
||||
import type { ProjectDocument } from "../core/document";
|
||||
import type { PanelHostValue } from "../panels/host";
|
||||
import type { InfoGroupId } from "../panels/objectInfo/groups";
|
||||
import { getState, setState } from "../state/appStore";
|
||||
@@ -45,26 +50,152 @@ export interface ObjectSettingsSnapshot {
|
||||
ui: ObjectSettingsUi;
|
||||
}
|
||||
|
||||
export type ObjectSettingsSync = { full: ObjectSettingsSnapshot } | { project?: ProjectDelta; ui?: StateDelta };
|
||||
/** Inhalt einer Sync-Nachricht (ohne Nummer). `ops` = Op-Folge als JSON mit Nicht-endlich-Marken (`opsToJson`). */
|
||||
export type ObjectSettingsSyncBody = { full: ObjectSettingsSnapshot } | { project?: ProjectDelta; ops?: string; ui?: StateDelta };
|
||||
|
||||
/** Nachricht von `prev` (zuletzt gesendet, `null` = noch nichts) nach `next`; `null` = nichts zu senden. */
|
||||
export function objectSettingsSync(prev: ObjectSettingsSnapshot | null, next: ObjectSettingsSnapshot): ObjectSettingsSync | null {
|
||||
/** Sync-Nachricht: `seq` zählt je Nachricht um 1 (Lücke = verlorene Nachricht → Vollabgleich). */
|
||||
export type ObjectSettingsSync = { seq: number } & ObjectSettingsSyncBody;
|
||||
|
||||
/**
|
||||
* Nachricht von `prev` (zuletzt gesendet, `null` = noch nichts) nach `next`; `null` = nichts zu senden. `ops` =
|
||||
* die angenommenen Ops von `prev.project` nach `next.project` (`null` = nicht bekannt → Feld-Differenz).
|
||||
*/
|
||||
export function objectSettingsSync(
|
||||
prev: ObjectSettingsSnapshot | null,
|
||||
next: ObjectSettingsSnapshot,
|
||||
ops: readonly CoreOp[] | null = null,
|
||||
): ObjectSettingsSyncBody | null {
|
||||
if (!prev) return { full: next };
|
||||
const project = prev.project === next.project ? null : projectDelta(prev.project, next.project);
|
||||
const ui = stateDelta(prev.ui, next.ui);
|
||||
if (ui === "full") return { full: next };
|
||||
if (!project && !ui) return null;
|
||||
return { ...(project ? { project } : {}), ...(ui ? { ui } : {}) };
|
||||
const changed = prev.project !== next.project;
|
||||
const project = changed && !ops ? projectDelta(prev.project, next.project) : null;
|
||||
const opsJson = changed && ops ? opsToJson(ops) : null;
|
||||
if (!project && !opsJson && !ui) return null;
|
||||
return { ...(project ? { project } : {}), ...(opsJson ? { ops: opsJson } : {}), ...(ui ? { ui } : {}) };
|
||||
}
|
||||
|
||||
/** Fensterseite: Nachricht anwenden. Eine Differenz vor dem ersten ganzen Stand wird verworfen (`null`). */
|
||||
export function applyObjectSettingsSync(prev: ObjectSettingsSnapshot | null, msg: ObjectSettingsSync): ObjectSettingsSnapshot | null {
|
||||
if ("full" in msg) return msg.full;
|
||||
if (!prev) return null;
|
||||
return {
|
||||
project: msg.project ? applyProjectDelta(prev.project, msg.project) : prev.project,
|
||||
ui: msg.ui ? applyStateDelta(prev.ui, msg.ui) : prev.ui,
|
||||
};
|
||||
/** Eine angenommene Änderung des Kern-Dokuments (s. `ProjectDocument.onChange`). */
|
||||
interface JournalEntry {
|
||||
before: Project;
|
||||
after: Project;
|
||||
ops: readonly CoreOp[];
|
||||
}
|
||||
|
||||
/** Ab so vielen gesammelten Ops zwischen zwei Syncs lohnt der Strom nicht mehr (Feld-Differenz). */
|
||||
const MAX_STREAM_OPS = 20000;
|
||||
|
||||
/**
|
||||
* Hauptfensterseite: baut die Sync-Nachrichten. {@link record} sammelt die angenommenen Ops des Kern-Dokuments
|
||||
* seit dem letzten Sync; {@link next} schickt sie, wenn sie lückenlos vom zuletzt gesendeten zum aktuellen
|
||||
* Projekt führen, sonst die Feld-Differenz.
|
||||
*/
|
||||
export class ObjectSettingsSender {
|
||||
private sent: ObjectSettingsSnapshot | null = null;
|
||||
private seq = 0;
|
||||
private journal: JournalEntry[] | null = [];
|
||||
private journalOps = 0;
|
||||
|
||||
/** Neu anfangen (Fenster [wieder] bereit, Senden fehlgeschlagen): nächste Nachricht ist der ganze Stand. */
|
||||
reset(): void {
|
||||
this.sent = null;
|
||||
this.journal = [];
|
||||
this.journalOps = 0;
|
||||
}
|
||||
|
||||
/** Neue Leseansicht des Kern-Dokuments (Listener von `ProjectDocument.onChange`). */
|
||||
record(before: Project, after: Project, ops: readonly CoreOp[] | null): void {
|
||||
if (!this.sent || !this.journal || before === after) return;
|
||||
if (!ops || this.journalOps + ops.length > MAX_STREAM_OPS) {
|
||||
this.journal = null; // Kette unterbrochen: bis zum nächsten Sync Feld-Differenz
|
||||
return;
|
||||
}
|
||||
this.journal.push({ before, after, ops });
|
||||
this.journalOps += ops.length;
|
||||
}
|
||||
|
||||
/** Ops von `from` nach `to` aus dem Journal, `null` = keine lückenlose Kette. */
|
||||
private chain(from: Project, to: Project): CoreOp[] | null {
|
||||
const j = this.journal;
|
||||
if (!j || j.length === 0) return null;
|
||||
let at = from;
|
||||
const out: CoreOp[] = [];
|
||||
for (const e of j) {
|
||||
if (e.before !== at) return null;
|
||||
for (const op of e.ops) out.push(op);
|
||||
at = e.after;
|
||||
}
|
||||
return at === to ? out : null;
|
||||
}
|
||||
|
||||
/** Nachricht zum Stand `snap` (`null` = nichts zu senden); merkt ihn als gesendet. */
|
||||
next(snap: ObjectSettingsSnapshot): ObjectSettingsSync | null {
|
||||
const prev = this.sent;
|
||||
const ops = prev && prev.project !== snap.project ? this.chain(prev.project, snap.project) : null;
|
||||
const body = objectSettingsSync(prev, snap, ops);
|
||||
this.journal = [];
|
||||
this.journalOps = 0;
|
||||
this.sent = snap;
|
||||
if (!body) return null;
|
||||
this.seq++;
|
||||
return { seq: this.seq, ...body };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fensterseite: wendet die Nachrichten auf den Spiegel und das Kern-Dokument des Fensters an (`docFor` = das
|
||||
* Dokument, dessen Leseansicht das Projekt ist; im Fenster `storeDocument`).
|
||||
*/
|
||||
export class ObjectSettingsReceiver {
|
||||
private snap: ObjectSettingsSnapshot | null = null;
|
||||
private seq = 0;
|
||||
private requested = false;
|
||||
/** Zahl der Vollabgleiche des Kern-Dokuments (Messung, Tests). */
|
||||
fullLoads = 0;
|
||||
|
||||
constructor(private readonly docFor: (project: Project) => ProjectDocument) {}
|
||||
|
||||
get current(): ObjectSettingsSnapshot | null {
|
||||
return this.snap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Nachricht anwenden: neuer Stand, `"resync"` = Lücke bzw. Ops abgelehnt, das Hauptfenster soll den ganzen
|
||||
* Stand schicken (einmal je Lücke; bis dahin liefern Differenzen `null` = verworfen).
|
||||
*/
|
||||
receive(msg: ObjectSettingsSync): ObjectSettingsSnapshot | "resync" | null {
|
||||
if ("full" in msg) {
|
||||
const p = msg.full.project;
|
||||
this.loadDoc(p);
|
||||
this.snap = msg.full;
|
||||
this.seq = msg.seq;
|
||||
this.requested = false;
|
||||
return this.snap;
|
||||
}
|
||||
const prev = this.snap;
|
||||
let project = prev?.project;
|
||||
if (prev && project && msg.seq === this.seq + 1) {
|
||||
if (msg.ops) project = this.docFor(project).applyRemote(project, fromMarkedJson<CoreOp[]>(msg.ops)) ?? undefined;
|
||||
else if (msg.project) this.loadDoc((project = applyProjectDelta(project, msg.project)));
|
||||
} else project = undefined;
|
||||
if (!prev || !project) {
|
||||
this.snap = null;
|
||||
if (this.requested) return null;
|
||||
this.requested = true;
|
||||
return "resync";
|
||||
}
|
||||
this.seq = msg.seq;
|
||||
this.snap = { project, ui: msg.ui ? applyStateDelta(prev.ui, msg.ui) : prev.ui };
|
||||
return this.snap;
|
||||
}
|
||||
|
||||
/** Vollabgleich: `project` als Ganzes ins Kern-Dokument laden. */
|
||||
private loadDoc(project: Project): void {
|
||||
const doc = this.docFor(project);
|
||||
if (doc.current === project) return;
|
||||
doc.load(project);
|
||||
this.fullLoads++;
|
||||
}
|
||||
}
|
||||
|
||||
/** Felder, die das Nebenfenster selbst stellt (nicht JSON-tauglich bzw. je Mausschritt neu). */
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
import { useEffect, useRef } from "react";
|
||||
import { getState, subscribe } from "../state/appStore";
|
||||
import { storeDocument } from "../state/historySlice";
|
||||
import { t } from "../i18n";
|
||||
import type { NotifyKind } from "../state/notifySlice";
|
||||
import type { InfoGroupId } from "../panels/objectInfo/groups";
|
||||
@@ -16,9 +17,9 @@ import { BORDERLESS_WINDOW_CHROME } from "./windowChrome";
|
||||
import {
|
||||
OBJECT_SETTINGS_PREFIX as PREFIX,
|
||||
OBJECT_SETTINGS_WINDOW_LABEL as LABEL,
|
||||
ObjectSettingsSender,
|
||||
applyForwardedChange,
|
||||
hostSnapshot,
|
||||
objectSettingsSync,
|
||||
type ObjectSettingsSnapshot,
|
||||
} from "./objectSettingsSync";
|
||||
|
||||
@@ -66,7 +67,8 @@ export function useObjectSettingsWindowHost(host: object): void {
|
||||
if (!isTauriRuntime()) return;
|
||||
let disposed = false;
|
||||
let open = false;
|
||||
let sent: ObjectSettingsSnapshot | null = null;
|
||||
// Baut die Nachrichten; sammelt dafür die angenommenen Ops des Kern-Dokuments (Ops-Strom statt Neuladen).
|
||||
const sender = new ObjectSettingsSender();
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
const unlisten: Array<() => void> = [];
|
||||
|
||||
@@ -81,17 +83,14 @@ export function useObjectSettingsWindowHost(host: object): void {
|
||||
if (!(await WebviewWindow.getByLabel(LABEL))) {
|
||||
// Fenster zu: beim nächsten Öffnen meldet es „ready" und bekommt den ganzen Stand.
|
||||
open = false;
|
||||
sent = null;
|
||||
sender.reset();
|
||||
return;
|
||||
}
|
||||
const next = current();
|
||||
const msg = objectSettingsSync(sent, next);
|
||||
const msg = sender.next(current());
|
||||
if (!msg) return;
|
||||
sent = next;
|
||||
const { emitTo } = await import("@tauri-apps/api/event");
|
||||
await emitTo(LABEL, `${PREFIX}-sync`, msg).catch(() => {
|
||||
sent = null;
|
||||
});
|
||||
// Fehlgeschlagen: das Fenster merkt die Lücke an `seq`; der nächste Sync ist ohnehin der ganze Stand.
|
||||
await emitTo(LABEL, `${PREFIX}-sync`, msg).catch(() => sender.reset());
|
||||
};
|
||||
const schedule = () => {
|
||||
if (!open || timer !== null) return;
|
||||
@@ -108,6 +107,7 @@ export function useObjectSettingsWindowHost(host: object): void {
|
||||
last = s;
|
||||
}),
|
||||
);
|
||||
unlisten.push(storeDocument(getState().project).onChange((before, after, ops) => sender.record(before, after, ops)));
|
||||
focusListeners.add(schedule);
|
||||
unlisten.push(() => focusListeners.delete(schedule));
|
||||
|
||||
@@ -116,7 +116,7 @@ export function useObjectSettingsWindowHost(host: object): void {
|
||||
if (disposed) return;
|
||||
const offReady = await listen(`${PREFIX}-window-ready`, () => {
|
||||
open = true;
|
||||
sent = null;
|
||||
sender.reset();
|
||||
void flush();
|
||||
});
|
||||
const offCommand = await listen<{ name: string; args?: unknown[] }>(`${PREFIX}-command`, (e) => {
|
||||
|
||||
Reference in new issue
Block a user