middleware-object-hooks

Object/method based middleware/reduce hooks

middleware-object-hooks

Middleware(ish) hooks based on "methods" in objects.

(Also see examples/)

var middleware = require('middleware-object-hooks');
 
middlware.use({
  startfunction(value) {
    value.calls = value.calls || 0;
    return value;
  }
});
 
middlware.use({
  startfunction(value) {
    value.calls++;
  }  
});
 
middlware.use({
  startfunction() {
    return new Promise(function(acceptreject) {
      // do some magic then accept / reject 
    });
  }
});
 
middlware.run(
  'start', // method in the middleware 
  {
    // passed to the method in the middleware   
  }
).then(
  function(value) {
  },
 
  function(err) {
  }
);
  • Middleware methods are invoked within the context of their object

  • Multiple values may be passed to run (or none at all)

  • Each result is passed directly to the next middleware so its possible to both mutate the value (if its an object) and entirely replace it.