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

Performance: Memoize multi selection selectors #3170

Merged
merged 1 commit into from
Oct 26, 2017
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
42 changes: 26 additions & 16 deletions editor/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,22 +553,25 @@ export function getSelectedBlock( state ) {
* @param {Object} state Global application state
* @return {Array} Multi-selected block unique UDs
*/
export function getMultiSelectedBlockUids( state ) {
const { blockOrder } = state.editor;
const { start, end } = state.blockSelection;
if ( start === end ) {
return [];
}
export const getMultiSelectedBlockUids = createSelector(
( state ) => {
const { blockOrder } = state.editor;
const { start, end } = state.blockSelection;
if ( start === end ) {
return [];
}

const startIndex = blockOrder.indexOf( start );
const endIndex = blockOrder.indexOf( end );
const startIndex = blockOrder.indexOf( start );
const endIndex = blockOrder.indexOf( end );

if ( startIndex > endIndex ) {
return blockOrder.slice( endIndex, startIndex + 1 );
}
if ( startIndex > endIndex ) {
return blockOrder.slice( endIndex, startIndex + 1 );
}

return blockOrder.slice( startIndex, endIndex + 1 );
}
return blockOrder.slice( startIndex, endIndex + 1 );
},
( state ) => [ state.editor.blockOrder, state.blockSelection ],
);

/**
* Returns the current multi-selection set of blocks, or an empty array if
Expand All @@ -577,9 +580,16 @@ export function getMultiSelectedBlockUids( state ) {
* @param {Object} state Global application state
* @return {Array} Multi-selected block objects
*/
export function getMultiSelectedBlocks( state ) {
return getMultiSelectedBlockUids( state ).map( ( uid ) => getBlock( state, uid ) );
}
export const getMultiSelectedBlocks = createSelector(
( state ) => getMultiSelectedBlockUids( state ).map( ( uid ) => getBlock( state, uid ) ),
( state ) => [
state.editor.blockOrder,
state.blockSelection,
state.editor.blocksByUid,
state.editor.edits.meta,
state.currentPost.meta,
]
);

/**
* Returns the unique ID of the first block in the multi-selection set, or null
Expand Down
3 changes: 3 additions & 0 deletions editor/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
getSelectedBlock,
getEditedPostContent,
getMultiSelectedBlockUids,
getMultiSelectedBlocks,
getMultiSelectedBlocksStartUid,
getMultiSelectedBlocksEndUid,
getBlockUids,
Expand Down Expand Up @@ -97,6 +98,8 @@ describe( 'selectors', () => {
getBlock.clear();
getBlocks.clear();
getEditedPostContent.clear();
getMultiSelectedBlockUids.clear();
getMultiSelectedBlocks.clear();
} );

afterAll( () => {
Expand Down