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,23 +59,7 @@ export class StorageHelper<T> {
} }
} }
/** private onData = (data: T): void => {
* 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
init({ force = false } = {}) {
if (this.initialized && !force) {
return;
}
this.getItemFromStorage()
.then(data => {
const notEmpty = data != null; const notEmpty = data != null;
const notDefault = !this.isDefaultValue(data); const notDefault = !this.isDefaultValue(data);
@ -84,10 +68,29 @@ export class StorageHelper<T> {
} }
this.initialized = true; this.initialized = true;
}) };
.catch(error => {
private onError = (error: any): void => {
logger.error(`[load]: ${error}`, this); logger.error(`[load]: ${error}`, this);
}); };
@action
init({ force = false } = {}) {
if (this.initialized && !force) {
return;
}
try {
const data = this.storage.getItem(this.key);
if (data instanceof Promise) {
data.then(this.onData, this.onError);
} else {
this.onData(data);
}
} catch (error) {
this.onError(error);
}
} }
isDefaultValue(value: T): boolean { isDefaultValue(value: T): boolean {