@@ -584,77 +584,6 @@ export async function getPrimaryDbscheme(
584584 return dbscheme ;
585585}
586586
587- /**
588- * A cached mapping from strings to value of type U.
589- */
590- export class CachedOperation < U > {
591- private readonly operation : ( t : string , ...args : any [ ] ) => Promise < U > ;
592- private readonly cached : Map < string , U > ;
593- private readonly lru : string [ ] ;
594- private readonly inProgressCallbacks : Map <
595- string ,
596- Array < [ ( u : U ) => void , ( reason ?: any ) => void ] >
597- > ;
598-
599- constructor (
600- operation : ( t : string , ...args : any [ ] ) => Promise < U > ,
601- private cacheSize = 100 ,
602- ) {
603- this . operation = operation ;
604- this . lru = [ ] ;
605- this . inProgressCallbacks = new Map <
606- string ,
607- Array < [ ( u : U ) => void , ( reason ?: any ) => void ] >
608- > ( ) ;
609- this . cached = new Map < string , U > ( ) ;
610- }
611-
612- async get ( t : string , ...args : any [ ] ) : Promise < U > {
613- // Try and retrieve from the cache
614- const fromCache = this . cached . get ( t ) ;
615- if ( fromCache !== undefined ) {
616- // Move to end of lru list
617- this . lru . push (
618- this . lru . splice (
619- this . lru . findIndex ( ( v ) => v === t ) ,
620- 1 ,
621- ) [ 0 ] ,
622- ) ;
623- return fromCache ;
624- }
625- // Otherwise check if in progress
626- const inProgressCallback = this . inProgressCallbacks . get ( t ) ;
627- if ( inProgressCallback !== undefined ) {
628- // If so wait for it to resolve
629- return await new Promise ( ( resolve , reject ) => {
630- inProgressCallback . push ( [ resolve , reject ] ) ;
631- } ) ;
632- }
633-
634- // Otherwise compute the new value, but leave a callback to allow sharing work
635- const callbacks : Array < [ ( u : U ) => void , ( reason ?: any ) => void ] > = [ ] ;
636- this . inProgressCallbacks . set ( t , callbacks ) ;
637- try {
638- const result = await this . operation ( t , ...args ) ;
639- callbacks . forEach ( ( f ) => f [ 0 ] ( result ) ) ;
640- this . inProgressCallbacks . delete ( t ) ;
641- if ( this . lru . length > this . cacheSize ) {
642- const toRemove = this . lru . shift ( ) ! ;
643- this . cached . delete ( toRemove ) ;
644- }
645- this . lru . push ( t ) ;
646- this . cached . set ( t , result ) ;
647- return result ;
648- } catch ( e ) {
649- // Rethrow error on all callbacks
650- callbacks . forEach ( ( f ) => f [ 1 ] ( e ) ) ;
651- throw e ;
652- } finally {
653- this . inProgressCallbacks . delete ( t ) ;
654- }
655- }
656- }
657-
658587/**
659588 * The following functions al heuristically determine metadata about databases.
660589 */
0 commit comments