Skip to content

Commit

Permalink
Try to optimize getClientIdsOfDescendants using memoization. (#40112)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZebulanStanphill committed Apr 7, 2022
1 parent ba8b6ef commit 10032aa
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,21 @@ export const __unstableGetClientIdsTree = createSelector(
*
* @return {Array} ids of descendants.
*/
export const getClientIdsOfDescendants = ( state, clientIds ) => {
const collectedIds = [];
for ( const givenId of clientIds ) {
for ( const descendantId of getBlockOrder( state, givenId ) ) {
collectedIds.push(
descendantId,
...getClientIdsOfDescendants( state, [ descendantId ] )
);
export const getClientIdsOfDescendants = createSelector(
( state, clientIds ) => {
const collectedIds = [];
for ( const givenId of clientIds ) {
for ( const descendantId of getBlockOrder( state, givenId ) ) {
collectedIds.push(
descendantId,
...getClientIdsOfDescendants( state, [ descendantId ] )
);
}
}
}
return collectedIds;
};
return collectedIds;
},
( state ) => [ state.blocks.order ]
);

/**
* Returns an array containing the clientIds of the top-level blocks and
Expand Down

0 comments on commit 10032aa

Please sign in to comment.