export interface CacheManagerListener { onCacheHit : ( path : string ) => void ; onCacheMiss : ( path : string ) => void ; onCacheRequested : ( path : string ) => void ; onCacheSaved : ( path : string ) => void ; } export class CacheManager { private cache = Object . create ( null ) ; private static listeners : CacheManagerListener [ ] = [ ] ; public getDocument ( path ) { const doc = this . cache [ path ] ; CacheManager . listeners . forEach ( ( listener ) => listener . onCacheRequested ( path ) ) ; if ( doc == undefined ) { CacheManager . listeners . forEach ( ( listener ) => listener . onCacheMiss ( path ) ) ; } else { CacheManager . listeners . forEach ( ( listener ) => listener . onCacheHit ( path ) ) ; } return doc ; } public saveDocument ( path , document ) { this . cache [ path ] = document ; CacheManager . listeners . forEach ( ( listener ) => listener . onCacheSaved ( path ) ) ; } public static addListener ( listener ) { CacheManager . listeners . push ( listener ) ; } public static removeListener ( listener ) { const index = CacheManager . listeners . indexOf ( listener ) ; CacheManager . listeners . splice ( index , 1 ) ; } public static removeAllListeners ( ) { CacheManager . listeners = [ ] ; } }