-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data module: Add built-in support for persisting stores (#8146)
* Data module: Add built-in support for persisting stores * Remove useless storage key * Don't repersist if the persisted changes are kept identical * Restoring deprecated APIs * Avoid using window.localStorage when loading the persist file * Fix inline-tokens registration * Fix with-history Higher-order reducer to be stateless
- Loading branch information
1 parent
b6ca3c1
commit f508995
Showing
11 changed files
with
393 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import deprecated from '@wordpress/deprecated'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getPersistenceStorage } from './persist'; | ||
|
||
/** | ||
* Adds the rehydration behavior to redux reducers. | ||
* | ||
* @param {Function} reducer The reducer to enhance. | ||
* @param {string} reducerKey The reducer key to persist. | ||
* @param {string} storageKey The storage key to use. | ||
* | ||
* @return {Function} Enhanced reducer. | ||
*/ | ||
export function withRehydration( reducer, reducerKey, storageKey ) { | ||
deprecated( 'wp.data.withRehydration', { | ||
version: '3.6', | ||
plugin: 'Gutenberg', | ||
hint: 'See https://github.com/WordPress/gutenberg/pull/8146 for more details', | ||
} ); | ||
|
||
// EnhancedReducer with auto-rehydration | ||
const enhancedReducer = ( state, action ) => { | ||
const nextState = reducer( state, action ); | ||
|
||
if ( action.type === 'REDUX_REHYDRATE' && action.storageKey === storageKey ) { | ||
return { | ||
...nextState, | ||
[ reducerKey ]: action.payload, | ||
}; | ||
} | ||
|
||
return nextState; | ||
}; | ||
|
||
return enhancedReducer; | ||
} | ||
|
||
/** | ||
* Loads the initial state and persist on changes. | ||
* | ||
* This should be executed after the reducer's registration. | ||
* | ||
* @param {Object} store Store to enhance. | ||
* @param {Function} reducer The reducer function. Used to get default values and to allow custom serialization by the reducers. | ||
* @param {string} reducerKey The reducer key to persist (example: reducerKey.subReducerKey). | ||
* @param {string} storageKey The storage key to use. | ||
*/ | ||
export function loadAndPersist( store, reducer, reducerKey, storageKey ) { | ||
deprecated( 'wp.data.loadAndPersist', { | ||
version: '3.6', | ||
plugin: 'Gutenberg', | ||
hint: 'See https://github.com/WordPress/gutenberg/pull/8146 for more details', | ||
} ); | ||
|
||
// Load initially persisted value | ||
const persistedString = getPersistenceStorage().getItem( storageKey ); | ||
if ( persistedString ) { | ||
const persistedState = { | ||
...get( reducer( undefined, { type: '@@gutenberg/init' } ), reducerKey ), | ||
...JSON.parse( persistedString ), | ||
}; | ||
|
||
store.dispatch( { | ||
type: 'REDUX_REHYDRATE', | ||
payload: persistedState, | ||
storageKey, | ||
} ); | ||
} | ||
|
||
// Persist updated preferences | ||
let currentStateValue = get( store.getState(), reducerKey ); | ||
store.subscribe( () => { | ||
const newStateValue = get( store.getState(), reducerKey ); | ||
if ( newStateValue !== currentStateValue ) { | ||
currentStateValue = newStateValue; | ||
const stateToSave = get( reducer( store.getState(), { type: 'SERIALIZE' } ), reducerKey ); | ||
getPersistenceStorage().setItem( storageKey, JSON.stringify( stateToSave ) ); | ||
} | ||
} ); | ||
} |
Oops, something went wrong.