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

hookable useable before being created now #2

Merged
merged 2 commits into from
Feb 21, 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
5 changes: 1 addition & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ module.exports = {
},
rules: {
"prettier/prettier": "error",
"linebreak-style": [
"error",
"unix"
]
"linebreak-style": ["error", "unix"]
}
};
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ hookedIncrement.before(function sideEffect(next, ...args) {
```

### Naming
Hooks can be given a name and then they will be exported using the `.hooks` property. This can be useful for defining the
Hooks can be given a name and then accessed using the `.get` method. This can be useful for defining the
extensible API for your application. _Note: You can also just expose references to the hooked functions themselves,
this is just a convenience to group all those function references together._
this is just a convenience. Also, when using named hooks, you can reference the hook by name using `.get` and add
`before` and `after` hooks before the hook itself has actually been created!_

```javascript
// some-applicaiton
Expand All @@ -190,20 +191,26 @@ function getPrice(item) {
return item.price;
}

// works, even though the "item" hook isn't defined until below!
hook.get("item").after(function(next, id) {
console.log("accessing item: " + id);
next(id);
});

hook("async", getItem, "item"); // naming this hook `item`
hook("sync", getPrice, "price"); // naming this hook `price`

export const hooks = hook.hooks;
export const getHook = hook.get;

// extending application
import { hooks } from "some-application";
import { getHook } from "some-application";

hooks.item.before(function modifyId(next, id) {
getHook("item").before(function modifyId(next, id) {
let newId = getUpdatedId(id); // `id` naming scheme changed... luckily we have this hook available!
next(newId);
});

hooks.price.after(function currencyConversion(next, price) {
getHook("price").after(function currencyConversion(next, price) {
let newPrice = convert(price, "USD");
next(newPrice);
});
Expand Down Expand Up @@ -251,13 +258,16 @@ Hooked methods are all assumed to be `sync` unless otherwise specified.
hook(Thing.prototype, ["setValue", "sync:getValue" /* same as "getValue" */, "async:loadData"]);
```

If a third argument, `name`, is provided, then the object's hooked methods will be added to the `.hooks` property
described above in [Naming](#naming).
If a third argument, `name`, is provided, then the object's hooked methods will be made accessible to the `.get`
method described above using `<Object Name>.<Method Name>` in [Naming](#naming).

```javascript
hook(Thing.prototype, ["setValue", 'getValue"], "thing");

hook.hooks; // {thing: {setValue, getValue}}
// grab the collection of hooks
hook.get("thing"); // {thing: {setValue, getValue}}
// or grab an individual hook
hook.get("thing.setValue");
```

### Ready
Expand Down
Loading