-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data: Add persistence via data plugin interface
- Loading branch information
Showing
16 changed files
with
224 additions
and
428 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
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,29 @@ | ||
{ | ||
"name": "@wordpress/data-plugin-persistence", | ||
"version": "1.0.1", | ||
"description": "Data module plugin for persistence.", | ||
"author": "The WordPress Contributors", | ||
"license": "GPL-2.0-or-later", | ||
"keywords": [ | ||
"wordpress", | ||
"data" | ||
], | ||
"homepage": "https://github.com/WordPress/gutenberg/tree/master/packages/data-plugin-persistence/README.md", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/WordPress/gutenberg.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/WordPress/gutenberg/issues" | ||
}, | ||
"main": "build/index.js", | ||
"module": "build-module/index.js", | ||
"react-native": "src/index", | ||
"dependencies": { | ||
"@babel/runtime": "^7.0.0-beta.52", | ||
"@wordpress/is-shallow-equal": "file:../is-shallow-equal" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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,120 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import isShallowEqual from '@wordpress/is-shallow-equal'; | ||
|
||
// Defaults to the local storage. | ||
let persistenceStorage; | ||
|
||
/** | ||
* Sets a different persistence storage. | ||
* | ||
* @param {Object} storage Persistence storage. | ||
*/ | ||
export function setPersistenceStorage( storage ) { | ||
persistenceStorage = storage; | ||
} | ||
|
||
/** | ||
* Get the persistence storage handler. | ||
* | ||
* @return {Object} Persistence storage. | ||
*/ | ||
export function getPersistenceStorage() { | ||
return persistenceStorage || window.localStorage; | ||
} | ||
|
||
/** | ||
* Higher-order reducer used to persist just one key from the reducer state. | ||
* | ||
* @param {function} reducer Reducer function. | ||
* @param {string} keyToPersist The reducer key to persist. | ||
* | ||
* @return {function} Updated reducer. | ||
*/ | ||
export function restrictPersistence( reducer, keyToPersist ) { | ||
return ( state, action ) => { | ||
const nextState = reducer( state, action ); | ||
|
||
if ( action.type === 'SERIALIZE' ) { | ||
// Returning the same instance if the state is kept identical avoids reserializing again | ||
if ( | ||
action.previousState && | ||
action.previousState[ keyToPersist ] === nextState[ keyToPersist ] | ||
) { | ||
return action.previousState; | ||
} | ||
|
||
return { [ keyToPersist ]: nextState[ keyToPersist ] }; | ||
} | ||
|
||
return nextState; | ||
}; | ||
} | ||
|
||
export function createPersistencePlugin( storageKey ) { | ||
return ( registry ) => { | ||
const { namespaces, subscribe } = registry; | ||
const persisted = Object.create( null ); | ||
|
||
// Load initially persisted value | ||
const persistedString = getPersistenceStorage().getItem( storageKey ); | ||
const persistedData = persistedString ? JSON.parse( persistedString ) : {}; | ||
let previousValue = null; | ||
|
||
const triggerPersist = () => { | ||
const newValue = Object.entries( namespaces ) | ||
.filter( ( [ reducerKey ] ) => persisted[ reducerKey ] ) | ||
.reduce( ( memo, [ reducerKey, { reducer, store } ] ) => { | ||
const action = { type: 'SERIALIZE' }; | ||
if ( previousValue ) { | ||
action.previousState = previousValue[ reducerKey ]; | ||
} | ||
|
||
memo[ reducerKey ] = reducer( store.getState(), action ); | ||
return memo; | ||
}, {} ); | ||
|
||
if ( ! isShallowEqual( newValue, previousValue ) ) { | ||
getPersistenceStorage().setItem( storageKey, JSON.stringify( newValue ) ); | ||
} | ||
|
||
previousValue = newValue; | ||
}; | ||
|
||
function withInitialState( reducerKey, reducer ) { | ||
return ( state, action ) => { | ||
if ( state === undefined ) { | ||
state = persistedData[ reducerKey ]; | ||
} | ||
|
||
return reducer( state, action ); | ||
}; | ||
} | ||
|
||
// Persist updated preferences | ||
subscribe( triggerPersist ); | ||
|
||
return { | ||
registerStore( reducerKey, options ) { | ||
options = { | ||
...options, | ||
reducer: withInitialState( reducerKey, options.reducer ), | ||
}; | ||
|
||
if ( options.persist ) { | ||
persisted[ reducerKey ] = true; | ||
|
||
if ( Array.isArray( options.persist ) ) { | ||
options.reducer = options.persist.reduce( | ||
restrictPersistence, | ||
options.reducer, | ||
); | ||
} | ||
} | ||
|
||
return registry.registerStore( reducerKey, options ); | ||
}, | ||
}; | ||
}; | ||
} |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.