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.

JitsiLocalStorage.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import Logger from 'jitsi-meet-logger';
  2. const logger = Logger.getLogger(__filename);
  3. /**
  4. * Dummy implementation of Storage interface with empty methods.
  5. */
  6. class DummyLocalStorage {
  7. /**
  8. * Empty function
  9. */
  10. getItem() { }
  11. /**
  12. * Empty function
  13. */
  14. setItem() { }
  15. /**
  16. * Empty function
  17. */
  18. removeItem() { }
  19. }
  20. /**
  21. * Wrapper class for browser's local storage object.
  22. */
  23. class JitsiLocalStorage extends DummyLocalStorage {
  24. /**
  25. * @constructor
  26. * @param {Storage} storage browser's local storage object.
  27. */
  28. constructor() {
  29. super();
  30. let storage;
  31. try {
  32. storage = window.localStorage;
  33. } catch (error) {
  34. logger.error(error);
  35. }
  36. this.storage = storage || new DummyLocalStorage();
  37. }
  38. /**
  39. * Returns that passed key's value.
  40. * @param {string} keyName the name of the key you want to retrieve
  41. * the value of.
  42. * @returns {String|null} the value of the key. If the key does not exist,
  43. * null is returned.
  44. */
  45. getItem(keyName) {
  46. return this.storage.getItem(keyName);
  47. }
  48. /**
  49. * Adds a key to the storage, or update key's value if it already exists.
  50. * @param {string} keyName the name of the key you want to create/update.
  51. * @param {string} keyValue the value you want to give the key you are
  52. * creating/updating.
  53. */
  54. setItem(keyName, keyValue) {
  55. return this.storage.setItem(keyName, keyValue);
  56. }
  57. /**
  58. * Remove a key from the storage.
  59. * @param {string} keyName the name of the key you want to remove.
  60. */
  61. removeItem(keyName) {
  62. return this.storage.removeItem(keyName);
  63. }
  64. }
  65. export default new JitsiLocalStorage();