bigbuffet-rw/app/soapbox/settings.js

51 lines
1,008 B
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
'use strict';
export default class Settings {
constructor(keyBase = null) {
this.keyBase = keyBase;
}
generateKey(id) {
return this.keyBase ? [this.keyBase, `id${id}`].join('.') : id;
}
set(id, data) {
const key = this.generateKey(id);
try {
const encodedData = JSON.stringify(data);
localStorage.setItem(key, encodedData);
return data;
} catch (e) {
return null;
}
}
get(id) {
const key = this.generateKey(id);
try {
const rawData = localStorage.getItem(key);
return JSON.parse(rawData);
} catch (e) {
return null;
}
}
remove(id) {
const data = this.get(id);
if (data) {
const key = this.generateKey(id);
try {
localStorage.removeItem(key);
} catch (e) {
2021-08-03 12:29:36 -07:00
// Do nothing
2020-03-27 13:59:38 -07:00
}
}
return data;
}
}
2020-05-28 15:52:07 -07:00
export const pushNotificationsSetting = new Settings('soapbox_push_notification_data');
export const tagHistory = new Settings('soapbox_tag_history');