Resolve default image sizes atomically with creation history and recovery
This commit is contained in:
@@ -7,6 +7,7 @@ import { rebaseIntent, type IntentContext } from './change-rebase.ts';
|
||||
import { sameHistory, type HistoryReplay, type HistoryReplayEvent } from './history-replay.ts';
|
||||
import { historyEventSource } from './history-event-source.ts';
|
||||
import type { EngineSnapshot } from './contracts.ts';
|
||||
import { planCreationSize, validateCreationSizeEffect } from '../canvas-core/creation-size-plan.ts';
|
||||
|
||||
/** Native history events are reconstructed from evidence; all cursors remain private until the recovery root is accepted. */
|
||||
export function createHistoryJournalRecovery(snapshot:EngineSnapshot,pending:readonly OperationIntent[],registry:Registry,limit:number){
|
||||
@@ -21,6 +22,7 @@ export function createHistoryJournalRecovery(snapshot:EngineSnapshot,pending:rea
|
||||
const baseline=history,replayEvents:HistoryReplayEvent[]=[],adoptionRevision=snapshot.rootRevision+1;
|
||||
function recordStep(before:HistoryState,entryId:Id|undefined,operationId?:Id){const transition=describeHistoryTransition(before,history,adoptionRevision,entryId,!!operationId);if(transition)replayEvents.push(operationId?freeze({kind:'operation' as const,operationId,transition}):freeze({kind:'adjustment' as const,transition}));}
|
||||
let next=0;const touched=new Set<Id>(),retired=new Set<Id>();let remap:((change:Change)=>Change)|undefined;
|
||||
let sizeHistory: HistoryState | undefined;
|
||||
const state=(undo:readonly Id[],redo:readonly Id[],entries:ReadonlyMap<Id,HistoryEntry>)=>Object.freeze({undo:freeze([...undo]),redo:freeze([...redo]),entries:readMap(entries)});
|
||||
const eventFor=(intent:OperationIntent)=>{const event=events[next];if(!event||event.record?.intent.id!==intent.id)throw fault('recovery_history_event_order',[intent.id]);return event;};
|
||||
function reviewPrefix(entry:HistoryEntry,document:Document):readonly Change[]{
|
||||
@@ -61,7 +63,7 @@ export function createHistoryJournalRecovery(snapshot:EngineSnapshot,pending:rea
|
||||
if(!record)applyAdjustment(event);
|
||||
else{
|
||||
if(pendingIds.has(record.intent.id))throw fault('recovery_history_event_order',[record.intent.id]);if(!['applied','elided'].includes(record.resolution??''))throw fault('recovery_history_event_unresolved',[record.intent.id]);
|
||||
if(record.intent.origin==='user')addUser(record.intent,record.intent.changes,record.intent.id,false);else if(['undo','redo'].includes(record.intent.origin))addConfirmedTraversal(record.intent);else throw fault('recovery_history_event_origin_unsupported',[record.intent.id]);
|
||||
if(record.intent.historyEffect)addConfirmedSize(record.intent);else if(record.intent.origin==='user')addUser(record.intent,record.intent.changes,record.intent.id,false);else if(['undo','redo'].includes(record.intent.origin))addConfirmedTraversal(record.intent);else throw fault('recovery_history_event_origin_unsupported',[record.intent.id]);
|
||||
}
|
||||
recordStep(before,event.transition.entryId,record?.intent.id);next++;
|
||||
}
|
||||
@@ -81,9 +83,27 @@ export function createHistoryJournalRecovery(snapshot:EngineSnapshot,pending:rea
|
||||
const entries=new Map(history.entries),map=incarnationRemap(before,after);for(const[otherId,other]of entries)if(otherId!==id)entries.set(otherId,freeze({...other,changes:other.changes.map(map)}));
|
||||
entries.set(id,freeze({...entry,changes,operationIds:[...entry.operationIds,intent.id]}));source.pop();destination.push(id);history=state(undo,redo,entries);
|
||||
}
|
||||
function addConfirmedSize(intent: OperationIntent) {
|
||||
validateCreationSizeEffect(intent.historyEffect);
|
||||
const event = eventFor(intent), entries = new Map(history.entries);
|
||||
if (event.transition.entryId !== intent.historyEffect.entryId || !equal(event.before!.undo, event.after!.undo) || !equal(event.before!.redo, event.after!.redo)) throw fault('recovery_history_size_conflict');
|
||||
for (const patch of event.transition.entries) {
|
||||
const current = entries.get(patch.id), before = event.before!.entries.get(patch.id), after = event.after!.entries.get(patch.id);
|
||||
if (!current || !before || !after || !equal(current.changes, before.changes) || current.label !== before.label || current.groupId !== before.groupId) throw fault('recovery_history_size_conflict', [patch.id]);
|
||||
entries.set(patch.id, freeze({ ...current, changes: after.changes, operationIds: patch.id === intent.historyEffect.entryId ? [...current.operationIds, intent.id] : current.operationIds }));
|
||||
}
|
||||
history = state(history.undo, history.redo, entries);
|
||||
}
|
||||
return{
|
||||
project(document:Document,intent:OperationIntent,context:IntentContext,keep:boolean){
|
||||
if(!intent.changes.length)return{document,changes:[] as readonly Change[]};advance(intent);const event=eventFor(intent),id=event.transition.entryId;remap=undefined;
|
||||
if(!intent.changes.length)return{document,changes:[] as readonly Change[]};advance(intent);const event=eventFor(intent),id=event.transition.entryId;remap=undefined;sizeHistory=undefined;
|
||||
if (intent.historyEffect) {
|
||||
validateCreationSizeEffect(intent.historyEffect);
|
||||
if (!keep) return { document, changes: [] as readonly Change[] };
|
||||
const effect = intent.historyEffect, plan = planCreationSize(document, history, registry, effect.entryId, effect.target, effect.size);
|
||||
sizeHistory = plan.history;
|
||||
return { document: plan.document, changes: plan.changes };
|
||||
}
|
||||
if(['undo','redo'].includes(intent.origin)){
|
||||
if(!id)throw fault('recovery_history_traversal_order');const entry=history.entries.get(id);
|
||||
if(!keep){
|
||||
@@ -100,7 +120,17 @@ export function createHistoryJournalRecovery(snapshot:EngineSnapshot,pending:rea
|
||||
},
|
||||
accept(intent:OperationIntent,replacement:OperationIntent|undefined,keep:boolean,before:Document,after:Document){
|
||||
if(!intent.changes.length)return;const previous=history,event=eventFor(intent),id=event.transition.entryId;
|
||||
if(intent.origin==='user'){if(replacement)addUser(intent,replacement.changes,replacement.id,true,before);else skipUser(intent,before);}
|
||||
if (intent.historyEffect) {
|
||||
if (keep) {
|
||||
if (!sizeHistory) throw fault('recovery_history_size_conflict');
|
||||
const entries = new Map(sizeHistory.entries), owner = entries.get(intent.historyEffect.entryId);
|
||||
if (!owner) throw fault('recovery_history_size_conflict');
|
||||
if (replacement) entries.set(owner.id, freeze({ ...owner, operationIds: [...owner.operationIds, replacement.id] }));
|
||||
for (const [entryId, entry] of entries) if (!equal(entry, history.entries.get(entryId))) touched.add(entryId);
|
||||
history = state(sizeHistory.undo, sizeHistory.redo, entries);
|
||||
}
|
||||
}
|
||||
else if(intent.origin==='user'){if(replacement)addUser(intent,replacement.changes,replacement.id,true,before);else skipUser(intent,before);}
|
||||
else if(['undo','redo'].includes(intent.origin)&&id&&keep){
|
||||
const entries=new Map(history.entries),undo=[...history.undo],redo=[...history.redo],source=intent.origin==='undo'?undo:redo,destination=intent.origin==='undo'?redo:undo,entry=entries.get(id)!;source.pop();
|
||||
if(replacement){const changes=intent.origin==='undo'?invert(replacement.changes):replacement.changes;try{planHistoryChanges(after,intent.origin==='undo'?changes:invert(changes),registry);}catch{throw fault('recovery_history_traversal_conflict',[id]);}if(remap)for(const[otherId,other]of entries)if(otherId!==id)entries.set(otherId,freeze({...other,changes:other.changes.map(remap)}));entries.set(id,freeze({...entry,changes,operationIds:[...entry.operationIds,replacement.id]}));destination.push(id);}else{entries.delete(id);retired.add(id);}
|
||||
|
||||
Reference in New Issue
Block a user