Skip to content

Commit

Permalink
Cache createBlock call in isUnmodifiedDefaultBlock (#12521)
Browse files Browse the repository at this point in the history
* Cache createBlock call in isUnmodifiedDefaultBlock

* Invalidate cache when default block name changes

* Merge ifs
  • Loading branch information
ellatrix authored and youknowriad committed Dec 8, 2018
1 parent 715af6b commit 8e772be
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
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 );
} );
} );
} );
11 changes: 10 additions & 1 deletion packages/blocks/src/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ export function isUnmodifiedDefaultBlock( block ) {
return false;
}

const newDefaultBlock = createBlock( defaultBlockName );
// Cache a created default block if no cache exists or the default block
// name changed.
if (
! isUnmodifiedDefaultBlock.block ||
isUnmodifiedDefaultBlock.block.name !== defaultBlockName
) {
isUnmodifiedDefaultBlock.block = createBlock( defaultBlockName );
}

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

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

0 comments on commit 8e772be

Please sign in to comment.