Skip to content

Commit

Permalink
Support parent/child registries
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Mar 28, 2019
1 parent 1e8d781 commit a6b33bd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
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 );
}

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();
} );
} );
} );

0 comments on commit a6b33bd

Please sign in to comment.