docs(core): clarify editor publication and history traversal contracts
This commit is contained in:
@@ -9,15 +9,17 @@ import { freeze } from './values.ts';
|
||||
|
||||
/** Pure history execution, also used to validate recovery proposals before publication. */
|
||||
/** 顺序回放一组历史修改,完成整份文档校验后返回候选文档和实际 changes。
|
||||
* remap 供调用者把其它历史目标同步映射到恢复后的 incarnation;失败时原文档不变。 */
|
||||
* remap 供调用者把其它历史目标同步映射到恢复后的 incarnation;函数不发布任何候选。
|
||||
* initial/planned 应来自已捕获的不可变状态,底层 applyChanges/freeze 不是任意外部对象的深隔离入口。 */
|
||||
export function planHistoryChanges(initial:Document,planned:readonly Change[],registry:Registry){
|
||||
// ID →(旧 incarnation → 新 incarnation);同一批恢复可能形成多级映射链。
|
||||
const generations=new Map<Id,Map<number,number>>();
|
||||
// 追踪到最终代次,避免后面的字段修改仍指向本批较早的已替换代次。
|
||||
// 追踪到本批最终代次;每次恢复分配更大代次,因此按这些内部生成的映射链前进。
|
||||
const incarnation=(id:Id,value:number)=>{const mapping=generations.get(id);while(mapping?.has(value))value=mapping.get(value)!;return value;};
|
||||
// 只改生命周期身份:字段目标与整元素 before/after 都要一致,canvas 变更原样通过。
|
||||
const remap=(change:Change):Change=>{
|
||||
if(change.kind==='field'){const next=incarnation(change.target.id,change.target.incarnation);return next===change.target.incarnation?change:freeze({...change,target:{...change.target,incarnation:next}});}
|
||||
// 整元素 before/after 只替换顶层 incarnation,记录内其它字段与引用不在此重新解释。
|
||||
if(change.kind==='element'){const record=(value:ElementRecord|null)=>value?{...value,incarnation:incarnation(value.id,value.incarnation)}:null;return freeze({...change,before:record(change.before),after:record(change.after)});}
|
||||
return change;
|
||||
};
|
||||
@@ -31,5 +33,6 @@ export function planHistoryChanges(initial:Document,planned:readonly Change[],re
|
||||
}
|
||||
// 暂态允许先恢复端点再恢复边;只有整批完成后才检查最终树、引用和 owner 一致性。
|
||||
validateDocument(document,registry);
|
||||
// restored 只表示是否建立过恢复代次映射;即使 false,文档也可能因普通字段/删除变化而改变。
|
||||
return{document,changes:freeze(changes),remap,restored:generations.size>0};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user