@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
45 lines (43 loc) • 1.23 kB
JavaScript
import { of } from 'rxjs';
import { tap } from 'rxjs/operators';
import { DataStore } from './data-store';
/**
* Defines a base class extended from DataStore but with additional caching cabailities
*/
export class CachedDataStore extends DataStore {
/**
* Backing subject for dataCleared property
*/
get cachedValue() {
return this.internalCachedValue;
}
internalCachedValue;
cached = false;
/**
* Initializes a new instance of CachedDataStore<TData, TStoredData>
*/
constructor() {
super();
this.rxjsLifetime.addSubscriptions(this.dataChanged.subscribe(data => this.internalCachedValue = data), this.dataCleared.subscribe(() => this.clearCache()));
}
/**
* Gets the data value from the data store
*/
get() {
if (this.cached) {
return of(this.cachedValue);
}
return super.get().pipe(tap(data => {
this.internalCachedValue = data;
this.cached = true;
}));
}
/**
* Clears the cache from this data store
*/
clearCache() {
this.cached = false;
this.internalCachedValue = null;
}
}
//# sourceMappingURL=cached-data-store.js.map