Skip to content

Commit

Permalink
Merge pull request #2 from snapwich/hooks-refactor
Browse files Browse the repository at this point in the history
hookable useable before being created now
  • Loading branch information
snapwich authored Feb 21, 2019
2 parents 91c7ee9 + d669e10 commit b4f3d9c
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 141 deletions.
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

0 comments on commit b4f3d9c

Please sign in to comment.