Skip to content
This repository has been archived by the owner on Jul 9, 2018. It is now read-only.

TRY: Support an optional namespace parameter for hasAction/hasFilter #106

Closed
wants to merge 7 commits into from

Conversation

adamsilverstein
Copy link
Member

  • Add tests
  • Update docblock

Fixes #104

@adamsilverstein adamsilverstein requested a review from aduth April 10, 2018 20:43
@codecov
Copy link

codecov bot commented Apr 10, 2018

Codecov Report

Merging #106 into master will increase coverage by 0.65%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #106      +/-   ##
==========================================
+ Coverage   64.07%   64.72%   +0.65%     
==========================================
  Files          42       42              
  Lines         604      618      +14     
  Branches      118      121       +3     
==========================================
+ Hits          387      400      +13     
- Misses        175      176       +1     
  Partials       42       42
Impacted Files Coverage Δ
packages/hooks/src/createHasHook.js 100% <100%> (ø) ⬆️
packages/i18n/src/index.js 93.54% <0%> (-1.46%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 21f3cea...e928084. Read the comment docs.

*
* @param {string} hookName The name of the hook to check for.
* @param {string} hookName The name of the hook to check for.
* @param {string} namespace Optional. The unique namespace identifying the callback in the form `vendor/plugin/function`.
Copy link
Member

Choose a reason for hiding this comment

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

Single spaces between @param and {string} and name

	 * @param {string} hookName  The name of the hook to check for.
	 * @param {string} namespace Optional. The unique na

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks, corrected.

if ( 'undefined' !== typeof namespace ){
return hookName in hooks &&
hooks[ hookName ].handlers &&
hooks[ hookName ].handlers.map( hook => hook['namespace'] ).includes( namespace );
Copy link
Member

Choose a reason for hiding this comment

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

Array#includes is not polyfilled, and is not supported in older browsers:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Browser_compatibility

Further, there's unnecessary work here to iterate the entire array of handlers, when Array#some serves this purpose to terminate iteration once the first match is encountered:

hooks[ hookName ].handlers.some( hook => hook['namespace'] === namespace );

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, right - thanks for catching that. Will fix.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated

// Use the namespace if provided.
if ( 'undefined' !== typeof namespace ){
return hookName in hooks &&
hooks[ hookName ].handlers &&
Copy link
Member

Choose a reason for hiding this comment

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

When will this not be truthy?

Copy link
Member Author

Choose a reason for hiding this comment

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

Possibly if no handlers have been added? generally prefer the defensive approach of always checking before using because otherwise everything breaks. even if handlers always set up currently, that might change in the future.

Copy link
Member

Choose a reason for hiding this comment

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

generally prefer the defensive approach of always checking before using because otherwise everything breaks.

Well, there's defensive, but there's also having a solid understanding of the shapes of data we expect to occur.

Copy link
Member Author

Choose a reason for hiding this comment

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

True :) I'll take another look - if handlers is always going to be truthy, i will remove the check.

Copy link
Member Author

Choose a reason for hiding this comment

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

removed extraneous check

@adamsilverstein adamsilverstein force-pushed the feature/second-parameter-for-has-hook branch from e1233d1 to 8d8881b Compare April 24, 2018 12:11
@adamsilverstein adamsilverstein changed the title Support an optional namespace parameter for hasAction/hasFilter TRY: Support an optional namespace parameter for hasAction/hasFilter Jun 25, 2018
@adamsilverstein
Copy link
Member Author

@aduth any concerns for merging this, seems useful?

Copy link

@kadamwhite kadamwhite left a comment

Choose a reason for hiding this comment

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

The implementation looks straightforward, I’d just question whether we’re pushing for this just to match core and fill a perceived need or whether we’ll actually find this useful in practice.

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

This looks good to me.

I think improved parity with equivalent core functions is enough justification to want this to land.

Unless it's urgent, I might suggest waiting 'til after WordPress/gutenberg#7556 to avoid complications with including the new history there.

// Use the namespace if provided.
if ( 'undefined' !== typeof namespace ){
return hookName in hooks &&
hooks[ hookName ].handlers.some( hook => hook.namespace === namespace );
Copy link
Member

Choose a reason for hiding this comment

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

return function hasHook( hookName ) {
return function hasHook( hookName, namespace ) {
// Use the namespace if provided.
if ( 'undefined' !== typeof namespace ){
Copy link
Member

Choose a reason for hiding this comment

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

Style: Space before opening curly brace.

return hookName in hooks &&
hooks[ hookName ].handlers.some( hook => hook.namespace === namespace );
}

return hookName in hooks;
Copy link
Member

Choose a reason for hiding this comment

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

Minor: Regardless of whether namespace is provided, we have this same condition apply, so rather than duplicate, we could move it as a primary check of the function:

if ( ! ( hookName in hooks ) ) {
	return false;
}

if ( 'undefined' === typeof namespace ) {
	return true;
}

return hooks[ hookName ].handlers.some( ( hook ) => {
	return hook.namespace === namespace;
} );

@adamsilverstein
Copy link
Member Author

@aduth thanks for the feedback, I'll work on these soon

@ntwb
Copy link
Member

ntwb commented Jul 9, 2018

Issue moved to WordPress/gutenberg #7819 via ZenHub

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants