-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
getBlockParentsByBlockName: simplify filtering condition #51439
Conversation
Size Change: -5 B (0%) Total Size: 1.4 MB
ℹ️ View Unchanged
|
Flaky tests detected in c18e830. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5252227794
|
const hasName = Array.isArray( blockName ) | ||
? ( name ) => blockName.includes( name ) | ||
: ( name ) => blockName === name; | ||
return parents.filter( ( id ) => hasName( getBlockName( state, id ) ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need to redeclare the function on every run? Also, I find the polymorphic behavior of hasName
a bit confusing - on one run it will accept an array only, and on another - a string only, while it has the same name. I think this gets behavior exposed better if we have a helper like this:
function hasBlockName( blockNames, name ) {
if ( Array.isArray( blockNames ) ) {
return blockNames.includes( name );
}
return blockNames === name;
}
and then use that inside getBlockParentsByBlockName()
:
export const getBlockParentsByBlockName = createSelector(
( state, clientId, blockName, ascending = false ) => {
const parents = getBlockParents( state, clientId, ascending );
return parents.filter( ( id ) =>
hasBlockName( blockName, getBlockName( state, id ) )
);
},
( state ) => [ state.blocks.parents ]
);
That way we also won't need to redeclare it on every run. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intent was to avoid calling Array.isArray
on every iteration of the .filter
function. blockName
is constant through that loop, so why not do the array check only once at the beginning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Well, performance-wise, the difference seems negligible, so to me, it was more about readability, but I have no strong feelings either way 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's more an aesthetic concern rather than real perfomance, as the lists of blockNames
and parents
are always short. If you have a suggestion that makes the code more readable, and still keeps the single isArray
check, I would love to see it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can think of a few alternative ways, but they still sacrifice readability a bit.
With that in mind, I think your approach is just fine 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍 🚀 Thanks!
While debugging I noticed that a filtering condition in
getBlockParentsByBlockName
is unnecessarily complex. This PR is replacing the.map.filter.map
chain with one.filter
call.