philomena/assets/js/utils/store.js

65 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-10-05 02:09:52 +02:00
/**
* localStorage utils
*/
export const lastUpdatedSuffix = '__lastUpdated';
2019-10-05 02:09:52 +02:00
export default {
set(key, value) {
try {
localStorage.setItem(key, JSON.stringify(value));
return true;
}
catch (err) {
return false;
}
},
get(key) {
const value = localStorage.getItem(key);
try {
return JSON.parse(value);
}
catch (err) {
return value;
}
},
remove(key) {
try {
localStorage.removeItem(key);
return true;
}
catch (err) {
return false;
}
},
// Watch changes to a specified key - returns value on change
watch(key, callback) {
const handler = event => {
2019-10-05 02:09:52 +02:00
if (event.key === key) callback(this.get(key));
};
window.addEventListener('storage', handler);
return () => window.removeEventListener('storage', handler);
2019-10-05 02:09:52 +02:00
},
// set() with an additional key containing the current time + expiration time
setWithExpireTime(key, value, maxAge) {
const lastUpdatedKey = key + lastUpdatedSuffix;
const lastUpdatedTime = Date.now() + maxAge;
this.set(key, value) && this.set(lastUpdatedKey, lastUpdatedTime);
},
// Whether the value of a key set with setWithExpireTime() has expired
hasExpired(key) {
const lastUpdatedKey = key + lastUpdatedSuffix;
const lastUpdatedTime = this.get(lastUpdatedKey);
return Date.now() > lastUpdatedTime;
2019-10-05 02:09:52 +02:00
},
};