import { Gauge } from './MetricsProxy' ; import { Proxiable } from './domain' ; import { MetricType } from '../common/MetricType' ; export abstract class CachedGauge < T > implements Gauge < T > , Proxiable { static DEFAULT_TIMEOUT = 5000 ; type = MetricType . GAUGE ; proxyMethod = [ ] ; reloadAt = Date . now ( ) ; value : T ; timeout ; constructor ( timeout ? ) { this . timeout = timeout || CachedGauge . DEFAULT_TIMEOUT ; } return protected abstract loadValue ( ) ; getValue ( ) { if ( this . shouldLoad ( ) ) { this . value = this . loadValue ( ) ; } return this . value ; } shouldLoad ( ) { let current = Date . now ( ) ; if ( current - this . reloadAt > this . timeout ) { this . reloadAt = current ; return true ; } return false ; } }