Skip to content

Commit a6b33bd

Browse files
committed
Support parent/child registries
1 parent 1e8d781 commit a6b33bd

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

packages/data/src/registry.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ import createCoreDataStore from './store';
3434
* Creates a new store registry, given an optional object of initial store
3535
* configurations.
3636
*
37-
* @param {Object} storeConfigs Initial store configurations.
37+
* @param {Object} storeConfigs Initial store configurations.
38+
* @param {Object?} parent Parent registry.
3839
*
3940
* @return {WPDataRegistry} Data registry.
4041
*/
41-
export function createRegistry( storeConfigs = {} ) {
42+
export function createRegistry( storeConfigs = {}, parent = null ) {
4243
const stores = {};
4344
let listeners = [];
4445

@@ -74,7 +75,11 @@ export function createRegistry( storeConfigs = {} ) {
7475
*/
7576
function select( reducerKey ) {
7677
const store = stores[ reducerKey ];
77-
return store && store.getSelectors();
78+
if ( store ) {
79+
return store.getSelectors();
80+
}
81+
82+
return parent && parent.select( reducerKey );
7883
}
7984

8085
/**
@@ -87,7 +92,11 @@ export function createRegistry( storeConfigs = {} ) {
8792
*/
8893
function dispatch( reducerKey ) {
8994
const store = stores[ reducerKey ];
90-
return store && store.getActions();
95+
if ( store ) {
96+
return store.getActions();
97+
}
98+
99+
return parent && parent.dispatch( reducerKey );
91100
}
92101

93102
//
@@ -172,5 +181,9 @@ export function createRegistry( storeConfigs = {} ) {
172181
( [ name, config ] ) => registry.registerStore( name, config )
173182
);
174183

184+
if ( parent ) {
185+
parent.subscribe( globalListener );
186+
}
187+
175188
return withPlugins( registry );
176189
}

packages/data/src/test/registry.js

+48
Original file line numberDiff line numberDiff line change
@@ -604,4 +604,52 @@ describe( 'createRegistry', () => {
604604
expect( registry.select() ).toBe( 10 );
605605
} );
606606
} );
607+
608+
describe( 'parent registry', () => {
609+
it( 'should call parent registry selectors/actions if defined', () => {
610+
const mySelector = jest.fn();
611+
const myAction = jest.fn();
612+
const getSelectors = () => ( { mySelector } );
613+
const getActions = () => ( { myAction } );
614+
const subscribe = () => {};
615+
registry.registerGenericStore( 'store', { getSelectors, getActions, subscribe } );
616+
const subRegistry = createRegistry( {}, registry );
617+
618+
subRegistry.select( 'store' ).mySelector();
619+
subRegistry.dispatch( 'store' ).myAction();
620+
621+
expect( mySelector ).toHaveBeenCalled();
622+
expect( myAction ).toHaveBeenCalled();
623+
} );
624+
625+
it( 'should override existing store in parent registry', () => {
626+
const mySelector = jest.fn();
627+
const myAction = jest.fn();
628+
const getSelectors = () => ( { mySelector } );
629+
const getActions = () => ( { myAction } );
630+
const subscribe = () => {};
631+
registry.registerGenericStore( 'store', { getSelectors, getActions, subscribe } );
632+
633+
const subRegistry = createRegistry( {}, registry );
634+
const mySelector2 = jest.fn();
635+
const myAction2 = jest.fn();
636+
const getSelectors2 = () => ( { mySelector: mySelector2 } );
637+
const getActions2 = () => ( { myAction: myAction2 } );
638+
const subscribe2 = () => {};
639+
subRegistry.registerGenericStore( 'store', {
640+
getSelectors: getSelectors2,
641+
getActions: getActions2,
642+
subscribe: subscribe2,
643+
} );
644+
645+
subRegistry.select( 'store' ).mySelector();
646+
subRegistry.dispatch( 'store' ).myAction();
647+
648+
expect( mySelector ).not.toHaveBeenCalled();
649+
expect( myAction ).not.toHaveBeenCalled();
650+
651+
expect( mySelector2 ).toHaveBeenCalled();
652+
expect( myAction2 ).toHaveBeenCalled();
653+
} );
654+
} );
607655
} );

0 commit comments

Comments
 (0)