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

Hooks: export the defaultHooks singleton instance #28725

Merged
merged 2 commits into from
Feb 4, 2021
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
4 changes: 4 additions & 0 deletions packages/hooks/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New Feature

- Export the default `createHooks` singleton instance as `defaultHooks`, in addition to exporting the individual methods.

## 2.11.0 (2020-12-17)

### New Feature
Expand Down
9 changes: 8 additions & 1 deletion packages/hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ myObject.hooks = createHooks();
myObject.hooks.addAction(); //etc...
```

In the WordPress context, API functions can be called via the global `wp.hooks` like this `wp.hooks.addAction()`, etc. One notable difference from the PHP API is that `addAction()` and `addFilter()` also need to include a namespace as the second argument.
#### The global instance

In the above example, we are creating a custom instance of the `Hooks` object and registering hooks there. The package also creates a default global instance that's accessible through the `defaultHooks` named exports, and its methods are also separately exported one-by-one.

In the WordPress context, that enables API functions to be called via the global `wp.hooks` object, like `wp.hooks.addAction()`, etc.

One notable difference between the JS and PHP hooks API is that in the JS version, `addAction()` and `addFilter()` also need to include a namespace as the second argument.

### API Usage

Expand All @@ -43,6 +49,7 @@ In the WordPress context, API functions can be called via the global `wp.hooks`
* `hasFilter( 'hookName', 'namespace' )`
* `actions`
* `filters`
* `defaultHooks`


### Events on action/filter add or remove
Expand Down
4 changes: 3 additions & 1 deletion packages/hooks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import createHooks from './createHooks';
* @typedef {import('./createHooks').Hooks} Hooks
*/

export const defaultHooks = createHooks();

const {
addAction,
addFilter,
Expand All @@ -55,7 +57,7 @@ const {
didFilter,
actions,
filters,
} = createHooks();
} = defaultHooks;

export {
createHooks,
Expand Down