-
Notifications
You must be signed in to change notification settings - Fork 2k
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
SDK: Try wp.data with calypso state tree #26838
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4e0f7a6
state: Try wp.data with calypso state tree
coderkevin 4befbc1
state: Action handling for wp.data calypso plugin
coderkevin 1388482
state: wp.data adapter adjustments
coderkevin bcc4b32
state: Add WP Data Provider to ReduxWrappedLayout
coderkevin 17b8bda
wp-data: Put preferences in `calypso/preferences`
coderkevin d7324b4
State: Use more general-purpose wp.data plugins.
coderkevin 6d8a6cf
wp-data: internals-plugin, mutate registry in use
coderkevin 90b204b
wp-data: custom-store-plugin, adjust subscribe
coderkevin c5a4515
Update shrinkwrap
coderkevin 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import preferences from './preferences'; | ||
|
||
export function registerStores( registry, calypsoStore ) { | ||
registry.registerStore( 'calypso/preferences', preferences, calypsoStore ); | ||
}; |
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,19 @@ | ||
/** @format */ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { fetchPreferences, setPreference, savePreference } from 'state/preferences/actions'; | ||
import { getPreference, isFetchingPreferences } from 'state/preferences/selectors'; | ||
|
||
export default { | ||
selectors: { | ||
getPreference, | ||
isFetchingPreferences, | ||
}, | ||
actions: { | ||
fetchPreferences, | ||
setPreference, | ||
savePreference, | ||
}, | ||
}; |
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,40 @@ | ||
/** @format */ | ||
/** | ||
* External dependencies | ||
*/ | ||
import React, { Component } from 'react'; | ||
import { createRegistry, RegistryProvider } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import internalsPlugin from './plugins/internals-plugin'; | ||
import customStorePlugin from './plugins/custom-store-plugin'; | ||
import { registerStores } from './calypso-stores'; | ||
|
||
class CalypsoWPDataProvider extends Component { | ||
constructor( props ) { | ||
super( ...arguments ); | ||
this.updateRegistry( props.calypsoStore ); | ||
} | ||
|
||
updateRegistry( calypsoStore ) { | ||
this.registry = createRegistry() | ||
.use( internalsPlugin ) | ||
.use( customStorePlugin ); | ||
|
||
registerStores( this.registry, calypsoStore ); | ||
} | ||
|
||
componentDidUpdate( prevProps ) { | ||
if ( prevProps.calypsoStore !== this.props.calypsoStore ) { | ||
this.updateRegistry( this.props.calypsoStore ); | ||
} | ||
} | ||
|
||
render() { | ||
return <RegistryProvider value={ this.registry }>{ this.props.children }</RegistryProvider>; | ||
} | ||
} | ||
|
||
export default CalypsoWPDataProvider; |
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,5 @@ | ||
/** @format */ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
export { default as CalypsoWPDataProvider } from './calypso-wp-data-provider'; |
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,88 @@ | ||
/** @format */ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { findKey } from 'lodash'; | ||
|
||
/** | ||
* Creates a plugin that allows custom store implementations. | ||
* | ||
* IMPORTANT: This plugin depends on the `internals-plugin` to work. | ||
* It must be given a registry already using that plugin. | ||
* | ||
* @param {Object} registry The original registry to wrap. | ||
* @return {Object} The wrapped registry from this plugin. | ||
*/ | ||
function customStorePlugin( registry ) { | ||
if ( ! registry._internals ) { | ||
throw TypeError( 'Expected a registry with _internals. See internals-plugin' ); | ||
} | ||
|
||
/** | ||
* Registers a custom store with actions and selectors. | ||
* | ||
* @param {string} reducerKey Reducer key. | ||
* @param {Object} store Existing redux-like store to be used. | ||
* @return {Object} Store originally given, now registered. | ||
*/ | ||
function registerCustomStore( reducerKey, store ) { | ||
const { globalListener, namespaces } = registry._internals; | ||
const existingStoreKey = findKey( namespaces, value => value.store === store ); | ||
|
||
namespaces[ reducerKey ] = { store }; | ||
|
||
if ( ! existingStoreKey ) { | ||
// Customize subscribe behavior to call listeners only on effective change, | ||
// not on every dispatch. | ||
let lastState = store.getState(); | ||
store.subscribe( () => { | ||
const state = store.getState(); | ||
const hasChanged = state !== lastState; | ||
lastState = state; | ||
|
||
if ( hasChanged ) { | ||
globalListener(); | ||
} | ||
} ); | ||
} | ||
|
||
return store; | ||
} | ||
|
||
/** | ||
* Registers a store within the registry. | ||
* | ||
* @param {String} reducerKey The desired key to denote this store. | ||
* @param {Object} options Options for store, such as actions, selectors. | ||
* @param {Object} [customStore] Optional custom store to use instead of creating one. | ||
* @return {Object} The newly registered store. | ||
*/ | ||
function registerStore( reducerKey, options, customStore ) { | ||
if ( customStore ) { | ||
const store = registerCustomStore( reducerKey, customStore ); | ||
|
||
if ( options.actions ) { | ||
registry.registerActions( reducerKey, options.actions ); | ||
} | ||
|
||
if ( options.selectors ) { | ||
registry.registerSelectors( reducerKey, options.selectors ); | ||
} | ||
|
||
if ( options.resolvers ) { | ||
registry.registerResolvers( reducerKey, options.resolvers ); | ||
} | ||
|
||
return store; | ||
} | ||
|
||
// No store given in options, pass through to normal operation. | ||
return registry.registerStore( reducerKey, options ); | ||
} | ||
|
||
return { | ||
registerStore, | ||
}; | ||
} | ||
|
||
export default customStorePlugin; |
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.
I think this needs to go to
client/layout
instead, at least for the purpose of this PR.RootChild
is mostly used by modals, whereas the color scheme picker (like most components) is a direct descendant ofclient/layout
. I think that in this PR,withSelect()
andwithDispatch()
are currently picking up a 'global' registry instance (inside@wp/data
).I think I got this to work so it picks up the correct registry in my PR, #26930.
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.
Thanks for the tip. I'll adjust this one too.
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.
Would it make sense to have the WP Data Provider in both places? Both here and in ReduxWrappedLayout?
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.
Addressed in e9be45c
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.
Eventually, we'll need that Provider at the top of all component hierarchies contain components wrapped in
withSelect
and/orwithDispatch
, but for the sake of this PR, wrapping only inReduxWrappedLayout
should be enough.(We might end up writing a HOC that contains both the Redux and Calypso WP Data Provider, and wrap all component hierarchies in that.)