feat(canvas): schedule deep copy planning cooperatively
This commit is contained in:
@@ -34,12 +34,14 @@ export function commonCanvas(document: Document, left: Id, right: Id): Id {
|
||||
if (!result) throw fault('no_common_canvas', [left, right]);
|
||||
return result;
|
||||
}
|
||||
export function validateDocument(document: Document, registry: Registry): void {
|
||||
/** Cooperative validation over an immutable document; synchronous callers drain the same checks. */
|
||||
export function* validateDocumentSteps(document: Document, registry: Registry): Generator<void, void> {
|
||||
if (!document.canvases.size) throw fault('missing_root');
|
||||
for (const [id, incarnation] of document.tombstones) if (!id || !Number.isSafeInteger(incarnation) || incarnation < 1) throw fault('invalid_tombstone', [id]);
|
||||
for (const [id, incarnation] of document.tombstones) { yield; if (!id || !Number.isSafeInteger(incarnation) || incarnation < 1) throw fault('invalid_tombstone', [id]); }
|
||||
const entries = new Set<Id>();
|
||||
let roots = 0;
|
||||
for (const canvas of document.canvases.values()) {
|
||||
yield;
|
||||
if (typeof canvas.id !== 'string' || !canvas.id || !Object.hasOwn(canvas,'parent') || canvas.parent !== null && (!canvas.parent || typeof canvas.parent.canvasId !== 'string' || typeof canvas.parent.entryElementId !== 'string')) throw fault('invalid_canvas');
|
||||
ancestors(document, canvas.id);
|
||||
if (!canvas.parent) { roots++; continue; }
|
||||
@@ -49,6 +51,7 @@ export function validateDocument(document: Document, registry: Registry): void {
|
||||
}
|
||||
if (roots !== 1) throw fault('invalid_roots');
|
||||
for (const element of document.elements.values()) {
|
||||
yield;
|
||||
if (typeof element.id !== 'string' || !element.id || typeof element.componentId !== 'string' || !element.componentId || !['node','connection'].includes(element.structure) || !document.canvases.has(element.ownerCanvasId) || !Number.isSafeInteger(element.incarnation) || element.incarnation < 1 || !Number.isSafeInteger(element.dataVersion) || element.dataVersion < 1 || !element.data || typeof element.data !== 'object' || Array.isArray(element.data) || !validJson(element.data)) throw fault('invalid_element', [element.id]);
|
||||
const dead = document.tombstones.get(element.id);
|
||||
if (dead !== undefined && element.incarnation <= dead) throw fault('stale_incarnation', [element.id]);
|
||||
@@ -64,6 +67,9 @@ export function validateDocument(document: Document, registry: Registry): void {
|
||||
}
|
||||
}
|
||||
}
|
||||
export function validateDocument(document: Document, registry: Registry): void {
|
||||
for (const _ of validateDocumentSteps(document, registry)) { /* Drain identical validation without scheduling. */ }
|
||||
}
|
||||
export function applyChanges(document: Document, changes: readonly Change[], conditional = true): Document {
|
||||
const elements = new Map(document.elements), canvases = new Map(document.canvases), tombstones = new Map(document.tombstones);
|
||||
for (const change of changes) {
|
||||
|
||||
Reference in New Issue
Block a user