Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusable blocks: fix performance of __experimentalGetAllowedPatterns #64871

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2348,6 +2348,34 @@ export function __experimentalGetDirectInsertBlock(
return getDirectInsertBlock( state, rootClientId );
}

const parsedPatternCache = new WeakMap();

function parsePattern( pattern ) {
const blocks = parse( pattern.content, {
__unstableSkipMigrationLogs: true,
} );
if ( blocks.length === 1 ) {
blocks[ 0 ].attributes = {
...blocks[ 0 ].attributes,
metadata: {
...( blocks[ 0 ].attributes.metadata || {} ),
categories: pattern.categories,
patternName: pattern.name,
name: blocks[ 0 ].attributes.metadata?.name || pattern.title,
},
};
}
return {
...pattern,
blocks,
};
}

function getParsedPattern( pattern ) {
const parsedPattern = parsedPatternCache.has( pattern );
return parsedPattern ? parsedPattern : parsePattern( pattern );
}

export const __experimentalGetParsedPattern = createRegistrySelector(
ellatrix marked this conversation as resolved.
Show resolved Hide resolved
( select ) =>
createSelector(
Expand All @@ -2358,26 +2386,7 @@ export const __experimentalGetParsedPattern = createRegistrySelector(
if ( ! pattern ) {
return null;
}
const blocks = parse( pattern.content, {
__unstableSkipMigrationLogs: true,
} );
if ( blocks.length === 1 ) {
blocks[ 0 ].attributes = {
...blocks[ 0 ].attributes,
metadata: {
...( blocks[ 0 ].attributes.metadata || {} ),
categories: pattern.categories,
patternName: pattern.name,
name:
blocks[ 0 ].attributes.metadata?.name ||
pattern.title,
},
};
}
return {
...pattern,
blocks,
};
return getParsedPattern( pattern );
},
( state, patternName ) => [
unlock( select( STORE_NAME ) ).getPatternBySlug( patternName ),
Expand All @@ -2401,16 +2410,13 @@ const getAllowedPatternsDependants = ( select ) => ( state, rootClientId ) => [
export const __experimentalGetAllowedPatterns = createRegistrySelector(
( select ) => {
return createSelector( ( state, rootClientId = null ) => {
const {
getAllPatterns,
__experimentalGetParsedPattern: getParsedPattern,
} = unlock( select( STORE_NAME ) );
const { getAllPatterns } = unlock( select( STORE_NAME ) );
const patterns = getAllPatterns();
const { allowedBlockTypes } = getSettings( state );

const parsedPatterns = patterns
.filter( ( { inserter = true } ) => !! inserter )
.map( ( { name } ) => getParsedPattern( name ) );
.map( getParsedPattern );
const availableParsedPatterns = parsedPatterns.filter(
( { blocks } ) =>
checkAllowListRecursive( blocks, allowedBlockTypes )
Expand Down
Loading