From 2fc48e9ab6cddfb0904e5582c39d3c333cba0e9e Mon Sep 17 00:00:00 2001 From: Anton Timmermans Date: Wed, 21 Nov 2018 10:35:19 +0100 Subject: [PATCH] Rename emptyArray to EMPTY_ARRAY --- packages/annotations/src/store/selectors.js | 17 +++++++++++++---- packages/rich-text/src/register-format-type.js | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/annotations/src/store/selectors.js b/packages/annotations/src/store/selectors.js index f161b94a79fea7..a39a315c92f907 100644 --- a/packages/annotations/src/store/selectors.js +++ b/packages/annotations/src/store/selectors.js @@ -4,7 +4,16 @@ import createSelector from 'rememo'; import { get, flatMap } from 'lodash'; -const emptyArray = []; +/** + * Shared reference to an empty array for cases where it is important to avoid + * returning a new array reference on every invocation, as in a connected or + * other pure component which performs `shouldComponentUpdate` check on props. + * This should be used as a last resort, since the normalized data should be + * maintained by the reducer result in state. + * + * @type {Array} + */ +const EMPTY_ARRAY = []; /** * Returns the annotations for a specific client ID. @@ -21,12 +30,12 @@ export const __experimentalGetAnnotationsForBlock = createSelector( } ); }, ( state, blockClientId ) => [ - get( state, blockClientId, emptyArray ), + get( state, blockClientId, EMPTY_ARRAY ), ] ); export const __experimentalGetAllAnnotationsForBlock = function( state, blockClientId ) { - return get( state, blockClientId, [] ); + return get( state, blockClientId, EMPTY_ARRAY ); }; /** @@ -56,7 +65,7 @@ export const __experimentalGetAnnotationsForRichText = createSelector( } ); }, ( state, blockClientId ) => [ - get( state, blockClientId, emptyArray ), + get( state, blockClientId, EMPTY_ARRAY ), ] ); diff --git a/packages/rich-text/src/register-format-type.js b/packages/rich-text/src/register-format-type.js index 1fd7206ad5892c..262812a22b248f 100644 --- a/packages/rich-text/src/register-format-type.js +++ b/packages/rich-text/src/register-format-type.js @@ -11,6 +11,17 @@ import { select, dispatch, withSelect, withDispatch } from '@wordpress/data'; import { addFilter } from '@wordpress/hooks'; import { compose } from '@wordpress/compose'; +/** + * Shared reference to an empty array for cases where it is important to avoid + * returning a new array reference on every invocation, as in a connected or + * other pure component which performs `shouldComponentUpdate` check on props. + * This should be used as a last resort, since the normalized data should be + * maintained by the reducer result in state. + * + * @type {Array} + */ +const EMPTY_ARRAY = []; + /** * Registers a new format provided a unique name and an object defining its * behavior. @@ -120,8 +131,7 @@ export function registerFormatType( name, settings ) { dispatch( 'core/rich-text' ).addFormatTypes( settings ); - const emptyArray = []; - const getFunctionStackMemoized = memize( ( previousStack = emptyArray, newFunction ) => { + const getFunctionStackMemoized = memize( ( previousStack = EMPTY_ARRAY, newFunction ) => { return [ ...previousStack, newFunction,