From f32e987d1546b1409dc28bd09f8ed0e2f3327253 Mon Sep 17 00:00:00 2001 From: blam Date: Wed, 3 Jun 2020 17:59:26 +0200 Subject: [PATCH] chore(core-api/Storage): Fixing some code review comments, so we don't have to cast some stuff in Typescript --- packages/core-api/src/apis/definitions/StorageApi.ts | 12 ++++++------ .../apis/implementations/StorageApi/WebStorage.ts | 8 +++----- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/core-api/src/apis/definitions/StorageApi.ts b/packages/core-api/src/apis/definitions/StorageApi.ts index 90c1961588..7d7533f5b4 100644 --- a/packages/core-api/src/apis/definitions/StorageApi.ts +++ b/packages/core-api/src/apis/definitions/StorageApi.ts @@ -17,21 +17,21 @@ import { createApiRef } from '../ApiRef'; import { Observable } from '../../types'; -export type ObservableMessage = { +export type ObservableMessage = { 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; /** - * 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; /** - * + * Observe changes on a particular key in the bucket * @param {String} key Unique key associated with the data */ - observe$(key: string): Observable; + observe$(key: string): Observable>; } export const storageApiRef = createApiRef({ diff --git a/packages/core-api/src/apis/implementations/StorageApi/WebStorage.ts b/packages/core-api/src/apis/implementations/StorageApi/WebStorage.ts index a7150b3647..c9665253de 100644 --- a/packages/core-api/src/apis/implementations/StorageApi/WebStorage.ts +++ b/packages/core-api/src/apis/implementations/StorageApi/WebStorage.ts @@ -48,17 +48,15 @@ export class WebStorage implements StorageApi { this.notifyChanges({ key, newValue: undefined }); } - observe$(key: string): Observable { - return this.observable.filter( - ({ key: messageKey }) => messageKey === key, - ) as Observable; + observe$(key: string): Observable> { + return this.observable.filter(({ key: messageKey }) => messageKey === key); } private getKeyName(key: string) { return `${this.namespace}/${key}`; } - private notifyChanges(message: ObservableMessage) { + private notifyChanges(message: ObservableMessage) { for (const subscription of this.subscribers) { subscription.next(message); }