From a6b33bdbffed2e4cd8b7c507deecf7d25839d517 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Mon, 11 Mar 2019 12:21:01 +0100 Subject: [PATCH] Support parent/child registries --- packages/data/src/registry.js | 21 ++++++++++--- packages/data/src/test/registry.js | 48 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/packages/data/src/registry.js b/packages/data/src/registry.js index e1c324f6ef8ac0..15f02180c7b859 100644 --- a/packages/data/src/registry.js +++ b/packages/data/src/registry.js @@ -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 = []; @@ -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 ); } /** @@ -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 ); } // @@ -172,5 +181,9 @@ export function createRegistry( storeConfigs = {} ) { ( [ name, config ] ) => registry.registerStore( name, config ) ); + if ( parent ) { + parent.subscribe( globalListener ); + } + return withPlugins( registry ); } diff --git a/packages/data/src/test/registry.js b/packages/data/src/test/registry.js index cd18ac2d892854..b828cd01991596 100644 --- a/packages/data/src/test/registry.js +++ b/packages/data/src/test/registry.js @@ -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(); + } ); + } ); } );