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

wp.hooks - enable the 'all' hook #12038

Merged
merged 9 commits into from
May 16, 2019
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
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
Copy link
Member

Choose a reason for hiding this comment

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

I updated CHANGELOG myself. We are still not set on the proper recommendation here, but there is new emerging pattern to list new changes as coming from master. Probably something good to document. /cc @aduth


### 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' );
} );