okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
1/** One active inference request; queued work is cancellable and never survives host exit. */ 2export class RequestQueue { 3 #pending= []; 4 #active= false ; 5constructor ( onChange = () => {}) { this . onChange = onChange ; } 6get waiting () { return this . #pending. length ; } 7run ( signal , task ) { 8return new Promise (( resolve , reject ) => { 9const entry = { signal, task, resolve, reject, abort :null }; 10entry . abort = () => { 11const index = this . #pending. indexOf ( entry ); 12if ( index < 0 ) return ; 13this . #pending. splice ( index , 1 ); 14reject ( signal . reason ?? new DOMException ( 'Request cancelled' , 'AbortError' )); 15this . onChange ( this . waiting ); 16}; 17if ( signal . aborted ) { reject ( signal . reason ); return ; } 18signal . addEventListener ( 'abort' , entry . abort , { once :true }); 19this . #pending. push ( entry ); 20this . #drain(); 21}); 22} 23async #drain() { 24if ( this . #active) { this . onChange ( this . waiting ); return ; } 25const entry = this . #pending. shift (); 26if ( ! entry ) { this . onChange ( 0 ); return ; } 27this . #active= true ; 28entry . signal . removeEventListener ( 'abort' , entry . abort ); 29this . onChange ( this . waiting ); 30try { entry . signal . throwIfAborted (); entry . resolve ( await entry . task ()); } 31catch ( error ) { entry . reject ( error ); } 32finally { this . #active= false ; this . #drain(); } 33} 34}