/** One immutable source, one cached selection; no second document or operation state. */ export type ExternalSource={getSnapshot():S;subscribe(notify:()=>void):()=>void}; export function createSelectorStore(source:ExternalSource,selector:(snapshot:S)=>T,isEqual:(a:T,b:T)=>boolean=Object.is){ let initialized=false,snapshot:S,selection:T; function getSnapshot():T{ const current=source.getSnapshot();if(initialized&&Object.is(snapshot,current))return selection; const next=selector(current),same=initialized&&isEqual(selection,next); snapshot=current;if(!same)selection=next;initialized=true;return selection; } return Object.freeze({getSnapshot,subscribe(notify:()=>void){let observed=getSnapshot();return source.subscribe(()=>{let next:T;try{next=getSnapshot();}catch{notify();return;}if(!Object.is(observed,next)){observed=next;notify();}});}}); }