From 9f8f805a6f8460a2ac019675516e854f99ac458e Mon Sep 17 00:00:00 2001 From: iseulde Date: Sun, 2 Dec 2018 14:10:26 +0100 Subject: [PATCH 1/3] Cache createBlock call in isUnmodifiedDefaultBlock --- packages/blocks/src/api/utils.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/blocks/src/api/utils.js b/packages/blocks/src/api/utils.js index 392593b440096..0e47ecd850c6f 100644 --- a/packages/blocks/src/api/utils.js +++ b/packages/blocks/src/api/utils.js @@ -38,7 +38,12 @@ export function isUnmodifiedDefaultBlock( block ) { return false; } - const newDefaultBlock = createBlock( defaultBlockName ); + // Cache a newly created default block. + if ( ! isUnmodifiedDefaultBlock.block ) { + isUnmodifiedDefaultBlock.block = createBlock( defaultBlockName ); + } + + const newDefaultBlock = isUnmodifiedDefaultBlock.block; const blockType = getBlockType( defaultBlockName ); return every( blockType.attributes, ( value, key ) => From 93c735357cb589094203ceb093461617d83515c4 Mon Sep 17 00:00:00 2001 From: iseulde Date: Mon, 3 Dec 2018 11:07:17 +0100 Subject: [PATCH 2/3] Invalidate cache when default block name changes --- packages/blocks/src/api/test/utils.js | 29 +++++++++++++++++++++++++++ packages/blocks/src/api/utils.js | 10 ++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/blocks/src/api/test/utils.js b/packages/blocks/src/api/test/utils.js index 0bfeb8b0fe258..ea3fe57908441 100644 --- a/packages/blocks/src/api/test/utils.js +++ b/packages/blocks/src/api/test/utils.js @@ -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 ); + } ); } ); } ); diff --git a/packages/blocks/src/api/utils.js b/packages/blocks/src/api/utils.js index 0e47ecd850c6f..6b9d0241feadd 100644 --- a/packages/blocks/src/api/utils.js +++ b/packages/blocks/src/api/utils.js @@ -38,7 +38,15 @@ export function isUnmodifiedDefaultBlock( block ) { return false; } - // Cache a newly created default block. + // Invalidate default block cache if default block name changed. + if ( + isUnmodifiedDefaultBlock.block && + isUnmodifiedDefaultBlock.block.name !== defaultBlockName + ) { + delete isUnmodifiedDefaultBlock.block; + } + + // Cache a created default block. if ( ! isUnmodifiedDefaultBlock.block ) { isUnmodifiedDefaultBlock.block = createBlock( defaultBlockName ); } From 18823a2d2f6eca7580b99d6191367bd96d8fc2b0 Mon Sep 17 00:00:00 2001 From: iseulde Date: Mon, 3 Dec 2018 11:19:17 +0100 Subject: [PATCH 3/3] Merge ifs --- packages/blocks/src/api/utils.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/blocks/src/api/utils.js b/packages/blocks/src/api/utils.js index 6b9d0241feadd..6dcabbcbcea01 100644 --- a/packages/blocks/src/api/utils.js +++ b/packages/blocks/src/api/utils.js @@ -38,16 +38,12 @@ export function isUnmodifiedDefaultBlock( block ) { return false; } - // Invalidate default block cache if default block name changed. + // Cache a created default block if no cache exists or the default block + // name changed. if ( - isUnmodifiedDefaultBlock.block && + ! isUnmodifiedDefaultBlock.block || isUnmodifiedDefaultBlock.block.name !== defaultBlockName ) { - delete isUnmodifiedDefaultBlock.block; - } - - // Cache a created default block. - if ( ! isUnmodifiedDefaultBlock.block ) { isUnmodifiedDefaultBlock.block = createBlock( defaultBlockName ); }