docs(tests): clarify scoped subscriptions and resource lifecycle assertions
This commit is contained in:
@@ -7,6 +7,7 @@ import {createElementSources} from '../../src/lib/canvas-bindings/element-source
|
||||
import {createSelectorStore} from '../../src/lib/canvas-bindings/selectors.ts';
|
||||
/** 按count建元素表;lookup区分present/unloaded/absent并记录ID,edit携带field变化,emit可模拟整根替换。 */
|
||||
function fixture(count=3){
|
||||
// 元素故意只有订阅器需要的字段;这个夹具不是可通过完整文档 schema 校验的业务模型。
|
||||
const element=id=>({id,incarnation:1,ownerCanvasId:'root',data:{title:id}}),document={elements:new Map(Array.from({length:count},(_,index)=>{const id=String(index);return[id,element(id)];}))};let root={document,runtime:{acknowledged:document}},throwSubscribe=false;const listeners=new Set(),lookups=[];
|
||||
// throwSubscribe模拟启动失败,lookups观察实际读过哪些ID;订阅返回的清理函数只移除对应监听。
|
||||
const ports={getSnapshot:()=>root,subscribe(fn){if(throwSubscribe)throw Error('subscribe failed');listeners.add(fn);return()=>listeners.delete(fn);},lookup(snapshot,id){lookups.push(id);const element=snapshot.document.elements.get(id);return element?{status:'present',element}:{status:snapshot.runtime.loading?'unloaded':'absent'};}};
|
||||
@@ -24,6 +25,7 @@ test('one ID edit among 1000 subscriptions evaluates only that target; runtime-o
|
||||
// ACK没有changes清单但替换document:应检查活跃目标,深相等克隆保持0原快照,真正改1仍通知。
|
||||
test('ACK replacement checks active targets, ignores equal cloned records and delivers unlisted changes',()=>{
|
||||
const f=fixture(),sources=createElementSources(f.ports),seen=[];const a=sources.forElement('0'),b=sources.forElement('1'),stopA=a.subscribe(()=>seen.push('0')),stopB=b.subscribe(()=>seen.push('1')),old=a.getSnapshot();
|
||||
// 不提供 changes 时用整个新文档模拟 canonical 替换;0 的克隆等价、1 的正文变化,分别检查复用和通知。
|
||||
const elements=new Map([...f.root().document.elements].map(([id,row])=>[id,structuredClone(row)]));elements.get('1').data.title='canonical';f.emit({...f.root(),document:{elements}});assert.deepEqual(seen,['1']);assert.strictEqual(a.getSnapshot(),old);assert.equal(b.getSnapshot().element.data.title,'canonical');stopA();stopB();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user