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

PersistenceRegistry.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // @flow
  2. import md5 from 'js-md5';
  3. const logger = require('jitsi-meet-logger').getLogger(__filename);
  4. /**
  5. * The name of the {@code localStorage} store where the app persists its values.
  6. */
  7. const PERSISTED_STATE_NAME = 'jitsi-state';
  8. /**
  9. * Mixed type of the element (subtree) config. If it's a {@code boolean} (and is
  10. * {@code true}), we persist the entire subtree. If it's an {@code Object}, we
  11. * perist a filtered subtree based on the properties of the config object.
  12. */
  13. declare type ElementConfig = boolean | Object;
  14. /**
  15. * The type of the name-config pairs stored in {@code PersistenceRegistry}.
  16. */
  17. declare type PersistencyConfigMap = { [name: string]: ElementConfig };
  18. /**
  19. * A registry to allow features to register their redux store subtree to be
  20. * persisted and also handles the persistency calls too.
  21. */
  22. class PersistenceRegistry {
  23. _checksum: string;
  24. _elements: PersistencyConfigMap = {};
  25. /**
  26. * Returns the persisted redux state. Takes the {@link #_elements} into
  27. * account as we may have persisted something in the past that we don't want
  28. * to retreive anymore. The next {@link #persistState} will remove such
  29. * values.
  30. *
  31. * @returns {Object}
  32. */
  33. getPersistedState() {
  34. let filteredPersistedState = {};
  35. // localStorage key per feature
  36. for (const subtreeName of Object.keys(this._elements)) {
  37. // Assumes that the persisted value is stored under the same key as
  38. // the feature's redux state name.
  39. // TODO We'll need to introduce functions later that can control the
  40. // persist key's name. Similar to control serialization and
  41. // deserialization. But that should be a straightforward change.
  42. const persistedSubtree
  43. = this._getPersistedSubtree(
  44. subtreeName,
  45. this._elements[subtreeName]);
  46. if (persistedSubtree !== undefined) {
  47. filteredPersistedState[subtreeName] = persistedSubtree;
  48. }
  49. }
  50. // legacy
  51. if (Object.keys(filteredPersistedState).length === 0) {
  52. const { localStorage } = window;
  53. let persistedState = localStorage.getItem(PERSISTED_STATE_NAME);
  54. if (persistedState) {
  55. try {
  56. persistedState = JSON.parse(persistedState);
  57. } catch (error) {
  58. logger.error(
  59. 'Error parsing persisted state',
  60. persistedState,
  61. error);
  62. persistedState = {};
  63. }
  64. filteredPersistedState = this._getFilteredState(persistedState);
  65. // Store into the new format and delete the old format so that
  66. // it's not used again.
  67. this.persistState(filteredPersistedState);
  68. localStorage.removeItem(PERSISTED_STATE_NAME);
  69. }
  70. }
  71. // Initialize the checksum.
  72. this._checksum = this._calculateChecksum(filteredPersistedState);
  73. logger.info('redux state rehydrated as', filteredPersistedState);
  74. return filteredPersistedState;
  75. }
  76. /**
  77. * Initiates a persist operation, but its execution will depend on the
  78. * current checksums (checks changes).
  79. *
  80. * @param {Object} state - The redux state.
  81. * @returns {void}
  82. */
  83. persistState(state: Object) {
  84. const filteredState = this._getFilteredState(state);
  85. const checksum = this._calculateChecksum(filteredState);
  86. if (checksum !== this._checksum) {
  87. for (const subtreeName of Object.keys(filteredState)) {
  88. try {
  89. window.localStorage.setItem(
  90. subtreeName,
  91. JSON.stringify(filteredState[subtreeName]));
  92. } catch (error) {
  93. logger.error(
  94. 'Error persisting redux subtree',
  95. subtreeName,
  96. filteredState[subtreeName],
  97. error);
  98. }
  99. }
  100. logger.info(
  101. `redux state persisted. ${this._checksum} -> ${checksum}`);
  102. this._checksum = checksum;
  103. }
  104. }
  105. /**
  106. * Registers a new subtree config to be used for the persistency.
  107. *
  108. * @param {string} name - The name of the subtree the config belongs to.
  109. * @param {ElementConfig} config - The config {@code Object}, or
  110. * {@code boolean} if the entire subtree needs to be persisted.
  111. * @returns {void}
  112. */
  113. register(name: string, config?: ElementConfig = true) {
  114. this._elements[name] = config;
  115. }
  116. /**
  117. * Calculates the checksum of a specific state.
  118. *
  119. * @param {Object} state - The redux state to calculate the checksum of.
  120. * @private
  121. * @returns {string} The checksum of the specified {@code state}.
  122. */
  123. _calculateChecksum(state: Object) {
  124. try {
  125. return md5.hex(JSON.stringify(state) || '');
  126. } catch (error) {
  127. logger.error('Error calculating checksum for state', state, error);
  128. return '';
  129. }
  130. }
  131. /**
  132. * Prepares a filtered state from the actual or the persisted redux state,
  133. * based on this registry.
  134. *
  135. * @param {Object} state - The actual or persisted redux state.
  136. * @private
  137. * @returns {Object}
  138. */
  139. _getFilteredState(state: Object) {
  140. const filteredState = {};
  141. for (const name of Object.keys(this._elements)) {
  142. if (state[name]) {
  143. filteredState[name]
  144. = this._getFilteredSubtree(
  145. state[name],
  146. this._elements[name]);
  147. }
  148. }
  149. return filteredState;
  150. }
  151. /**
  152. * Prepares a filtered subtree based on the config for persisting or for
  153. * retrieval.
  154. *
  155. * @param {Object} subtree - The redux state subtree.
  156. * @param {ElementConfig} subtreeConfig - The related config.
  157. * @private
  158. * @returns {Object}
  159. */
  160. _getFilteredSubtree(subtree, subtreeConfig) {
  161. let filteredSubtree;
  162. if (typeof subtreeConfig === 'object') {
  163. // Only a filtered subtree gets persisted as specified by
  164. // subtreeConfig.
  165. filteredSubtree = {};
  166. for (const persistedKey of Object.keys(subtree)) {
  167. if (subtreeConfig[persistedKey]) {
  168. filteredSubtree[persistedKey] = subtree[persistedKey];
  169. }
  170. }
  171. } else if (subtreeConfig) {
  172. // Persist the entire subtree.
  173. filteredSubtree = subtree;
  174. }
  175. return filteredSubtree;
  176. }
  177. /**
  178. * Retreives a persisted subtree from the storage.
  179. *
  180. * @param {string} subtreeName - The name of the subtree.
  181. * @param {Object} subtreeConfig - The config of the subtree from
  182. * {@link #_elements}.
  183. * @private
  184. * @returns {Object}
  185. */
  186. _getPersistedSubtree(subtreeName, subtreeConfig) {
  187. let persistedSubtree = window.localStorage.getItem(subtreeName);
  188. if (persistedSubtree) {
  189. try {
  190. persistedSubtree = JSON.parse(persistedSubtree);
  191. const filteredSubtree
  192. = this._getFilteredSubtree(persistedSubtree, subtreeConfig);
  193. if (filteredSubtree !== undefined) {
  194. return filteredSubtree;
  195. }
  196. } catch (error) {
  197. logger.error(
  198. 'Error parsing persisted subtree',
  199. subtreeName,
  200. persistedSubtree,
  201. error);
  202. }
  203. }
  204. return undefined;
  205. }
  206. }
  207. export default new PersistenceRegistry();