-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
State: Use more general-purpose wp.data plugins.
This creates a `custom-store-plugin` which is more general-purpose than the previous `calypso-registry-plugin`. It allows any pre-existing redux store to be used for a given wp.data store instead of wp.data creating one itself. This allows for two things: 1. Stores can use enhancers and middleware, which is not possible in the default wp.data configuration. 2. wp.data stores can share redux stores. Note: This also creates an `internals-plugin`, which is necessary to avoid the reimplementing of internals within the `calypso-registry-plugin`. The `use` function of the `internals-plugin` will be submitted as a PR to core Gutenberg as well so perhaps it will not be necessary for long.
- Loading branch information
1 parent
17b8bda
commit d7324b4
Showing
7 changed files
with
590 additions
and
130 deletions.
There are no files selected for viewing
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
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,81 @@ | ||
/** @format */ | ||
|
||
/** | ||
* 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; | ||
|
||
namespaces[ reducerKey ] = { store }; | ||
|
||
// 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.