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.8KB

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