From 0e46b9a2761110675a9743089e81797edd00d9d9 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Wed, 27 Jun 2018 10:02:42 +0100 Subject: [PATCH] Provide the default registry by using the default value of the createContext API --- packages/data/src/components/registry-provider/index.js | 7 ++++++- packages/data/src/components/with-dispatch/index.js | 7 ++----- packages/data/src/components/with-select/index.js | 7 ++----- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/data/src/components/registry-provider/index.js b/packages/data/src/components/registry-provider/index.js index 7b86a20a0cfca..c296f46a1ab1a 100644 --- a/packages/data/src/components/registry-provider/index.js +++ b/packages/data/src/components/registry-provider/index.js @@ -3,7 +3,12 @@ */ import { createContext } from '@wordpress/element'; -const { Consumer, Provider } = createContext( null ); +/** + * Internal dependencies + */ +import defaultRegistry from '../../default-registry'; + +const { Consumer, Provider } = createContext( defaultRegistry ); export const RegistryConsumer = Consumer; diff --git a/packages/data/src/components/with-dispatch/index.js b/packages/data/src/components/with-dispatch/index.js index ef41c38d0575b..55bb0d13db1fc 100644 --- a/packages/data/src/components/with-dispatch/index.js +++ b/packages/data/src/components/with-dispatch/index.js @@ -17,7 +17,6 @@ import { /** * Internal dependencies */ -import defaultRegistry from '../../default-registry'; import { RegistryConsumer } from '../registry-provider'; /** @@ -50,15 +49,13 @@ const withDispatch = ( mapDispatchToProps ) => createHigherOrderComponent( proxyDispatch( propName, ...args ) { // Original dispatcher is a pre-bound (dispatching) action creator. - const dispatch = this.props.registry ? this.props.registry.dispatch : defaultRegistry.dispatch; - mapDispatchToProps( dispatch, this.props.ownProps )[ propName ]( ...args ); + mapDispatchToProps( this.props.registry.dispatch, this.props.ownProps )[ propName ]( ...args ); } setProxyProps( props ) { // Assign as instance property so that in reconciling subsequent // renders, the assigned prop values are referentially equal. - const dispatch = props.registry ? props.registry.dispatch : defaultRegistry.dispatch; - const propsToDispatchers = mapDispatchToProps( dispatch, props.ownProps ); + const propsToDispatchers = mapDispatchToProps( this.props.registry.dispatch, props.ownProps ); this.proxyProps = mapValues( propsToDispatchers, ( dispatcher, propName ) => { // Prebind with prop name so we have reference to the original // dispatcher to invoke. Track between re-renders to avoid diff --git a/packages/data/src/components/with-select/index.js b/packages/data/src/components/with-select/index.js index 486018df139de..0d46129aebcd3 100644 --- a/packages/data/src/components/with-select/index.js +++ b/packages/data/src/components/with-select/index.js @@ -11,7 +11,6 @@ import isShallowEqual from '@wordpress/is-shallow-equal'; /** * Internal dependencies */ -import defaultRegistry from '../../default-registry'; import { RegistryConsumer } from '../registry-provider'; /** @@ -40,9 +39,8 @@ const withSelect = ( mapStateToProps ) => createHigherOrderComponent( ( WrappedC // A constant value is used as the fallback since it can be more // efficiently shallow compared in case component is repeatedly // rendered without its own merge props. - const select = props.registry ? props.registry.select : defaultRegistry.select; const mergeProps = ( - mapStateToProps( select, props.ownProps ) || + mapStateToProps( props.registry.select, props.ownProps ) || DEFAULT_MERGE_PROPS ); @@ -66,8 +64,7 @@ const withSelect = ( mapStateToProps ) => createHigherOrderComponent( ( WrappedC } subscribe() { - const subscribe = this.props.registry ? this.props.registry.subscribe : defaultRegistry.subscribe; - this.unsubscribe = subscribe( () => { + this.unsubscribe = this.props.registry.subscribe( () => { if ( ! this.canRunSelection ) { return; }