Skip to content

Commit

Permalink
Use const empty array
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Nov 3, 2020
1 parent a04d880 commit 6b1d661
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions packages/core-data/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ import { getQueriedItems } from './queried-data';
import { DEFAULT_ENTITY_KEY } from './entities';
import { getNormalizedCommaSeparable } from './utils';

/**
* 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 true if a request is in progress for embed preview data, or false
* otherwise.
Expand Down Expand Up @@ -217,9 +228,6 @@ export function hasEntityRecords( state, kind, name, query ) {
return Array.isArray( getEntityRecords( state, kind, name, query ) );
}

const getQueriedState = ( state, kind, name ) =>
get( state.entities.data, [ kind, name, 'queriedData' ] );

/**
* Returns the Entity's records.
*
Expand All @@ -230,20 +238,21 @@ const getQueriedState = ( state, kind, name ) =>
*
* @return {?Array} Records.
*/
export const getEntityRecords = createSelector(
( state, kind, name, query ) => {
// Queried data state is prepopulated for all known entities. If this is not
// assigned for the given parameters, then it is known to not exist. Thus, a
// return value of an empty array is used instead of `null` (where `null` is
// otherwise used to represent an unknown state).
const queriedState = getQueriedState( state, kind, name );
if ( ! queriedState ) {
return [];
}
return getQueriedItems( queriedState, query );
},
( state, kind, name ) => [ getQueriedState( state, kind, name ) ]
);
export function getEntityRecords( state, kind, name, query ) {
// Queried data state is prepopulated for all known entities. If this is not
// assigned for the given parameters, then it is known to not exist. Thus, a
// return value of an empty array is used instead of `null` (where `null` is
// otherwise used to represent an unknown state).
const queriedState = get( state.entities.data, [
kind,
name,
'queriedData',
] );
if ( ! queriedState ) {
return EMPTY_ARRAY;
}
return getQueriedItems( queriedState, query );
}

/**
* Returns the list of dirty entity records.
Expand Down

0 comments on commit 6b1d661

Please sign in to comment.