skyhook

1.1.0 • Public • Published

skyhook

io.js compatibility node.js compatibility

NPM version Dependency Status Dev Dependency Status Code Climate Build Status Coverage Status

A Promise-based pub sub hub hook system for node.

Installation

npm install --save skyhook

Usage

// Normal example
var hooks = require('skyhook')();
 
hooks.register('init', function () {
  console.log('initializing');
});
 
hooks.trigger('init').then(function () {
  console.log('initialized');
  // console.logs the following:
  // initializing
  // initialized
});
 
// Real World example
var express = require('express');
 
hooks.register('middleware', function (app) {
  app.get('/user', function (req, res, next) {
    res.json({
      firstName: req.body.firstName,
      lastName: req.body.lastName
    });
  });
}, {once: true, weight: 1});
 
hooks.register('middleware', function (app) {
  app.use(express.static(__dirname + '/public'));
}, {once: true});
 
hooks.trigger('middleware', express()).then(function (app) {
  app.listen(8000);
});

Options

Constructor options

Below are the default options passed to the constructor.

var Skyhook = require('skyhook');
// You don't need the `new` keyword. It will create a new instance for you.
var hooks = new Skyhook({
  Promise: require('bluebird') // 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('init', function () {}, {
  weight: 0, // Orders the hook methods in order of weight, smallest number going first
  once: false // Will only call the hook method once, no matter how many times the hook is called
});

.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)

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.1.02latest

Version History

VersionDownloads (Last 7 Days)Published
1.1.02
1.0.10
1.0.00

Package Sidebar

Install

npm i skyhook

Weekly Downloads

0

Version

1.1.0

License

MIT

Last publish

Collaborators

  • ksmithut