1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

remove configureObservable and simplify init

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-03-31 12:19:43 -04:00
parent 1bf4d4ce8b
commit ac95f7575f

View File

@ -25,7 +25,7 @@ export interface StorageHelperOptions<T> {
} }
export class StorageHelper<T> { export class StorageHelper<T> {
static defaultOptions: Partial<StorageHelperOptions<any>> = { static readonly defaultOptions: Partial<StorageHelperOptions<any>> = {
autoInit: true, autoInit: true,
observable: { observable: {
deep: true, deep: true,
@ -37,30 +37,45 @@ export class StorageHelper<T> {
@observable initialized = false; @observable initialized = false;
whenReady = when(() => this.initialized); whenReady = when(() => this.initialized);
get storage(): StorageAdapter<T> { public readonly storage: StorageAdapter<T>;
return this.options.storage; public readonly defaultValue: T;
}
get defaultValue(): T {
return this.options.defaultValue;
}
constructor(readonly key: string, private options: StorageHelperOptions<T>) { constructor(readonly key: string, private options: StorageHelperOptions<T>) {
this.options = { ...StorageHelper.defaultOptions, ...options }; this.data = observable.box<T>(this.options.defaultValue, {
this.configureObservable(); ...StorageHelper.defaultOptions.observable,
this.reset(); ...(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) { if (this.options.autoInit) {
this.init(); 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<T> {
return this.storage.getItem(this.key);
}
@action @action
init({ force = false } = {}) { init({ force = false } = {}) {
if (this.initialized && !force) return; if (this.initialized && !force) {
return;
}
this.loadFromStorage({ this.getItemFromStorage()
onData: (data: T) => { .then(data => {
const notEmpty = data != null; const notEmpty = data != null;
const notDefault = !this.isDefaultValue(data); const notDefault = !this.isDefaultValue(data);
@ -69,49 +84,16 @@ export class StorageHelper<T> {
} }
this.initialized = true; this.initialized = true;
}, })
onError: (error?: any) => { .catch(error => {
logger.error(`[init]: ${error}`, this); logger.error(`[load]: ${error}`, this);
}, });
});
}
private loadFromStorage(opts: { onData?(data: T): void, onError?(error?: any): void } = {}) {
let data: T | Promise<T>;
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;
} }
isDefaultValue(value: T): boolean { isDefaultValue(value: T): boolean {
return isEqual(value, this.defaultValue); return isEqual(value, this.defaultValue);
} }
@action
private configureObservable(options = this.options.observable) {
this.data = observable.box<T>(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) { protected onChange(value: T, oldValue?: T) {
if (!this.initialized) return; if (!this.initialized) return;