Skip to content

Commit

Permalink
wp.hooks - enable the 'all' hook (#12038)
Browse files Browse the repository at this point in the history
* All action: Shim doAction and applyFilters to trigger the all listener for every hook.

* add all hook handling wrapped in comment blocks for production removal

* Add some unit tests for the ‘all’ hook

* Avoid hook recursion when adding all hook

* Conditionally add all hook support based on process.env.NODE_ENV

* use negative production check

* Add a changelog entry describing the all hook

* Document the `all` hook

* Update CHANGELOG.md
  • Loading branch information
Adam Silverstein authored and gziolo committed May 16, 2019
1 parent fb27643 commit f07e756
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/hooks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Master

### New Feature

- Enable support for the 'all' hook in non production environments.

## 2.0.4 (2019-01-03)

## 2.0.0 (2018-09-05)
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ Whenever an action or filter is added or removed, a matching `hookAdded` or `hoo
* `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`.

### The `all` hook

In non-minified builds developers can register a filter or action that will be called on *all* hooks, for example: `addAction( 'all', 'namespace', callbackFunction );`. Useful for debugging, the code supporting the `all` hook is stripped from the production code for performance reasons.

<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
8 changes: 8 additions & 0 deletions packages/hooks/src/createRunHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ function createRunHook( hooks, returnFirstArg ) {

const handlers = hooks[ hookName ].handlers;

// The following code is stripped from production builds.
if ( 'production' !== process.env.NODE_ENV ) {
// Handle any 'all' hooks registered.
if ( 'hookAdded' !== hookName && hooks.all ) {
handlers.push( ...hooks.all.handlers );
}
}

if ( ! handlers || ! handlers.length ) {
return returnFirstArg ?
args[ 0 ] :
Expand Down
45 changes: 45 additions & 0 deletions packages/hooks/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ beforeEach( () => {

delete hooks[ k ];
}
delete hooks.all;
} );
} );

Expand Down Expand Up @@ -697,3 +698,47 @@ test( 'removing a filter triggers a hookRemoved action passing all callback deta
'my_callback3'
);
} );

test( 'add an all filter and run it any hook to trigger it', () => {
addFilter( 'all', 'my_callback', filterA );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testa' );
expect( applyFilters( 'test.filter-anything', 'test' ) ).toBe( 'testa' );
} );

test( 'add an all action and run it any hook to trigger it', () => {
addAction( 'all', 'my_callback', actionA );
addAction( 'test.action', 'my_callback', actionA );// Doesn't get triggered.
doAction( 'test.action-anything' );
expect( window.actionValue ).toBe( 'a' );
} );

test( 'add multiple all filters and run it any hook to trigger them', () => {
addFilter( 'all', 'my_callback', filterA );
addFilter( 'all', 'my_callback', filterB );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' );
expect( applyFilters( 'test.filter-anything', 'test' ) ).toBe( 'testab' );
} );

test( 'add multiple all actions and run it any hook to trigger them', () => {
addAction( 'all', 'my_callback', actionA );
addAction( 'all', 'my_callback', actionB );
addAction( 'test.action', 'my_callback', actionA ); // Doesn't get triggered.
doAction( 'test.action-anything' );
expect( window.actionValue ).toBe( 'ab' );
} );

test( 'add multiple all filters and run it any hook to trigger them by priority', () => {
addFilter( 'all', 'my_callback', filterA, 11 );
addFilter( 'all', 'my_callback', filterB, 10 );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testba' );
expect( applyFilters( 'test.filter-anything', 'test' ) ).toBe( 'testba' );
} );

test( 'add multiple all actions and run it any hook to trigger them by priority', () => {
addAction( 'all', 'my_callback', actionA, 11 );
addAction( 'all', 'my_callback', actionB, 10 );
addAction( 'test.action', 'my_callback', actionA ); // Doesn't get triggered.
doAction( 'test.action-anything' );
expect( window.actionValue ).toBe( 'ba' );
} );

0 comments on commit f07e756

Please sign in to comment.