mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fix createStorage initializing twice
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
c7e1ab27fd
commit
1b8ab6d430
@ -26,7 +26,7 @@ describe("renderer/utils/StorageHelper", () => {
|
||||
expect(storageHelper.defaultValue).toBe("test");
|
||||
expect(storageHelper.get()).toBe("test");
|
||||
|
||||
await storageHelper.init();
|
||||
storageHelper.init();
|
||||
|
||||
expect(storageHelper.key).toBe(storageKey);
|
||||
expect(storageHelper.defaultValue).toBe("test");
|
||||
|
||||
@ -5,6 +5,7 @@ import { action, comparer, observable, toJS, when } from "mobx";
|
||||
import produce, { Draft, enableMapSet, setAutoFreeze } from "immer";
|
||||
import { isEqual, isFunction, isPlainObject } from "lodash";
|
||||
import logger from "../../main/logger";
|
||||
import { IObservableValue } from "mobx/lib/internal";
|
||||
|
||||
setAutoFreeze(false); // allow to merge observables
|
||||
enableMapSet(); // allow merging maps and sets
|
||||
@ -21,7 +22,7 @@ export interface StorageHelperOptions<T> {
|
||||
autoInit?: boolean; // start preloading data immediately, default: true
|
||||
observable?: CreateObservableOptions;
|
||||
storage: StorageAdapter<T>;
|
||||
defaultValue?: T;
|
||||
defaultValue: T;
|
||||
}
|
||||
|
||||
export class StorageHelper<T> {
|
||||
@ -33,7 +34,7 @@ export class StorageHelper<T> {
|
||||
}
|
||||
};
|
||||
|
||||
@observable private data = observable.box<T>();
|
||||
private data: IObservableValue<T>;
|
||||
@observable initialized = false;
|
||||
whenReady = when(() => this.initialized);
|
||||
|
||||
@ -41,14 +42,19 @@ export class StorageHelper<T> {
|
||||
return this.options.storage;
|
||||
}
|
||||
|
||||
get defaultValue(): T {
|
||||
return this.options.defaultValue;
|
||||
}
|
||||
readonly defaultValue: T;
|
||||
|
||||
constructor(readonly key: string, private options: StorageHelperOptions<T>) {
|
||||
this.options = { ...StorageHelper.defaultOptions, ...options };
|
||||
this.configureObservable();
|
||||
this.reset();
|
||||
this.defaultValue = options.defaultValue;
|
||||
this.data = observable.box(this.defaultValue, {
|
||||
...StorageHelper.defaultOptions.observable, // inherit default observability options
|
||||
...(options.observable ?? {}),
|
||||
});
|
||||
this.data.observe(change => {
|
||||
const { newValue, oldValue } = toJS(change, { recurseEverything: true });
|
||||
|
||||
this.onChange(newValue, oldValue);
|
||||
});
|
||||
|
||||
if (this.options.autoInit) {
|
||||
this.init();
|
||||
@ -59,59 +65,40 @@ export class StorageHelper<T> {
|
||||
init({ force = false } = {}) {
|
||||
if (this.initialized && !force) return;
|
||||
|
||||
this.loadFromStorage({
|
||||
onData: (data: T) => {
|
||||
const notEmpty = data != null;
|
||||
const notDefault = !this.isDefaultValue(data);
|
||||
|
||||
if (notEmpty && notDefault) {
|
||||
this.merge(data);
|
||||
}
|
||||
|
||||
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<T>;
|
||||
|
||||
try {
|
||||
data = this.storage.getItem(this.key); // sync reading from storage when exposed
|
||||
const data = this.storage.getItem(this.key); // sync reading from storage when exposed
|
||||
|
||||
if (data instanceof Promise) {
|
||||
data.then(opts.onData, opts.onError);
|
||||
data.then(this.onData, this.onError);
|
||||
} else {
|
||||
opts?.onData(data);
|
||||
this.onData(data);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
logger.error(`[load]: ${error}`, this);
|
||||
opts?.onError(error);
|
||||
this.onError(error);
|
||||
}
|
||||
}
|
||||
|
||||
private onData = (data: T) => {
|
||||
const notEmpty = data != null;
|
||||
const notDefault = !this.isDefaultValue(data);
|
||||
|
||||
if (notEmpty && notDefault) {
|
||||
this.merge(data);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
this.initialized = true;
|
||||
};
|
||||
|
||||
private onError = (error?: any) => {
|
||||
logger.error(`[init]: ${error}`, this);
|
||||
};
|
||||
|
||||
isDefaultValue(value: T): boolean {
|
||||
return isEqual(value, this.defaultValue);
|
||||
}
|
||||
|
||||
@action
|
||||
private configureObservable(options = this.options.observable) {
|
||||
this.data = observable.box<T>(this.data.get(), {
|
||||
...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;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user