闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
973 B

  1. export default class Settings {
  2. constructor(keyBase = null) {
  3. this.keyBase = keyBase;
  4. }
  5. generateKey(id) {
  6. return this.keyBase ? [this.keyBase, `id${id}`].join('.') : id;
  7. }
  8. set(id, data) {
  9. const key = this.generateKey(id);
  10. try {
  11. const encodedData = JSON.stringify(data);
  12. localStorage.setItem(key, encodedData);
  13. return data;
  14. } catch (e) {
  15. return null;
  16. }
  17. }
  18. get(id) {
  19. const key = this.generateKey(id);
  20. try {
  21. const rawData = localStorage.getItem(key);
  22. return JSON.parse(rawData);
  23. } catch (e) {
  24. return null;
  25. }
  26. }
  27. remove(id) {
  28. const data = this.get(id);
  29. if (data) {
  30. const key = this.generateKey(id);
  31. try {
  32. localStorage.removeItem(key);
  33. } catch (e) {
  34. }
  35. }
  36. return data;
  37. }
  38. }
  39. export const pushNotificationsSetting = new Settings('mastodon_push_notification_data');
  40. export const tagHistory = new Settings('mastodon_tag_history');