Skip to content

Commit

Permalink
Block Directory: Memoize store selectors (#63346)
Browse files Browse the repository at this point in the history
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: ellatrix <[email protected]>
  • Loading branch information
3 people authored Jul 10, 2024
1 parent bdfd1d7 commit b4711e7
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions packages/block-directory/src/store/selectors.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { createRegistrySelector } from '@wordpress/data';
import { createSelector, createRegistrySelector } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
Expand Down Expand Up @@ -53,15 +53,21 @@ export function getInstalledBlockTypes( state ) {
*
* @return {Array} Block type items.
*/
export const getNewBlockTypes = createRegistrySelector(
( select ) => ( state ) => {
const usedBlockTree = select( blockEditorStore ).getBlocks();
const installedBlockTypes = getInstalledBlockTypes( state );
export const getNewBlockTypes = createRegistrySelector( ( select ) =>
createSelector(
( state ) => {
const usedBlockTree = select( blockEditorStore ).getBlocks();
const installedBlockTypes = getInstalledBlockTypes( state );

return installedBlockTypes.filter( ( blockType ) =>
hasBlockType( blockType, usedBlockTree )
);
}
return installedBlockTypes.filter( ( blockType ) =>
hasBlockType( blockType, usedBlockTree )
);
},
( state ) => [
getInstalledBlockTypes( state ),
select( blockEditorStore ).getBlocks(),
]
)
);

/**
Expand All @@ -72,15 +78,21 @@ export const getNewBlockTypes = createRegistrySelector(
*
* @return {Array} Block type items.
*/
export const getUnusedBlockTypes = createRegistrySelector(
( select ) => ( state ) => {
const usedBlockTree = select( blockEditorStore ).getBlocks();
const installedBlockTypes = getInstalledBlockTypes( state );
export const getUnusedBlockTypes = createRegistrySelector( ( select ) =>
createSelector(
( state ) => {
const usedBlockTree = select( blockEditorStore ).getBlocks();
const installedBlockTypes = getInstalledBlockTypes( state );

return installedBlockTypes.filter(
( blockType ) => ! hasBlockType( blockType, usedBlockTree )
);
}
return installedBlockTypes.filter(
( blockType ) => ! hasBlockType( blockType, usedBlockTree )
);
},
( state ) => [
getInstalledBlockTypes( state ),
select( blockEditorStore ).getBlocks(),
]
)
);

/**
Expand Down

0 comments on commit b4711e7

Please sign in to comment.