1.1.0 • Public • Published


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.


npm install --save skyhook


// Normal example
var hooks = require('skyhook')();
hooks.register('init', function () {
hooks.trigger('init').then(function () {
  // 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) {
      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) {


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.


PRs/Issues welcome!





Package Sidebar


npm i skyhook

Weekly Downloads






Last publish


  • ksmithut