Skip to content

Commit

Permalink
Hooks: export the defaultHooks singleton instance (#28725)
Browse files Browse the repository at this point in the history
* Hooks: export the defaultHooks singleton instance

* Document defaultHooks and the global singleton instance in README
  • Loading branch information
jsnajdr authored Feb 4, 2021
1 parent 9033199 commit 6ac30fb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
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

0 comments on commit 6ac30fb

Please sign in to comment.