2022-07-15 12:33:09 +00:00
|
|
|
// @ts-ignore
|
2021-06-02 19:21:39 +00:00
|
|
|
import Bourne from '@hapi/bourne';
|
2022-09-06 17:32:20 +00:00
|
|
|
// eslint-disable-next-line lines-around-comment
|
2022-07-15 12:33:09 +00:00
|
|
|
// @ts-ignore
|
2020-07-09 07:17:23 +00:00
|
|
|
import { jitsiLocalStorage } from '@jitsi/js-utils';
|
2018-01-17 11:19:10 +00:00
|
|
|
import md5 from 'js-md5';
|
|
|
|
|
2019-08-21 14:50:00 +00:00
|
|
|
import logger from './logger';
|
2018-01-17 11:19:10 +00:00
|
|
|
|
2022-07-15 12:33:09 +00:00
|
|
|
declare let __DEV__: any;
|
2019-11-14 12:33:20 +00:00
|
|
|
|
2018-02-06 09:43:06 +00:00
|
|
|
/**
|
2018-02-27 20:21:28 +00:00
|
|
|
* Mixed type of the element (subtree) config. If it's a {@code boolean} (and is
|
|
|
|
* {@code true}), we persist the entire subtree. If it's an {@code Object}, we
|
|
|
|
* perist a filtered subtree based on the properties of the config object.
|
2018-02-06 09:43:06 +00:00
|
|
|
*/
|
2018-02-27 20:21:28 +00:00
|
|
|
declare type ElementConfig = boolean | Object;
|
2018-02-06 09:43:06 +00:00
|
|
|
|
2018-01-17 11:19:10 +00:00
|
|
|
/**
|
2018-02-27 20:21:28 +00:00
|
|
|
* The type of the name-config pairs stored in {@code PersistenceRegistry}.
|
2018-01-17 11:19:10 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
declare type PersistencyConfigMap = { [name: string]: ElementConfig; };
|
2018-01-17 11:19:10 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-29 22:20:38 +00:00
|
|
|
* A registry to allow features to register their redux store subtree to be
|
|
|
|
* persisted and also handles the persistency calls too.
|
2018-01-17 11:19:10 +00:00
|
|
|
*/
|
2018-02-02 19:35:49 +00:00
|
|
|
class PersistenceRegistry {
|
2022-07-15 12:33:09 +00:00
|
|
|
_checksum = '';
|
2022-09-08 09:52:36 +00:00
|
|
|
_defaultStates: { [name: string ]: Object | undefined; } = {};
|
2018-02-27 20:21:28 +00:00
|
|
|
_elements: PersistencyConfigMap = {};
|
2018-01-17 11:19:10 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-27 20:21:28 +00:00
|
|
|
* Returns the persisted redux state. Takes the {@link #_elements} into
|
|
|
|
* account as we may have persisted something in the past that we don't want
|
2021-03-16 15:59:33 +00:00
|
|
|
* to retrieve anymore. The next {@link #persistState} will remove such
|
2018-02-27 20:21:28 +00:00
|
|
|
* values.
|
2018-01-17 11:19:10 +00:00
|
|
|
*
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
getPersistedState() {
|
2022-07-12 14:59:06 +00:00
|
|
|
const filteredPersistedState: any = {};
|
2018-01-17 11:19:10 +00:00
|
|
|
|
2018-02-27 20:21:28 +00:00
|
|
|
// localStorage key per feature
|
|
|
|
for (const subtreeName of Object.keys(this._elements)) {
|
|
|
|
// Assumes that the persisted value is stored under the same key as
|
|
|
|
// the feature's redux state name.
|
|
|
|
const persistedSubtree
|
|
|
|
= this._getPersistedSubtree(
|
|
|
|
subtreeName,
|
2018-07-06 18:03:16 +00:00
|
|
|
this._elements[subtreeName],
|
|
|
|
this._defaultStates[subtreeName]);
|
2018-02-27 20:21:28 +00:00
|
|
|
|
|
|
|
if (persistedSubtree !== undefined) {
|
|
|
|
filteredPersistedState[subtreeName] = persistedSubtree;
|
2018-01-17 11:19:10 +00:00
|
|
|
}
|
2018-02-27 20:21:28 +00:00
|
|
|
}
|
2018-01-17 11:19:10 +00:00
|
|
|
|
2018-02-27 20:21:28 +00:00
|
|
|
// Initialize the checksum.
|
2018-02-12 22:03:05 +00:00
|
|
|
this._checksum = this._calculateChecksum(filteredPersistedState);
|
|
|
|
|
2019-11-14 12:33:20 +00:00
|
|
|
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
|
|
logger.info('redux state rehydrated as', filteredPersistedState);
|
|
|
|
}
|
2018-01-17 11:19:10 +00:00
|
|
|
|
|
|
|
return filteredPersistedState;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-29 22:20:38 +00:00
|
|
|
* Initiates a persist operation, but its execution will depend on the
|
|
|
|
* current checksums (checks changes).
|
2018-01-17 11:19:10 +00:00
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
persistState(state: Object) {
|
|
|
|
const filteredState = this._getFilteredState(state);
|
2018-02-27 20:21:28 +00:00
|
|
|
const checksum = this._calculateChecksum(filteredState);
|
2018-01-17 11:19:10 +00:00
|
|
|
|
2018-02-27 20:21:28 +00:00
|
|
|
if (checksum !== this._checksum) {
|
2018-02-12 22:03:05 +00:00
|
|
|
for (const subtreeName of Object.keys(filteredState)) {
|
|
|
|
try {
|
2020-05-01 19:48:08 +00:00
|
|
|
jitsiLocalStorage.setItem(subtreeName, JSON.stringify(filteredState[subtreeName]));
|
2018-02-12 22:03:05 +00:00
|
|
|
} catch (error) {
|
2020-05-01 19:48:08 +00:00
|
|
|
logger.error('Error persisting redux subtree', subtreeName, error);
|
2018-02-12 22:03:05 +00:00
|
|
|
}
|
2018-01-17 11:19:10 +00:00
|
|
|
}
|
2020-05-01 19:48:08 +00:00
|
|
|
logger.info(`redux state persisted. ${this._checksum} -> ${checksum}`);
|
2018-02-27 20:21:28 +00:00
|
|
|
this._checksum = checksum;
|
2018-01-17 11:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a new subtree config to be used for the persistency.
|
|
|
|
*
|
|
|
|
* @param {string} name - The name of the subtree the config belongs to.
|
2018-02-27 20:21:28 +00:00
|
|
|
* @param {ElementConfig} config - The config {@code Object}, or
|
|
|
|
* {@code boolean} if the entire subtree needs to be persisted.
|
2018-07-06 18:03:16 +00:00
|
|
|
* @param {Object} defaultState - The default state of the component. If
|
|
|
|
* it's provided, the rehydrated state will be merged with it before it gets
|
|
|
|
* pushed into Redux.
|
2018-01-17 11:19:10 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-07-06 18:03:16 +00:00
|
|
|
register(
|
|
|
|
name: string,
|
2022-07-15 12:33:09 +00:00
|
|
|
config: ElementConfig = true,
|
2018-07-06 18:03:16 +00:00
|
|
|
defaultState?: Object) {
|
2018-01-17 11:19:10 +00:00
|
|
|
this._elements[name] = config;
|
2018-07-06 18:03:16 +00:00
|
|
|
this._defaultStates[name] = defaultState;
|
2018-01-17 11:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-27 20:21:28 +00:00
|
|
|
* Calculates the checksum of a specific state.
|
2018-01-17 11:19:10 +00:00
|
|
|
*
|
2018-02-27 20:21:28 +00:00
|
|
|
* @param {Object} state - The redux state to calculate the checksum of.
|
2018-01-17 11:19:10 +00:00
|
|
|
* @private
|
2018-02-27 20:21:28 +00:00
|
|
|
* @returns {string} The checksum of the specified {@code state}.
|
2018-01-17 11:19:10 +00:00
|
|
|
*/
|
2018-02-27 20:21:28 +00:00
|
|
|
_calculateChecksum(state: Object) {
|
2018-01-17 11:19:10 +00:00
|
|
|
try {
|
2018-02-27 20:21:28 +00:00
|
|
|
return md5.hex(JSON.stringify(state) || '');
|
2018-01-17 11:19:10 +00:00
|
|
|
} catch (error) {
|
2019-11-14 12:33:20 +00:00
|
|
|
logger.error('Error calculating checksum for state', error);
|
2018-01-17 11:19:10 +00:00
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-29 22:20:38 +00:00
|
|
|
* Prepares a filtered state from the actual or the persisted redux state,
|
|
|
|
* based on this registry.
|
2018-01-17 11:19:10 +00:00
|
|
|
*
|
|
|
|
* @param {Object} state - The actual or persisted redux state.
|
2018-02-27 20:21:28 +00:00
|
|
|
* @private
|
2018-01-17 11:19:10 +00:00
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2022-07-15 12:33:09 +00:00
|
|
|
_getFilteredState(state: any): any {
|
|
|
|
const filteredState: any = {};
|
2018-01-17 11:19:10 +00:00
|
|
|
|
|
|
|
for (const name of Object.keys(this._elements)) {
|
|
|
|
if (state[name]) {
|
2018-02-27 20:21:28 +00:00
|
|
|
filteredState[name]
|
|
|
|
= this._getFilteredSubtree(
|
|
|
|
state[name],
|
|
|
|
this._elements[name]);
|
2018-01-17 11:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredState;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-29 22:20:38 +00:00
|
|
|
* Prepares a filtered subtree based on the config for persisting or for
|
|
|
|
* retrieval.
|
2018-01-17 11:19:10 +00:00
|
|
|
*
|
|
|
|
* @param {Object} subtree - The redux state subtree.
|
2018-02-06 09:43:06 +00:00
|
|
|
* @param {ElementConfig} subtreeConfig - The related config.
|
2018-02-27 20:21:28 +00:00
|
|
|
* @private
|
2018-01-17 11:19:10 +00:00
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2022-07-15 12:33:09 +00:00
|
|
|
_getFilteredSubtree(subtree: any, subtreeConfig: any) {
|
|
|
|
let filteredSubtree: any;
|
2018-02-06 09:43:06 +00:00
|
|
|
|
2018-02-27 20:21:28 +00:00
|
|
|
if (typeof subtreeConfig === 'object') {
|
|
|
|
// Only a filtered subtree gets persisted as specified by
|
|
|
|
// subtreeConfig.
|
2018-02-06 09:43:06 +00:00
|
|
|
filteredSubtree = {};
|
|
|
|
for (const persistedKey of Object.keys(subtree)) {
|
|
|
|
if (subtreeConfig[persistedKey]) {
|
|
|
|
filteredSubtree[persistedKey] = subtree[persistedKey];
|
|
|
|
}
|
2018-01-17 11:19:10 +00:00
|
|
|
}
|
2018-02-27 20:21:28 +00:00
|
|
|
} else if (subtreeConfig) {
|
|
|
|
// Persist the entire subtree.
|
|
|
|
filteredSubtree = subtree;
|
2018-01-17 11:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return filteredSubtree;
|
|
|
|
}
|
2018-02-27 20:21:28 +00:00
|
|
|
|
|
|
|
/**
|
2021-03-16 15:59:33 +00:00
|
|
|
* Retrieves a persisted subtree from the storage.
|
2018-02-27 20:21:28 +00:00
|
|
|
*
|
|
|
|
* @param {string} subtreeName - The name of the subtree.
|
|
|
|
* @param {Object} subtreeConfig - The config of the subtree from
|
|
|
|
* {@link #_elements}.
|
2018-07-06 18:03:16 +00:00
|
|
|
* @param {Object} subtreeDefaults - The defaults of the persisted subtree.
|
2018-02-27 20:21:28 +00:00
|
|
|
* @private
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2022-07-15 12:33:09 +00:00
|
|
|
_getPersistedSubtree(subtreeName: string, subtreeConfig: Object, subtreeDefaults?: Object) {
|
2020-05-01 19:48:08 +00:00
|
|
|
let persistedSubtree = jitsiLocalStorage.getItem(subtreeName);
|
2018-02-27 20:21:28 +00:00
|
|
|
|
|
|
|
if (persistedSubtree) {
|
|
|
|
try {
|
2021-06-02 19:21:39 +00:00
|
|
|
persistedSubtree = Bourne.parse(persistedSubtree);
|
2018-02-27 20:21:28 +00:00
|
|
|
|
|
|
|
const filteredSubtree
|
|
|
|
= this._getFilteredSubtree(persistedSubtree, subtreeConfig);
|
|
|
|
|
|
|
|
if (filteredSubtree !== undefined) {
|
2018-07-06 18:03:16 +00:00
|
|
|
return this._mergeDefaults(
|
|
|
|
filteredSubtree, subtreeDefaults);
|
2018-02-27 20:21:28 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
'Error parsing persisted subtree',
|
|
|
|
subtreeName,
|
|
|
|
persistedSubtree,
|
|
|
|
error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
2018-07-06 18:03:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges the persisted subtree with its defaults before rehydrating the
|
|
|
|
* values.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Object} subtree - The Redux subtree.
|
|
|
|
* @param {?Object} defaults - The defaults, if any.
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2022-07-15 12:33:09 +00:00
|
|
|
_mergeDefaults(subtree: Object, defaults?: Object) {
|
2018-07-06 18:03:16 +00:00
|
|
|
if (!defaults) {
|
|
|
|
return subtree;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the subtree is an array, we don't need to merge it with the
|
|
|
|
// defaults, because if it has a value, it will overwrite it, and if
|
|
|
|
// it's undefined, it won't be even returned, and Redux will natively
|
|
|
|
// use the default values instead.
|
|
|
|
if (!Array.isArray(subtree)) {
|
|
|
|
return {
|
|
|
|
...defaults,
|
|
|
|
...subtree
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 11:19:10 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 19:35:49 +00:00
|
|
|
export default new PersistenceRegistry();
|