Skip to content

Commit

Permalink
Memoize pattern objects returned from getAllowedPatterns (#66159)
Browse files Browse the repository at this point in the history
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
Co-authored-by: ndiego <ndiego@git.wordpress.org>
Co-authored-by: colorful-tones <colorful-tones@git.wordpress.org>
Co-authored-by: kevin940726 <kevin940726@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: madhusudhand <madhudollu@git.wordpress.org>
Co-authored-by: getdave <get_dave@git.wordpress.org>
Co-authored-by: annezazu <annezazu@git.wordpress.org>
  • Loading branch information
11 people authored Oct 16, 2024
1 parent 3fd7c8a commit 6d84740
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2385,6 +2385,21 @@ const getAllowedPatternsDependants = ( select ) => ( state, rootClientId ) => [
...getInsertBlockTypeDependants( state, rootClientId ),
];

const patternsWithParsedBlocks = new WeakMap();
function enhancePatternWithParsedBlocks( pattern ) {
let enhancedPattern = patternsWithParsedBlocks.get( pattern );
if ( ! enhancedPattern ) {
enhancedPattern = {
...pattern,
get blocks() {
return getParsedPattern( pattern ).blocks;
},
};
patternsWithParsedBlocks.set( pattern, enhancedPattern );
}
return enhancedPattern;
}

/**
* Returns the list of allowed patterns for inner blocks children.
*
Expand All @@ -2406,14 +2421,7 @@ export const __experimentalGetAllowedPatterns = createRegistrySelector(
const { allowedBlockTypes } = getSettings( state );
const parsedPatterns = patterns
.filter( ( { inserter = true } ) => !! inserter )
.map( ( pattern ) => {
return {
...pattern,
get blocks() {
return getParsedPattern( pattern ).blocks;
},
};
} );
.map( enhancePatternWithParsedBlocks );

const availableParsedPatterns = parsedPatterns.filter(
( pattern ) =>
Expand Down

0 comments on commit 6d84740

Please sign in to comment.