chore(core-api/Storage): Fixing some code review comments, so we don't have to cast some stuff in Typescript

This commit is contained in:
blam
2020-06-03 17:59:26 +02:00
parent fb32b09226
commit f32e987d15
2 changed files with 9 additions and 11 deletions
@@ -17,21 +17,21 @@
import { createApiRef } from '../ApiRef';
import { Observable } from '../../types';
export type ObservableMessage<T extends object = {}> = {
export type ObservableMessage<T = any> = {
key: string;
newValue?: T;
};
export interface StorageApi {
/**
* The names
* Create a bucket to store data in.
* @param {String} name Namespace for the storage to be stored under,
* will inherit previous namespaces too
*/
forBucket(name: string): StorageApi;
/**
* Get persistent data.
* Get the current value for persistent data, use observe$ to be notified of updates.
*
* @param {String} key Unique key associated with the data.
* @return {Object} data The data that should is stored.
@@ -46,17 +46,17 @@ export interface StorageApi {
remove(key: string): Promise<void>;
/**
* Save persistant data.
* Save persistant data, and emit messages to anyone that is using observe$ for this key
*
* @param {String} key Unique key associated with the data.
*/
set(key: string, data: any): Promise<void>;
/**
*
* Observe changes on a particular key in the bucket
* @param {String} key Unique key associated with the data
*/
observe$<T>(key: string): Observable<T>;
observe$<T>(key: string): Observable<ObservableMessage<T>>;
}
export const storageApiRef = createApiRef<StorageApi>({
@@ -48,17 +48,15 @@ export class WebStorage implements StorageApi {
this.notifyChanges({ key, newValue: undefined });
}
observe$<T>(key: string): Observable<T> {
return this.observable.filter(
({ key: messageKey }) => messageKey === key,
) as Observable<T>;
observe$<T>(key: string): Observable<ObservableMessage<T>> {
return this.observable.filter(({ key: messageKey }) => messageKey === key);
}
private getKeyName(key: string) {
return `${this.namespace}/${key}`;
}
private notifyChanges(message: ObservableMessage) {
private notifyChanges<T>(message: ObservableMessage<T>) {
for (const subscription of this.subscribers) {
subscription.next(message);
}