skyhook
A Promise-based pub sub hub hook system for node.
Installation
npm install --save skyhook
Usage
// Normal examplevar hooks = ; hooks; hooks; // Real World examplevar express = ; hooks; hooks; hooks;
Options
Constructor options
Below are the default options passed to the constructor.
var Skyhook = ;// You don't need the `new` keyword. It will create a new instance for you.var hooks = Promise: // You can override the Promise implementation.;
.register()
options
The first parameter is the name of the hook you want to register to. The second
is the method you want called. If you want to bind a specific context, you'll
need to bind it manually with .bind()
. It's only argument is the object that
the hook was triggered with. If the argument is an object, any changes you make
to the properties of the object will persist to the next call. If you get a
string, a number, or other value that is passed by reference, then you must
return the modified value if you wish your changes to be persisted.
This is chainable with multiple registers.
hooks;
.trigger()
options
The first parameter is the name of the hook you wish to call. The second is options, but if you wish to expose any config object you wish to be exposed to other hooks, this is where you would put it. Note that it only accepts 1 parameter. This is to simplify your hooks. If you wish to expose more than one parameter to be changed, just create another hook.
The third parameter is an options object. It's only option is returnChange
whose default value is true
. When set to false
, it will not accept values
returned from a hook to modify the value. Note that if a parameter is passed
by reference (such as an array or an object), then a hook can still modify
properties on the passed object.
This method returns a promise with resolves to the modified (or un-modified) value that you passed (or didn't pass).
.triggerMultiple()
options
triggerMultiple
is the same as .trigger()
, but the first argument accepts
an array of hooks you want to call. The second parameter works like before, and
it gets passed through all of the arguments passed.
Questions/Comments
PRs/Issues welcome!
License
MIT (see LICENCE)