[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
@@ -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). */
|
||||
|
||||
Reference in new issue
Block a user