Skip to content

Commit

Permalink
Block editor: selectors: avoid has() or double get() on Maps (#58372)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Jan 29, 2024
1 parent 252418b commit e3f4f76
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,13 @@ export function getBlock( state, clientId ) {

export const __unstableGetBlockWithoutInnerBlocks = createSelector(
( state, clientId ) => {
if ( ! state.blocks.byClientId.has( clientId ) ) {
const block = state.blocks.byClientId.get( clientId );
if ( ! block ) {
return null;
}

return {
...state.blocks.byClientId.get( clientId ),
...block,
attributes: getBlockAttributes( state, clientId ),
};
},
Expand Down Expand Up @@ -540,9 +541,7 @@ export function getSelectedBlock( state ) {
* @return {?string} Root client ID, if exists
*/
export function getBlockRootClientId( state, clientId ) {
return state.blocks.parents.has( clientId )
? state.blocks.parents.get( clientId )
: null;
return state.blocks.parents.get( clientId ) ?? null;
}

/**
Expand All @@ -558,8 +557,7 @@ export const getBlockParents = createSelector(
( state, clientId, ascending = false ) => {
const parents = [];
let current = clientId;
while ( !! state.blocks.parents.get( current ) ) {
current = state.blocks.parents.get( current );
while ( ( current = state.blocks.parents.get( current ) ) ) {
parents.push( current );
}

Expand Down Expand Up @@ -2737,13 +2735,10 @@ export const __unstableGetContentLockingParent = createSelector(
( state, clientId ) => {
let current = clientId;
let result;
while ( state.blocks.parents.has( current ) ) {
current = state.blocks.parents.get( current );
while ( ( current = state.blocks.parents.get( current ) ) ) {
if (
( current &&
getBlockName( state, current ) === 'core/block' ) ||
( current &&
getTemplateLock( state, current ) === 'contentOnly' )
getBlockName( state, current ) === 'core/block' ||
getTemplateLock( state, current ) === 'contentOnly'
) {
result = current;
}
Expand Down Expand Up @@ -2869,8 +2864,9 @@ export function __unstableIsWithinBlockOverlay( state, clientId ) {
export const getBlockEditingMode = createRegistrySelector(
( select ) =>
( state, clientId = '' ) => {
if ( state.blockEditingModes.has( clientId ) ) {
return state.blockEditingModes.get( clientId );
const blockEditingMode = state.blockEditingModes.get( clientId );
if ( blockEditingMode ) {
return blockEditingMode;
}
if ( ! clientId ) {
return 'default';
Expand Down

1 comment on commit e3f4f76

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in e3f4f76.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7695795903
📝 Reported issues:

Please sign in to comment.