|
|
|
@ -1,4 +1,5 @@ |
|
|
|
|
// @flow
|
|
|
|
|
|
|
|
|
|
import Logger from 'jitsi-meet-logger'; |
|
|
|
|
import md5 from 'js-md5'; |
|
|
|
|
|
|
|
|
@ -15,25 +16,26 @@ const PERSISTED_STATE_NAME = 'jitsi-state'; |
|
|
|
|
declare type PersistencyConfigMap = { [name: string]: Object }; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* A registry to allow features to register their redux store |
|
|
|
|
* subtree to be persisted and also handles the persistency calls too. |
|
|
|
|
* A registry to allow features to register their redux store subtree to be |
|
|
|
|
* persisted and also handles the persistency calls too. |
|
|
|
|
*/ |
|
|
|
|
class PersistencyRegistry { |
|
|
|
|
_checksum: string; |
|
|
|
|
|
|
|
|
|
_elements: PersistencyConfigMap; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Initiates the PersistencyRegistry. |
|
|
|
|
* Initializes a new {@ code PersistencyRegistry} instance. |
|
|
|
|
*/ |
|
|
|
|
constructor() { |
|
|
|
|
this._elements = {}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the persisted redux state. This function takes |
|
|
|
|
* the PersistencyRegistry._elements into account as we may have |
|
|
|
|
* persisted something in the past that we don't want to retreive anymore. |
|
|
|
|
* The next {@link #persistState} will remove those values. |
|
|
|
|
* Returns the persisted redux state. This function takes the |
|
|
|
|
* {@link #_elements} into account as we may have persisted something in the |
|
|
|
|
* past that we don't want to retreive anymore. The next |
|
|
|
|
* {@link #persistState} will remove those values. |
|
|
|
|
* |
|
|
|
|
* @returns {Object} |
|
|
|
|
*/ |
|
|
|
@ -46,8 +48,9 @@ class PersistencyRegistry { |
|
|
|
|
persistedState = JSON.parse(persistedState); |
|
|
|
|
} catch (error) { |
|
|
|
|
logger.error( |
|
|
|
|
'Error parsing persisted state', persistedState, error |
|
|
|
|
); |
|
|
|
|
'Error parsing persisted state', |
|
|
|
|
persistedState, |
|
|
|
|
error); |
|
|
|
|
persistedState = {}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -56,14 +59,14 @@ class PersistencyRegistry { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this._checksum = this._calculateChecksum(filteredPersistedState); |
|
|
|
|
logger.info('Redux state rehydrated as', filteredPersistedState); |
|
|
|
|
logger.info('redux state rehydrated as', filteredPersistedState); |
|
|
|
|
|
|
|
|
|
return filteredPersistedState; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Initiates a persist operation, but its execution will depend on |
|
|
|
|
* the current checksums (checks changes). |
|
|
|
|
* Initiates a persist operation, but its execution will depend on the |
|
|
|
|
* current checksums (checks changes). |
|
|
|
|
* |
|
|
|
|
* @param {Object} state - The redux state. |
|
|
|
|
* @returns {void} |
|
|
|
@ -76,14 +79,13 @@ class PersistencyRegistry { |
|
|
|
|
try { |
|
|
|
|
window.localStorage.setItem( |
|
|
|
|
PERSISTED_STATE_NAME, |
|
|
|
|
JSON.stringify(filteredState) |
|
|
|
|
); |
|
|
|
|
JSON.stringify(filteredState)); |
|
|
|
|
logger.info( |
|
|
|
|
`Redux state persisted. ${this._checksum} -> ${newCheckSum}` |
|
|
|
|
); |
|
|
|
|
`redux state persisted. ${this._checksum} -> ${ |
|
|
|
|
newCheckSum}`);
|
|
|
|
|
this._checksum = newCheckSum; |
|
|
|
|
} catch (error) { |
|
|
|
|
logger.error('Error persisting Redux state', error); |
|
|
|
|
logger.error('Error persisting redux state', error); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -103,7 +105,7 @@ class PersistencyRegistry { |
|
|
|
|
* Calculates the checksum of the current or the new values of the state. |
|
|
|
|
* |
|
|
|
|
* @private |
|
|
|
|
* @param {Object} filteredState - The filtered/persisted Redux state. |
|
|
|
|
* @param {Object} filteredState - The filtered/persisted redux state. |
|
|
|
|
* @returns {string} |
|
|
|
|
*/ |
|
|
|
|
_calculateChecksum(filteredState: Object) { |
|
|
|
@ -111,16 +113,17 @@ class PersistencyRegistry { |
|
|
|
|
return md5.hex(JSON.stringify(filteredState) || ''); |
|
|
|
|
} catch (error) { |
|
|
|
|
logger.error( |
|
|
|
|
'Error calculating checksum for state', filteredState, error |
|
|
|
|
); |
|
|
|
|
'Error calculating checksum for state', |
|
|
|
|
filteredState, |
|
|
|
|
error); |
|
|
|
|
|
|
|
|
|
return ''; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Prepares a filtered state from the actual or the |
|
|
|
|
* persisted Redux state, based on this registry. |
|
|
|
|
* Prepares a filtered state from the actual or the persisted redux state, |
|
|
|
|
* based on this registry. |
|
|
|
|
* |
|
|
|
|
* @private |
|
|
|
|
* @param {Object} state - The actual or persisted redux state. |
|
|
|
@ -131,10 +134,10 @@ class PersistencyRegistry { |
|
|
|
|
|
|
|
|
|
for (const name of Object.keys(this._elements)) { |
|
|
|
|
if (state[name]) { |
|
|
|
|
filteredState[name] = this._getFilteredSubtree( |
|
|
|
|
state[name], |
|
|
|
|
this._elements[name] |
|
|
|
|
); |
|
|
|
|
filteredState[name] |
|
|
|
|
= this._getFilteredSubtree( |
|
|
|
|
state[name], |
|
|
|
|
this._elements[name]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -142,8 +145,8 @@ class PersistencyRegistry { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Prepares a filtered subtree based on the config for |
|
|
|
|
* persisting or for retreival. |
|
|
|
|
* Prepares a filtered subtree based on the config for persisting or for |
|
|
|
|
* retrieval. |
|
|
|
|
* |
|
|
|
|
* @private |
|
|
|
|
* @param {Object} subtree - The redux state subtree. |
|
|
|
@ -155,8 +158,7 @@ class PersistencyRegistry { |
|
|
|
|
|
|
|
|
|
for (const persistedKey of Object.keys(subtree)) { |
|
|
|
|
if (subtreeConfig[persistedKey]) { |
|
|
|
|
filteredSubtree[persistedKey] |
|
|
|
|
= subtree[persistedKey]; |
|
|
|
|
filteredSubtree[persistedKey] = subtree[persistedKey]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|