Coalesce background Root notifications without delaying input updates
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import type {ExternalSource} from './selectors.ts';
|
||||
/** Presentation invalidations only. Commands, history and network work never wait here. */
|
||||
export type FrameClock={request(callback:()=>void):unknown;cancel(handle:unknown):void};
|
||||
export function createFrameQueue<K>(clock:FrameClock,publish:(dirty:ReadonlySet<K>)=>void){
|
||||
@@ -9,3 +10,13 @@ export function createFrameQueue<K>(clock:FrameClock,publish:(dirty:ReadonlySet<
|
||||
dispose(){if(disposed)return;disposed=true;if(scheduled)clock.cancel(scheduled.handle);scheduled=undefined;dirty.clear();},
|
||||
});
|
||||
}
|
||||
|
||||
/** Coalesce presentation notifications; immediate invalidations flush pending work synchronously. */
|
||||
export function createFrameSource<S>(source:{getSnapshot():S;subscribe(notify:(immediate?:boolean)=>void):()=>void},clock:FrameClock):ExternalSource<S>{
|
||||
return Object.freeze({getSnapshot:()=>source.getSnapshot(),subscribe(notify:()=>void){
|
||||
const queue=createFrameQueue<number>(clock,()=>notify());
|
||||
let stop:()=>void;
|
||||
try{stop=source.subscribe(immediate=>{queue.invalidate([0]);if(immediate)queue.flush();});}catch(error){queue.dispose();throw error;}
|
||||
return()=>{try{stop();}finally{queue.dispose();}};
|
||||
}});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user