@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
105 lines (103 loc) • 3.64 kB
JavaScript
import { Subject } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { LogLevel } from '../../diagnostics/log-level';
import { Logging } from '../../diagnostics/logging';
import { RxjsLifetimeManager } from '../rxjs-lifetime-manager';
/**
* Defines a base class for interchangeable single value data stores
* This class is intended to be an abstraction from getting, setting, and clearing a value from a data source.
* Unlike a "database" the idea here is that the store is concerned with a single value from an underlying storage mechanism.
* This mechanism could a database, a REST API, browser storage, even a variable in memory. The point is to abstract the storage
* implementation away from the management of the value
*/
export class DataStore {
/**
* Container for active subscriptions that should be cleaned up in the OnDestroy call.
*/
rxjsLifetime;
/**
* Observable that emits whenever the data has changed
*/
dataChanged;
/**
* Backing subject for dataChanged property
*/
dataChangedSubject;
/**
* Observable that emits whenever the data has been cleared
*/
dataCleared;
/**
* Backing subject for dataCleared property
*/
dataClearedSubject;
/**
* Initializes a new instance of DataStore<T>
*/
constructor() {
this.dataChangedSubject = new Subject();
this.dataChanged = this.dataChangedSubject.asObservable();
this.dataClearedSubject = new Subject();
this.dataCleared = this.dataClearedSubject.asObservable();
this.rxjsLifetime = new RxjsLifetimeManager();
this.rxjsLifetime.addSubjects(this.dataChangedSubject, this.dataClearedSubject);
}
/**
* Shortcut to log a record. The source name is automatically picked up from the class instance
* @param message the message of the log record
* @param level (optional) the log level (defaults to Debug)
* @param params (optional) the parameters to log
* @param source (optional) the source of the log message. Defaults to the name of the constructor that instantiated this instance
* @return Promise<any> settle to resolve if buffered.
*/
log(message, level = LogLevel.Debug, params, source = this.logSourceName) {
return Logging.log({
level: level,
message: message,
localParams: params,
source: source
});
}
/**
* Disposes this data store, freeing any resources consumed by this instance
*/
dispose() {
this.rxjsLifetime.dispose();
}
/**
* Gets the data value from the data store
*/
get() {
return this.getData().pipe(map(storedData => this.transformFromStore(storedData)));
}
/**
* Sets the data value in the data store
*/
set(data) {
const storedData = this.transformToStore(data);
return this.setData(storedData).pipe(tap(() => {
this.dataChangedSubject.next(data);
}));
}
/**
* clears the data value, effectively removing the value from the backing storage mechanism
*/
clear() {
return this.clearData().pipe(tap(() => {
this.dataClearedSubject.next();
}));
}
/**
* Transforms data in preparation for storage. Default behavior is no operation
*/
transformToStore(data) {
return data;
}
/**
* Transforms data in preparation for usage. Default behavior is no operation
*/
transformFromStore(storedData) {
return storedData;
}
}
//# sourceMappingURL=data-store.js.map