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

Cache createBlock call in isUnmodifiedDefaultBlock #12521

Merged
merged 3 commits into from
Dec 8, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
29 changes: 29 additions & 0 deletions packages/blocks/src/api/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,34 @@ describe( 'block helpers', () => {

expect( isUnmodifiedDefaultBlock( block ) ).toBe( false );
} );

it( 'should invalidate cache if the default block name changed', () => {
registerBlockType( 'core/test-block1', {
attributes: {
includesDefault1: {
type: 'boolean',
default: true,
},
},
save: noop,
category: 'common',
title: 'test block',
} );
registerBlockType( 'core/test-block2', {
attributes: {
includesDefault2: {
type: 'boolean',
default: true,
},
},
save: noop,
category: 'common',
title: 'test block',
} );
setDefaultBlockName( 'core/test-block1' );
isUnmodifiedDefaultBlock( createBlock( 'core/test-block1' ) );
setDefaultBlockName( 'core/test-block2' );
expect( isUnmodifiedDefaultBlock( createBlock( 'core/test-block2' ) ) ).toBe( true );
} );
} );
} );
15 changes: 14 additions & 1 deletion packages/blocks/src/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,20 @@ export function isUnmodifiedDefaultBlock( block ) {
return false;
}

const newDefaultBlock = createBlock( defaultBlockName );
// Invalidate default block cache if default block name changed.
if (
isUnmodifiedDefaultBlock.block &&
isUnmodifiedDefaultBlock.block.name !== defaultBlockName
ellatrix marked this conversation as resolved.
Show resolved Hide resolved
) {
delete isUnmodifiedDefaultBlock.block;
}

// Cache a created default block.
if ( ! isUnmodifiedDefaultBlock.block ) {
isUnmodifiedDefaultBlock.block = createBlock( defaultBlockName );
}
ellatrix marked this conversation as resolved.
Show resolved Hide resolved

const newDefaultBlock = isUnmodifiedDefaultBlock.block;
const blockType = getBlockType( defaultBlockName );

return every( blockType.attributes, ( value, key ) =>
Expand Down