-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove wp.data plugin support #12004
Closed
coderkevin
wants to merge
3
commits into
WordPress:master
from
coderkevin:remove/wp-data-plugin-support
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,134 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { pick } from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import defaultStorage from './storage/default'; | ||
|
||
/** | ||
* Persistence options. | ||
* | ||
* @property {Storage} storage Persistent storage implementation. This must | ||
* at least implement `getItem` and `setItem` of | ||
* the Web Storage API. | ||
* @property {string} storageKey Key on which to set in persistent storage. | ||
* | ||
* @typedef {WPDataPersistenceOptions} | ||
*/ | ||
|
||
/** | ||
* Default persistence options. | ||
*/ | ||
let DEFAULT_OPTIONS = { | ||
storage: defaultStorage, | ||
storageKey: 'WP_DATA', | ||
}; | ||
|
||
/** | ||
* Sets default options for the persistence. | ||
* This function merges the options given with the defaults already set. | ||
* | ||
* @param {WPDataPersistenceOptions} options The defaults to be set (e.g. 'storage', or 'storageKey') | ||
*/ | ||
export function setDefaults( options ) { | ||
DEFAULT_OPTIONS = { ...DEFAULT_OPTIONS, ...options }; | ||
} | ||
|
||
/** | ||
* Higher-order reducer to provides an initial value when state is undefined. | ||
* | ||
* @param {Function} reducer Original reducer. | ||
* @param {*} initialState Value to use as initial state. | ||
* | ||
* @return {Function} Enhanced reducer. | ||
*/ | ||
export function withInitialState( reducer, initialState ) { | ||
return ( state = initialState, action ) => { | ||
return reducer( state, action ); | ||
}; | ||
} | ||
|
||
/** | ||
* Creates a persistence interface, exposing getter and setter methods (`get` | ||
* and `set` respectively). | ||
* | ||
* @param {WPDataPersistenceOptions} options Persistence options. | ||
* | ||
* @return {Object} Persistence interface. | ||
*/ | ||
export function createPersistenceInterface( options ) { | ||
const { storage, storageKey } = { ...DEFAULT_OPTIONS, ...options }; | ||
|
||
let data; | ||
|
||
/** | ||
* Returns the persisted data as an object, defaulting to an empty object. | ||
* | ||
* @return {Object} Persisted data. | ||
*/ | ||
function get() { | ||
if ( data === undefined ) { | ||
// If unset, getItem is expected to return null. Fall back to | ||
// empty object. | ||
const persisted = storage.getItem( storageKey ); | ||
if ( persisted === null ) { | ||
data = {}; | ||
} else { | ||
try { | ||
data = JSON.parse( persisted ); | ||
} catch ( error ) { | ||
// Similarly, should any error be thrown during parse of | ||
// the string (malformed JSON), fall back to empty object. | ||
data = {}; | ||
} | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
|
||
/** | ||
* Merges an updated reducer state into the persisted data. | ||
* | ||
* @param {string} key Key to update. | ||
* @param {*} value Updated value. | ||
*/ | ||
function set( key, value ) { | ||
data = { ...data, [ key ]: value }; | ||
storage.setItem( storageKey, JSON.stringify( data ) ); | ||
} | ||
|
||
return { get, set }; | ||
} | ||
|
||
/** | ||
* Creates an enhanced store dispatch function, triggering the state of the | ||
* given reducer key to be persisted when changed. | ||
* | ||
* @param {Function} getState Function which returns current state. | ||
* @param {string} reducerKey Reducer key. | ||
* @param {?Array<string>} keys Optional subset of keys to save. | ||
* @param {Object} persistence The persistence interface to be used. | ||
* | ||
* @return {Function} Enhanced dispatch function. | ||
*/ | ||
export function createPersistOnChange( getState, reducerKey, keys, persistence ) { | ||
let lastState = getState(); | ||
|
||
return ( result ) => { | ||
let state = getState(); | ||
if ( state !== lastState ) { | ||
if ( Array.isArray( keys ) ) { | ||
state = pick( state, keys ); | ||
} | ||
|
||
persistence.set( reducerKey, state ); | ||
lastState = state; | ||
} | ||
|
||
return result; | ||
}; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the naming of this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a huge fan of this approach either, honestly. It's due to an artifact of the global registry. Feel free to rename or modify as you prefer.