Skip to content

Commit

Permalink
Remove superfluous checks and add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Andr3wid committed Mar 25, 2024
1 parent 87efc46 commit 6aef2b2
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/ts/storageutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export namespace StorageUtils {
}
} catch (e) {
/* Can get triggered e.g. by QuotaExceededError in Safari private mode */
console.warn(`Failed to set storage entry for ${key}`);
}
}

Expand All @@ -37,8 +38,9 @@ export namespace StorageUtils {
return window.localStorage.getItem(key);
}
} catch (e) {
return null;
console.warn(`Failed to retrieve storage entry for ${key}`);
}
return null;
}

/**
Expand All @@ -50,10 +52,8 @@ export namespace StorageUtils {
* @param data the object to store
*/
export function setObject<T>(key: string, data: T): void {
if (StorageUtils.isLocalStorageAvailable()) {
let json = JSON.stringify(data);
setItem(key, json);
}
let json = JSON.stringify(data);
setItem(key, json);
}

/**
Expand All @@ -64,14 +64,12 @@ export namespace StorageUtils {
* @param key the key to look up its associated object
* @return {any} Returns the object if found, null otherwise
*/
export function getObject<T>(key: string): T {
if (StorageUtils.isLocalStorageAvailable()) {
let json = getItem(key);
export function getObject<T>(key: string): T | null {
let json = getItem(key);

if (key) {
let object = JSON.parse(json);
return <T>object;
}
if (json) {
let object = JSON.parse(json);
return <T>object;
}
return null;
}
Expand Down

0 comments on commit 6aef2b2

Please sign in to comment.