@wordpress/hooks
Advanced tools
Comparing version 1.0.1 to 1.1.0
import validateNamespace from './validateNamespace.js'; | ||
import validateHookName from './validateHookName.js'; | ||
import { doAction } from './'; | ||
@@ -73,2 +74,6 @@ /** | ||
} | ||
if (hookName !== 'hookAdded') { | ||
doAction('hookAdded', hookName, namespace, callback, priority); | ||
} | ||
}; | ||
@@ -75,0 +80,0 @@ } |
import validateNamespace from './validateNamespace.js'; | ||
import validateHookName from './validateHookName.js'; | ||
import { doAction } from './'; | ||
@@ -71,2 +72,5 @@ /** | ||
} | ||
if (hookName !== 'hookRemoved') { | ||
doAction('hookRemoved', hookName, namespace); | ||
} | ||
@@ -73,0 +77,0 @@ return handlersRemoved; |
@@ -15,2 +15,4 @@ 'use strict'; | ||
var _ = require('./'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -87,2 +89,6 @@ | ||
} | ||
if (hookName !== 'hookAdded') { | ||
(0, _.doAction)('hookAdded', hookName, namespace, callback, priority); | ||
} | ||
}; | ||
@@ -89,0 +95,0 @@ } |
@@ -15,2 +15,4 @@ 'use strict'; | ||
var _ = require('./'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -85,2 +87,5 @@ | ||
} | ||
if (hookName !== 'hookRemoved') { | ||
(0, _.doAction)('hookRemoved', hookName, namespace); | ||
} | ||
@@ -87,0 +92,0 @@ return handlersRemoved; |
{ | ||
"name": "@wordpress/hooks", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "WordPress Hooks library", | ||
"author": "WordPress", | ||
"license": "GPL-2.0+", | ||
"keywords": [ | ||
"hooks" | ||
], | ||
"homepage": "https://github.com/WordPress/packages/tree/master/packages/hooks/README.md", | ||
"repository": { | ||
@@ -8,7 +15,7 @@ "type": "git", | ||
}, | ||
"description": "WordPress Hooks library", | ||
"bugs": { | ||
"url": "https://github.com/WordPress/packages/issues" | ||
}, | ||
"main": "build/index.js", | ||
"module": "build-module/index.js", | ||
"author": "WordPress", | ||
"license": "GPL-2.0+", | ||
"publishConfig": { | ||
@@ -15,0 +22,0 @@ "access": "public" |
@@ -25,3 +25,3 @@ # @wordpress/hooks | ||
* `removeAction( 'hookName', 'functionName' )` | ||
* `removeFilter( 'hookName', 'functionName' )` | ||
* `removeFilter( 'hookName', 'functionName' )` | ||
* `removeAllActions( 'hookName' )` | ||
@@ -45,2 +45,9 @@ * `removeAllFilters( 'hookName' )` | ||
API functions are then be called: `myObject.hooks.addAction()`... | ||
API functions are then be called: `myObject.hooks.addAction()`. | ||
### Events on action/filter add or remove | ||
Whenever an action or filter is added or removed, a matching `hookAdded` or `hookRemoved` action is triggered. | ||
* `hookAdded` action is triggered when `addFilter()` or `addAction()` method is called, passing values for `hookName`, `functionName`, `callback` and `priority`. | ||
* `hookRemoved` action is triggered when `removeFilter()` or `removeAction()` method is called, passing values for `hookName` and `functionName`. |
import validateNamespace from './validateNamespace.js'; | ||
import validateHookName from './validateHookName.js'; | ||
import { doAction } from './'; | ||
@@ -71,2 +72,6 @@ /** | ||
} | ||
if ( hookName !== 'hookAdded' ) { | ||
doAction( 'hookAdded', hookName, namespace, callback, priority ); | ||
} | ||
}; | ||
@@ -73,0 +78,0 @@ } |
import validateNamespace from './validateNamespace.js'; | ||
import validateHookName from './validateHookName.js'; | ||
import { doAction } from './'; | ||
@@ -68,2 +69,5 @@ /** | ||
} | ||
if ( hookName !== 'hookRemoved' ) { | ||
doAction( 'hookRemoved', hookName, namespace ); | ||
} | ||
@@ -70,0 +74,0 @@ return handlersRemoved; |
@@ -623,1 +623,64 @@ /* eslint-disable no-console */ | ||
} ); | ||
const setupActionListener = ( hookName, callback ) => | ||
addAction( hookName, 'my_callback', callback ); | ||
test( 'adding an action triggers a hookAdded action passing all callback details', () => { | ||
const hook_added_spy = jest.fn(); | ||
setupActionListener( 'hookAdded', hook_added_spy ); | ||
addAction( 'testAction', 'my_callback2', action_a, 9 ); | ||
expect( hook_added_spy ).toHaveBeenCalledTimes( 1 ); | ||
expect( hook_added_spy ).toHaveBeenCalledWith( | ||
'testAction', | ||
'my_callback2', | ||
action_a, | ||
9 | ||
); | ||
} ); | ||
test( 'adding a filter triggers a hookAdded action passing all callback details', () => { | ||
const hook_added_spy = jest.fn(); | ||
setupActionListener( 'hookAdded', hook_added_spy ); | ||
addFilter( 'testFilter', 'my_callback3', filter_a, 8 ); | ||
expect( hook_added_spy ).toHaveBeenCalledTimes( 1 ); | ||
expect( hook_added_spy ).toHaveBeenCalledWith( | ||
'testFilter', | ||
'my_callback3', | ||
filter_a, | ||
8 | ||
); | ||
} ); | ||
test( 'removing an action triggers a hookRemoved action passing all callback details', () => { | ||
const hook_removed_spy = jest.fn(); | ||
setupActionListener( 'hookRemoved', hook_removed_spy ); | ||
addAction( 'testAction', 'my_callback2', action_a, 9 ); | ||
removeAction( 'testAction', 'my_callback2' ); | ||
expect( hook_removed_spy ).toHaveBeenCalledTimes( 1 ); | ||
expect( hook_removed_spy ).toHaveBeenCalledWith( | ||
'testAction', | ||
'my_callback2' | ||
); | ||
} ); | ||
test( 'removing a filter triggers a hookRemoved action passing all callback details', () => { | ||
const hook_removed_spy = jest.fn(); | ||
setupActionListener( 'hookRemoved', hook_removed_spy ); | ||
addFilter( 'testFilter', 'my_callback3', filter_a, 8 ); | ||
removeFilter( 'testFilter', 'my_callback3' ); | ||
expect( hook_removed_spy ).toHaveBeenCalledTimes( 1 ); | ||
expect( hook_removed_spy ).toHaveBeenCalledWith( | ||
'testFilter', | ||
'my_callback3' | ||
); | ||
} ); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
0
0
52
69457
36
1855
1