您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LocalStorage.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { AsyncStorage } from 'react-native';
  2. /**
  3. * Prefix used to mark keys stored by our app in the global React Native
  4. * storage.
  5. */
  6. const JITSI_KEY_PREFIX = '@jitsi/';
  7. /**
  8. * Web Sorage API compatible object used for polyfilling window.localStorage.
  9. * The Web Storage API is synchronous, whereas AsyncStorage, the builtin generic
  10. * storage API in React Native, is asynchronous, so this object is optimistic:
  11. * it will first store the value locally (in memory) so results can be served
  12. * synchronously, and then save the value asynchronously.
  13. *
  14. * If any of the asynchronous operations produces an error, it's ignored.
  15. */
  16. export default class LocalStorage {
  17. /**
  18. * Loads all keys from React Native's AsyncStorage.
  19. */
  20. constructor() {
  21. AsyncStorage.getAllKeys()
  22. .then(keys => {
  23. const jitsiKeys
  24. = keys.filter(key => key.startsWith(JITSI_KEY_PREFIX));
  25. AsyncStorage.multiGet(jitsiKeys)
  26. .then(items => {
  27. for (const item of items) {
  28. const key = item[0].slice(JITSI_KEY_PREFIX.length);
  29. const value = item[1];
  30. this[key] = value;
  31. }
  32. });
  33. });
  34. }
  35. /**
  36. * Gets the number of items stored.
  37. *
  38. * @returns {number}
  39. */
  40. get length() {
  41. return Object.keys(this).length;
  42. }
  43. /**
  44. * Removes all keys from the storage.
  45. *
  46. * @returns {void}
  47. */
  48. clear() {
  49. const keys = Object.keys(this);
  50. for (const key of keys) {
  51. delete this[key];
  52. AsyncStorage.removeItem(`${JITSI_KEY_PREFIX}${key}`);
  53. }
  54. }
  55. /**
  56. * Gets the element that was stored for the given `key`.
  57. *
  58. * @param {string} key - The requested `key`.
  59. * @returns {string|null}
  60. */
  61. getItem(key) {
  62. if (this.hasOwnProperty(key)) {
  63. return this[key];
  64. }
  65. return null;
  66. }
  67. /**
  68. * Gets the nth element in the storage.
  69. *
  70. * @param {number} n - The element number that is requested.
  71. * @returns {string}
  72. */
  73. key(n) {
  74. return Object.keys(this)[n || 0];
  75. }
  76. /**
  77. * Removes the given `key` from the storage.
  78. *
  79. * @param {string} key - The `key` which will be removed.
  80. * @returns {void}
  81. */
  82. removeItem(key) {
  83. delete this[key];
  84. AsyncStorage.removeItem(`${JITSI_KEY_PREFIX}${key}`);
  85. }
  86. /**
  87. * Stores the given `value` for the given `key`. If a value or ready exists
  88. * for that key, it's updated.
  89. *
  90. * @param {string} key - The key for the value which will be stored.
  91. * @param {string} value - The value which will be stored.
  92. * @returns {void}
  93. */
  94. setItem(key, value) {
  95. // eslint-disable-next-line no-param-reassign
  96. value = String(value);
  97. this[key] = value;
  98. AsyncStorage.setItem(`${JITSI_KEY_PREFIX}${key}`, value);
  99. }
  100. }