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

revert init

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-04-01 13:44:35 -04:00
parent ac95f7575f
commit 8fafcb4e0c
2 changed files with 25 additions and 22 deletions

View File

@ -26,7 +26,7 @@ describe("renderer/utils/StorageHelper", () => {
expect(storageHelper.defaultValue).toBe("test"); expect(storageHelper.defaultValue).toBe("test");
expect(storageHelper.get()).toBe("test"); expect(storageHelper.get()).toBe("test");
await storageHelper.init(); storageHelper.init();
expect(storageHelper.key).toBe(storageKey); expect(storageHelper.key).toBe(storageKey);
expect(storageHelper.defaultValue).toBe("test"); expect(storageHelper.defaultValue).toBe("test");

View File

@ -59,14 +59,20 @@ export class StorageHelper<T> {
} }
} }
/** private onData = (data: T): void => {
* This function turns an optionally synchronous call to an asynchronous call const notEmpty = data != null;
* and correctly handles the synchronous call throwing an error const notDefault = !this.isDefaultValue(data);
* @returns the result of this.storage.getItem
*/ if (notEmpty && notDefault) {
private async getItemFromStorage(): Promise<T> { this.merge(data);
return this.storage.getItem(this.key); }
}
this.initialized = true;
};
private onError = (error: any): void => {
logger.error(`[load]: ${error}`, this);
};
@action @action
init({ force = false } = {}) { init({ force = false } = {}) {
@ -74,20 +80,17 @@ export class StorageHelper<T> {
return; return;
} }
this.getItemFromStorage() try {
.then(data => { const data = this.storage.getItem(this.key);
const notEmpty = data != null;
const notDefault = !this.isDefaultValue(data);
if (notEmpty && notDefault) { if (data instanceof Promise) {
this.merge(data); data.then(this.onData, this.onError);
} } else {
this.onData(data);
this.initialized = true; }
}) } catch (error) {
.catch(error => { this.onError(error);
logger.error(`[load]: ${error}`, this); }
});
} }
isDefaultValue(value: T): boolean { isDefaultValue(value: T): boolean {