import * as Events_Action from "VSS/Events/Action" ; import Service = require ( "VSS/Service" ) ; import { ICacheableQuery } from "./ICacheableQuery" ; export interface ICacheableQueryService { getCacheableQueryResult < T > ( query ) : IPromise < T > ; } /**
 * Implements a cache for queries, with a lifespan tied to the Dashboard page.
 */ export class CacheableQueryService extends Service . VssService implements ICacheableQueryService { private _cache : IDictionaryStringTo < IPromise < any > > ; constructor ( ) { super ( ) ; this . resetCache ( ) ; this . clearCacheOnDashboardAutoRefresh ( ) ; } public getCacheableQueryResult < T > ( query ) { let key = query . getKey ( ) ; let cachedResult = this . getCachedData ( key ) ; if ( cachedResult ) { return cachedResult ; } else { let resultPromise = query . runQuery ( ) ; this . setCachedData ( key , resultPromise ) ; return resultPromise ; } } /** Exposes cached promise for specified key. */ protected getCachedData ( key ) { return this . _cache [ key ] ; } /** Passes a promise to the keyed query */ protected setCachedData ( key , promise ) { this . _cache [ key ] = promise ; } /**
     * On Dashboard auto refresh, clear the cache and get fresh data
     */ private clearCacheOnDashboardAutoRefresh ( ) { Events_Action . getService ( ) . registerActionWorker ( "refreshtimer.on.refresh" , ( args , next ) => { this . resetCache ( ) ; if ( $ . isFunction ( next ) ) { next ( args ) ; } } ) ; } private resetCache ( ) { this . _cache = { } ; } }