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.get()).toBe("test");
await storageHelper.init();
storageHelper.init();
expect(storageHelper.key).toBe(storageKey);
expect(storageHelper.defaultValue).toBe("test");

View File

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