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

Exclude reusable blocks from the global block count #11787

Merged
merged 1 commit into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
map,
orderBy,
reduce,
size,
some,
} from 'lodash';
import createSelector from 'rememo';
Expand Down Expand Up @@ -728,16 +727,17 @@ export const getClientIdsWithDescendants = createSelector(
*/
export const getGlobalBlockCount = createSelector(
( state, blockName ) => {
const clientIds = getClientIdsWithDescendants( state );
if ( ! blockName ) {
return size( state.editor.present.blocks.byClientId );
return clientIds.length;
}
return reduce(
state.editor.present.blocks.byClientId,
( count, block ) => block.name === blockName ? count + 1 : count,
0
);
return reduce( clientIds, ( count, clientId ) => {
const block = state.editor.present.blocks.byClientId[ clientId ];
return block.name === blockName ? count + 1 : count;
}, 0 );
},
( state ) => [
state.editor.present.blocks.order,
state.editor.present.blocks.byClientId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really worth memoizing selectors if they depend on these two elements particularly? I feel state.editor.present.blocks.byClientId changes so often that it's just a waste of time to memoize these selectors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on this @aduth?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it seems wasteful to have this selector run when something unrelated e.g. a post attribute is modified, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know honestly, it feels like typing in blocks is the most impactful thing in terms of performance, changing an attribute only get triggered once and not extensively.

Anyway, just something to keep in mind for now, there's no clear "winner"

]
);
Expand Down
50 changes: 20 additions & 30 deletions packages/editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2511,54 +2511,44 @@ describe( 'selectors', () => {
} );

describe( 'getGlobalBlockCount', () => {
it( 'should return the global number of top-level blocks in the post', () => {
const state = {
editor: {
present: {
blocks: {
byClientId: {
23: { clientId: 23, name: 'core/heading', attributes: {} },
123: { clientId: 123, name: 'core/paragraph', attributes: {} },
},
const state = {
editor: {
present: {
blocks: {
byClientId: {
123: { clientId: 123, name: 'core/heading', attributes: {} },
456: { clientId: 456, name: 'core/paragraph', attributes: {} },
789: { clientId: 789, name: 'core/paragraph', attributes: {} },
},
order: {
'': [ 123, 456 ],
},
},
},
};
},
};

it( 'should return the global number of blocks in the post', () => {
expect( getGlobalBlockCount( state ) ).toBe( 2 );
} );

it( 'should return the global umber of blocks of a given type', () => {
const state = {
editor: {
present: {
blocks: {
byClientId: {
123: { clientId: 123, name: 'core/columns', attributes: {} },
456: { clientId: 456, name: 'core/paragraph', attributes: {} },
789: { clientId: 789, name: 'core/paragraph', attributes: {} },
124: { clientId: 123, name: 'core/heading', attributes: {} },
},
},
},
},
};

expect( getGlobalBlockCount( state, 'core/heading' ) ).toBe( 1 );
it( 'should return the global number of blocks in the post of a given type', () => {
expect( getGlobalBlockCount( state, 'core/paragraph' ) ).toBe( 1 );
} );

it( 'should return 0 if no blocks exist', () => {
const state = {
const emptyState = {
editor: {
present: {
blocks: {
byClientId: {},
order: {},
},
},
},
};
expect( getGlobalBlockCount( state ) ).toBe( 0 );
expect( getGlobalBlockCount( state, 'core/heading' ) ).toBe( 0 );
expect( getGlobalBlockCount( emptyState ) ).toBe( 0 );
expect( getGlobalBlockCount( emptyState, 'core/heading' ) ).toBe( 0 );
} );
} );

Expand Down