Skip to content

Commit

Permalink
state: wp.data adapter adjustments
Browse files Browse the repository at this point in the history
This addresses a couple of PR comments about some optimization and
additional commenting.
  • Loading branch information
coderkevin committed Aug 27, 2018
1 parent f957c81 commit 20b3b95
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
25 changes: 24 additions & 1 deletion client/state/wp-data/calypso-registry-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const calypsoRegistryPlugin = calypsoStore => registry => {
calypsoStore.subscribe( () => {
const calypsoState = calypsoStore.getState();
const hasChanged = calypsoState !== lastCalypsoState;
lastCalypsoState = calypsoState;

if ( hasChanged ) {
lastCalypsoState = calypsoState;
calypsoListener();
}
} );
Expand All @@ -46,6 +46,12 @@ const calypsoRegistryPlugin = calypsoStore => registry => {
namespaces[ reducerKey ].actions = mapValues( newActions, createBoundAction );
}

/**
* Registers a store within the registry.
* @param {String} reducerKey The desired key to denote this store.
* @param {Object} options The options for the store.
* @return {Object} The newly registered store.
*/
function registerStore( reducerKey, options ) {
if ( options.useCalypsoStore ) {
return registerToCalypsoStore( reducerKey, options );
Expand All @@ -55,6 +61,13 @@ const calypsoRegistryPlugin = calypsoStore => registry => {
return registry.registerStore( reducerKey, options );
}

/**
* Subscribe to registry change events.
* This subscribes to all store updates in the registry,
* calypso and otherwise.
* @param {Function} listener The listener function to be added.
* @returns {Function} The unsubscribe function for this listener.
*/
function subscribe( listener ) {
listeners.push( listener );
const registryUnsub = registry.subscribe( listener );
Expand All @@ -65,6 +78,11 @@ const calypsoRegistryPlugin = calypsoStore => registry => {
};
}

/**
* Gets selectors for a desired store.
* @param {String} reducerKey The reducerKey for the desired store.
* @return {Object} A mapping of selectors for the desired store.
*/
function select( reducerKey ) {
const namespace = namespaces[ reducerKey ];
if ( namespace ) {
Expand All @@ -73,6 +91,11 @@ const calypsoRegistryPlugin = calypsoStore => registry => {
return registry.select( reducerKey );
}

/**
* Gets action creators for a desired store.
* @param {String} reducerKey The reducerKey for the desired store.
* @return {Object} Action creators mapped to dispatch for the desired store.
*/
function dispatch( reducerKey ) {
const namespace = namespaces[ reducerKey ];
if ( namespace ) {
Expand Down
8 changes: 2 additions & 6 deletions client/state/wp-data/calypso-wp-data-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@ import calypsoRegistryPlugin from './calypso-registry-plugin';
import calypsoWPDataStore from './calypso-wp-data-store';

class CalypsoWPDataProvider extends Component {
constructor() {
constructor( props ) {
super( ...arguments );
this.registry = undefined;
this.updateRegistry( props.calypsoStore );
}

updateRegistry( calypsoStore ) {
this.registry = use( calypsoRegistryPlugin( calypsoStore ) );
this.registry.registerStore( 'calypso', calypsoWPDataStore );
}

componentDidMount() {
this.updateRegistry( this.props.calypsoStore );
}

componentDidUpdate( prevProps ) {
if ( prevProps.calypsoStore !== this.props.calypsoStore ) {
this.updateRegistry( this.props.calypsoStore );
Expand Down

0 comments on commit 20b3b95

Please sign in to comment.