Remap frame port references and commit import cycles atomically
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { CanvasRecord, Change, Document, Id, Json, Registry } from '../canvas-core/types.ts';
|
||||
import { copyReferenceIds } from '../canvas-core/copy-paths.ts';
|
||||
import type { DuplicatePlan } from '../canvas-core/duplicate.ts';
|
||||
import { clone, equal, fault, validJson } from '../canvas-core/values.ts';
|
||||
import type { ImportJob, ImportLine, ImportUnit } from './imports.ts';
|
||||
@@ -34,7 +35,7 @@ export async function encodeDuplicateSource(document: Document, registry: Regist
|
||||
const record=change.after;canvas(record.ownerCanvasId);
|
||||
if(record.structure==='connection'){element(record.source.elementId);element(record.target.elementId);}
|
||||
const schema=registry.components.get(record.componentId)?.copy;if(!schema)throw fault('copy_schema_unavailable');
|
||||
for(const ref of schema.references){let value:Json|undefined=record.data;for(const part of ref.path){if(!value||typeof value!=='object'||!Object.hasOwn(value,part)){value=undefined;break;}value=(value as Record<string,Json>)[part];}if(value===undefined||value===null)continue;for(const id of ref.many?value as Json[]:[value]){if(typeof id!=='string')throw fault('invalid_copy_reference');if(ref.target==='element')element(id);else canvas(id);}}
|
||||
for(const ref of schema.references)for(const id of copyReferenceIds(record.data,ref)){if(ref.target==='element')element(id);else canvas(id);}
|
||||
}
|
||||
const key=keys.get(change.id);if(!key)throw fault('invalid_duplicate_plan');dependencies.delete(key);
|
||||
const line:CopyLine={key,change,dependencies:[...dependencies],externalElements:[...elements.values()],externalCanvases:[...canvases.values()]};
|
||||
@@ -50,14 +51,25 @@ function copyLine(line:ImportLine,job:ImportJob):CopyLine {
|
||||
if(value.change.kind==='element'&&value.change.after?.incarnation!==1)throw fault('invalid_duplicate_source');
|
||||
return value;
|
||||
}
|
||||
export function prepareDuplicateImport(line:ImportLine,job:ImportJob,reserve:(key:string)=>Id):ImportUnit {
|
||||
const value=copyLine(line,job);if(reserve(value.key)!==value.change.id)throw fault('invalid_duplicate_source');
|
||||
/** Re-check the current reference contract when decoding old durable prepared sources. */
|
||||
function assertCopyReferences(value:CopyLine,job:ImportJob,registry:Registry){
|
||||
if(value.change.kind!=='element')return;
|
||||
const record=value.change.after!,schema=registry.components.get(record.componentId)?.copy;
|
||||
if(registry.resolve(record)!=='known'||!schema)throw fault('copy_schema_unavailable');
|
||||
for(const ref of schema.references)for(const id of copyReferenceIds(record.data,ref)){
|
||||
const key=Object.keys(job.stableIds).find(key=>job.stableIds[key]===id&&key.startsWith(`${ref.target}:`));
|
||||
if(key){if(key!==value.key&&!value.dependencies.includes(key))throw fault('invalid_duplicate_source');}
|
||||
else if(ref.external!=='preserve'||!(ref.target==='element'?value.externalElements:value.externalCanvases).some(guard=>guard.id===id))throw fault('copy_external_reference',[record.id,id]);
|
||||
}
|
||||
}
|
||||
export function prepareDuplicateImport(line:ImportLine,job:ImportJob,reserve:(key:string)=>Id,registry:Registry):ImportUnit {
|
||||
const value=copyLine(line,job);assertCopyReferences(value,job,registry);if(reserve(value.key)!==value.change.id)throw fault('invalid_duplicate_source');
|
||||
for(const key of value.dependencies)if(!Object.hasOwn(job.stableIds,key))throw fault('invalid_duplicate_source');
|
||||
return{key:value.key,dependencies:value.dependencies,changes:[value.change],guard:clone(line.value!)};
|
||||
}
|
||||
/** Check at the synchronous commit boundary, never only when the source was first parsed. */
|
||||
export function assertDuplicateExternal(line:ImportLine,job:ImportJob,document:Document){
|
||||
const value=copyLine(line,job);
|
||||
export function assertDuplicateExternal(line:ImportLine,job:ImportJob,document:Document,registry:Registry){
|
||||
const value=copyLine(line,job);assertCopyReferences(value,job,registry);
|
||||
for(const guard of value.externalElements){const record=document.elements.get(guard.id);if(!record||record.incarnation!==guard.incarnation||record.ownerCanvasId!==guard.ownerCanvasId)throw fault('copy_external_changed',[guard.id]);}
|
||||
for(const guard of value.externalCanvases){const record=document.canvases.get(guard.id);if(!record||!equal(record.parent,guard.parent)||guard.parent&&document.elements.get(guard.parent.entryElementId)?.incarnation!==guard.entryIncarnation)throw fault('copy_external_changed',[guard.id]);}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user