@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
96 lines (95 loc) • 4.07 kB
TypeScript
import { Observable, Subject } from 'rxjs';
import { Disposable } from '../../data/disposable';
import { LogLevel } from '../../diagnostics/log-level';
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 declare abstract class DataStore<TData, TStoredData> implements Disposable {
/**
* The source name to use for logging
*/
protected abstract readonly logSourceName: string;
/**
* Container for active subscriptions that should be cleaned up in the OnDestroy call.
*/
protected rxjsLifetime: RxjsLifetimeManager;
/**
* Observable that emits whenever the data has changed
*/
readonly dataChanged: Observable<TData>;
/**
* Backing subject for dataChanged property
*/
protected dataChangedSubject: Subject<TData>;
/**
* Observable that emits whenever the data has been cleared
*/
readonly dataCleared: Observable<void>;
/**
* Backing subject for dataCleared property
*/
protected dataClearedSubject: Subject<void>;
/**
* Implementation to get the stored data
* Called when the consumer wants to retrieve a value from the store.
* The result of this method will be processed through the transformFromStore
* method before giving the value to the consumer
*/
protected abstract getData(): Observable<TStoredData>;
/**
* Implementation to set the stored data
* Called when the consumer wants to assign a value to the store.
* It is called after processing the value through the transformToStore method
*/
protected abstract setData(data: TStoredData): Observable<void>;
/**
* Implementation to clear the stored data.
* Called when the consumer wants to remove or destroy the data.
* This may have slightly different meaning in different contexts,
* but is generally different than simply setting the value to null (although that may be effectively the same in some scenarios)
* dataCleared will be emitted immediately after this method is executed
*/
protected abstract clearData(): Observable<void>;
/**
* Initializes a new instance of DataStore<T>
*/
constructor();
/**
* 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.
*/
protected log(message: string, level?: LogLevel, params?: any, source?: string): Promise<any>;
/**
* Disposes this data store, freeing any resources consumed by this instance
*/
dispose(): void;
/**
* Gets the data value from the data store
*/
get(): Observable<TData>;
/**
* Sets the data value in the data store
*/
set(data: TData): Observable<void>;
/**
* clears the data value, effectively removing the value from the backing storage mechanism
*/
clear(): Observable<void>;
/**
* Transforms data in preparation for storage. Default behavior is no operation
*/
protected transformToStore(data: TData): TStoredData;
/**
* Transforms data in preparation for usage. Default behavior is no operation
*/
protected transformFromStore(storedData: TStoredData): TData;
}