import { PathCursor } from './path-cursor' ; import { LRUMap } from 'lru_map' ; export type PathCacheEvictHandler = ( cursor : PathCursor ) => void ; export class PathCache { private cache : LRUMap < number , PathCursor > ; private evictCb : PathCacheEvictHandler | null ; constructor ( size , evictCb ) { this . evictCb = evictCb ; this . cache = new LRUMap < number , PathCursor > ( size ) ; } public get ( key ) { return this . cache . get ( key ) || null ; } public reset ( ) { this . cache . clear ( ) ; } public set ( key , val ) { this . setWithEvict ( key , val ) ; } private setWithEvict ( key , value ) { let cacheSize = this . cache . size ; let cacheLimit = this . cache . limit ; let exists = this . cache . has ( key ) ; if ( cacheSize === cacheLimit && ! exists ) { let removedTuple = this . cache . shift ( ) ; if ( this . evictCb && removedTuple ) { this . evictCb ( removedTuple [ 1 ] ) ; } } this . cache . set ( key , value ) ; } }