Replay pending changes in one private document draft

This commit is contained in:
2026-11-10 17:45:34 +08:00
parent b735667e00
commit 0abb7c96ba
4 changed files with 76 additions and 23 deletions
+9 -22
View File
@@ -1,7 +1,7 @@
import { withLocalLifecycles } from './elision.ts';
import { validateCreationSizeEffect } from '../canvas-core/creation-size-plan.ts';
import type { Change, Document, Id, OperationIntent, Registry } from '../canvas-core/types.ts';
import { applyChanges, validateDocument } from '../canvas-core/document.ts';
import type { Document, Id, OperationIntent, Registry } from '../canvas-core/types.ts';
import { replayChanges, validateDocument } from '../canvas-core/document.ts';
import { equal, fault } from '../canvas-core/values.ts';
import type { LedgerSnapshot } from './ledger.ts';
@@ -53,27 +53,14 @@ export function operationCanvases(intent: OperationIntent, document: Document, a
/** Replay once, by logical sequence. A receipt can already contain a desired field value. */
export function replayPending(base: Document, ledger: LedgerSnapshot, registry: Registry): Document {
let document = withLocalLifecycles(base, ledger);
for (const record of [...ledger.operations.values()].sort((a, b) => a.intent.sequence - b.intent.sequence)) {
if (record.phase === 'resolved' || !['document.change','run.apply'].includes(record.intent.operationType)) continue;
if (record.intent.historyEffect) validateCreationSizeEffect(record.intent.historyEffect);
for (const change of record.intent.changes) {
if(change.kind==='field'){const owner=document.elements.get(change.target.id)?.ownerCanvasId;if(owner&&record.intent.canvasIds?.length&&!record.intent.canvasIds.includes(owner))throw fault('operation_context_changed',[change.target.id]);}
if (satisfied(document, change)) continue;
document = applyChanges(document, [change]);
function* pending(){
for(const record of [...ledger.operations.values()].sort((a,b)=>a.intent.sequence-b.intent.sequence)){
if(record.phase==='resolved'||!['document.change','run.apply'].includes(record.intent.operationType))continue;
if(record.intent.historyEffect)validateCreationSizeEffect(record.intent.historyEffect);
yield {changes:record.intent.changes,canvasIds:record.intent.canvasIds};
}
}
validateDocument(document, registry);
const document=replayChanges(withLocalLifecycles(base,ledger),pending());
validateDocument(document,registry);
return document;
}
function satisfied(document: Document, change: Change) {
if (change.kind === 'canvas') return equal(document.canvases.get(change.id) ?? null, change.after);
if (change.kind === 'element') return equal(document.elements.get(change.id) ?? null, change.after);
const element = document.elements.get(change.target.id);
if (!element || element.incarnation !== change.target.incarnation) return false;
if (change.field.startsWith('data.')) {
const key = change.field.slice(5);
return Object.hasOwn(element.data, key) === (change.afterPresent ?? true) && equal(element.data[key] ?? null, change.after);
}
return equal((element as unknown as Record<string, unknown>)[change.field], change.after);
}