Skip to content
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

Data Module: Support parent/child registries #14369

Merged
merged 2 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ configurations.
_Parameters_

- _storeConfigs_ `Object`: Initial store configurations.
- _parent_ `?Object`: Parent registry.

_Returns_

Expand Down
21 changes: 17 additions & 4 deletions packages/data/src/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ import createCoreDataStore from './store';
* Creates a new store registry, given an optional object of initial store
* configurations.
*
* @param {Object} storeConfigs Initial store configurations.
* @param {Object} storeConfigs Initial store configurations.
* @param {Object?} parent Parent registry.
*
* @return {WPDataRegistry} Data registry.
*/
export function createRegistry( storeConfigs = {} ) {
export function createRegistry( storeConfigs = {}, parent = null ) {
const stores = {};
let listeners = [];

Expand Down Expand Up @@ -74,7 +75,11 @@ export function createRegistry( storeConfigs = {} ) {
*/
function select( reducerKey ) {
const store = stores[ reducerKey ];
return store && store.getSelectors();
if ( store ) {
return store.getSelectors();
}

return parent && parent.select( reducerKey );
}

/**
Expand All @@ -87,7 +92,11 @@ export function createRegistry( storeConfigs = {} ) {
*/
function dispatch( reducerKey ) {
const store = stores[ reducerKey ];
return store && store.getActions();
if ( store ) {
return store.getActions();
}

return parent && parent.dispatch( reducerKey );
}

//
Expand Down Expand Up @@ -172,5 +181,9 @@ export function createRegistry( storeConfigs = {} ) {
( [ name, config ] ) => registry.registerStore( name, config )
);

if ( parent ) {
parent.subscribe( globalListener );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nice how simply this works 👍

It may prove to cause #13177 to be more difficult to implement, if we choose to pursue it again in the future (specifically, the change in signature to subscribe( listener[, reducerKeys ] ).

}

return withPlugins( registry );
}
48 changes: 48 additions & 0 deletions packages/data/src/test/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,4 +604,52 @@ describe( 'createRegistry', () => {
expect( registry.select() ).toBe( 10 );
} );
} );

describe( 'parent registry', () => {
it( 'should call parent registry selectors/actions if defined', () => {
const mySelector = jest.fn();
const myAction = jest.fn();
const getSelectors = () => ( { mySelector } );
const getActions = () => ( { myAction } );
const subscribe = () => {};
registry.registerGenericStore( 'store', { getSelectors, getActions, subscribe } );
const subRegistry = createRegistry( {}, registry );

subRegistry.select( 'store' ).mySelector();
subRegistry.dispatch( 'store' ).myAction();

expect( mySelector ).toHaveBeenCalled();
expect( myAction ).toHaveBeenCalled();
} );

it( 'should override existing store in parent registry', () => {
const mySelector = jest.fn();
const myAction = jest.fn();
const getSelectors = () => ( { mySelector } );
const getActions = () => ( { myAction } );
const subscribe = () => {};
registry.registerGenericStore( 'store', { getSelectors, getActions, subscribe } );

const subRegistry = createRegistry( {}, registry );
const mySelector2 = jest.fn();
const myAction2 = jest.fn();
const getSelectors2 = () => ( { mySelector: mySelector2 } );
const getActions2 = () => ( { myAction: myAction2 } );
const subscribe2 = () => {};
subRegistry.registerGenericStore( 'store', {
getSelectors: getSelectors2,
getActions: getActions2,
subscribe: subscribe2,
} );

subRegistry.select( 'store' ).mySelector();
subRegistry.dispatch( 'store' ).myAction();

expect( mySelector ).not.toHaveBeenCalled();
expect( myAction ).not.toHaveBeenCalled();

expect( mySelector2 ).toHaveBeenCalled();
expect( myAction2 ).toHaveBeenCalled();
} );
} );
} );