convert store utils to typescript

This commit is contained in:
SeinopSys 2022-03-26 00:28:45 +01:00 committed by Luna D
parent 2b4ed32233
commit 7f7228631d
No known key found for this signature in database
GPG key ID: 4B1C63448394F688

View file

@ -6,7 +6,7 @@ export const lastUpdatedSuffix = '__lastUpdated';
export default {
set(key, value) {
set(key: string, value: unknown) {
try {
localStorage.setItem(key, JSON.stringify(value));
return true;
@ -16,17 +16,18 @@ export default {
}
},
get(key) {
get<Value = unknown>(key: string): Value | null {
const value = localStorage.getItem(key);
if (value === null) return null;
try {
return JSON.parse(value);
}
catch (err) {
return value;
return value as unknown as Value;
}
},
remove(key) {
remove(key: string) {
try {
localStorage.removeItem(key);
return true;
@ -37,8 +38,8 @@ export default {
},
// Watch changes to a specified key - returns value on change
watch(key, callback) {
const handler = event => {
watch(key: string, callback: (value: unknown) => void) {
const handler = (event: StorageEvent) => {
if (event.key === key) callback(this.get(key));
};
window.addEventListener('storage', handler);
@ -46,7 +47,7 @@ export default {
},
// set() with an additional key containing the current time + expiration time
setWithExpireTime(key, value, maxAge) {
setWithExpireTime(key: string, value: unknown, maxAge: number) {
const lastUpdatedKey = key + lastUpdatedSuffix;
const lastUpdatedTime = Date.now() + maxAge;
@ -54,11 +55,11 @@ export default {
},
// Whether the value of a key set with setWithExpireTime() has expired
hasExpired(key) {
hasExpired(key: string) {
const lastUpdatedKey = key + lastUpdatedSuffix;
const lastUpdatedTime = this.get(lastUpdatedKey);
const lastUpdatedTime = this.get<number>(lastUpdatedKey);
return Date.now() > lastUpdatedTime;
return lastUpdatedTime !== null && Date.now() > lastUpdatedTime;
},
};