From ac95f7575f4ba249434c041364274f44b3b947f6 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Wed, 31 Mar 2021 12:19:43 -0400 Subject: [PATCH] remove configureObservable and simplify init Signed-off-by: Sebastian Malton --- src/renderer/utils/storageHelper.ts | 84 ++++++++++++----------------- 1 file changed, 33 insertions(+), 51 deletions(-) diff --git a/src/renderer/utils/storageHelper.ts b/src/renderer/utils/storageHelper.ts index 9f204894c6..400c014927 100755 --- a/src/renderer/utils/storageHelper.ts +++ b/src/renderer/utils/storageHelper.ts @@ -25,7 +25,7 @@ export interface StorageHelperOptions { } export class StorageHelper { - static defaultOptions: Partial> = { + static readonly defaultOptions: Partial> = { autoInit: true, observable: { deep: true, @@ -37,30 +37,45 @@ export class StorageHelper { @observable initialized = false; whenReady = when(() => this.initialized); - get storage(): StorageAdapter { - return this.options.storage; - } - - get defaultValue(): T { - return this.options.defaultValue; - } + public readonly storage: StorageAdapter; + public readonly defaultValue: T; constructor(readonly key: string, private options: StorageHelperOptions) { - this.options = { ...StorageHelper.defaultOptions, ...options }; - this.configureObservable(); - this.reset(); + this.data = observable.box(this.options.defaultValue, { + ...StorageHelper.defaultOptions.observable, + ...(options.observable ?? {}) + }); + this.data.observe(change => { + const { newValue, oldValue } = toJS(change, { recurseEverything: true }); + + this.onChange(newValue, oldValue); + }); + + this.storage = options.storage; + this.defaultValue = options.defaultValue; if (this.options.autoInit) { this.init(); } } + /** + * This function turns an optionally synchronous call to an asynchronous call + * and correctly handles the synchronous call throwing an error + * @returns the result of this.storage.getItem + */ + private async getItemFromStorage(): Promise { + return this.storage.getItem(this.key); + } + @action init({ force = false } = {}) { - if (this.initialized && !force) return; + if (this.initialized && !force) { + return; + } - this.loadFromStorage({ - onData: (data: T) => { + this.getItemFromStorage() + .then(data => { const notEmpty = data != null; const notDefault = !this.isDefaultValue(data); @@ -69,49 +84,16 @@ export class StorageHelper { } this.initialized = true; - }, - onError: (error?: any) => { - logger.error(`[init]: ${error}`, this); - }, - }); - } - - private loadFromStorage(opts: { onData?(data: T): void, onError?(error?: any): void } = {}) { - let data: T | Promise; - - try { - data = this.storage.getItem(this.key); // sync reading from storage when exposed - - if (data instanceof Promise) { - data.then(opts.onData, opts.onError); - } else { - opts?.onData(data); - } - } catch (error) { - logger.error(`[load]: ${error}`, this); - opts?.onError(error); - } - - return data; + }) + .catch(error => { + logger.error(`[load]: ${error}`, this); + }); } isDefaultValue(value: T): boolean { return isEqual(value, this.defaultValue); } - @action - private configureObservable(options = this.options.observable) { - this.data = observable.box(this.options.defaultValue, { - ...StorageHelper.defaultOptions.observable, // inherit default observability options - ...(options ?? {}), - }); - this.data.observe(change => { - const { newValue, oldValue } = toJS(change, { recurseEverything: true }); - - this.onChange(newValue, oldValue); - }); - } - protected onChange(value: T, oldValue?: T) { if (!this.initialized) return;