import { Observable } from 'rxjs/Observable' ; import { Subject } from 'rxjs/Subject' ; import 'rxjs/add/operator/startWith' ; import { createOnDemandCache , Notifier , NotificationMessage , setVerbose } from './caching-fns' ; export class Cacher < T > { /** Whether to log Cacher activity to console. For debugging/demos. */ static set verbose ( value ) { setVerbose ( value ) ; } ; /** Cached values expire after this period (ms) by default */ static defaultExpirationPeriod = 30000 ; /** Observable of cached values */ readonly cache : Observable < T > ; /** Observable of caching activity messages */ private notify = new Subject < NotificationMessage > ( ) ; notifications = this . notify . asObservable ( ) . startWith ( { type : '' } ) ; private updateWhen = new Subject < boolean > ( ) ; param { Observable < T > } source param { Notifier } notifier param { number } expirationPeriod constructor ( public readonly source , public readonly expirationPeriod = Cacher . defaultExpirationPeriod ) { const notifier = ( msg ) => this . notify . next ( msg ) ; this . cache = createOnDemandCache ( source , this . updateWhen , notifier , expirationPeriod ) ; } param { boolean } force update ( force = false ) { this . updateWhen . next ( force ) ; } }