9.0.0 • Published 3 years ago

middleware-dispatcher v9.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

Middlewares Dispatcher

Build Status Test Coverage Code Climate

Tiny and simple middlewares class.

Install

npm install middleware-dispatcher

Usage

var Middlewares = require('middlewares-dispatcher');

var md = new Middlewares();

// add async middleware
md.use(function (ctx, next) {
  console.log('dispatched');
  next();
});

// add sync middleware
md.use(function (ctx) {
  console.log('dispatched one more time');
});

// dispatch middlewares
md.dispatch(this); // dispatch with `this` context
md.dispatch(42); // dispatch with 42 context

Extend constructors

var Module = function() {
  // do something
};
util.inherits(Module, Middlewares);
var module = new Module();

module.use(middlewares);
module.dispatch(module);

If you want to operate middlewares like array, you must create it manualy in the constructor:

var Module = function() {
  this.middlewares = [];
};
util.inherits(Module, Middlewares);
var module = new Module();

module.unshift(middleware);
module.splice(-1,0,middleware);
// etc
module.dispatch(module);

API

this.middlewares

Array of the used middlewares.

this.use([middleware|middlewares])

Method to add middleware or array of middlewares.

this.dispatch([ctx, callback])

Dispatch all used middlewares with given context and callback

Middlewares

// async mw
var asyncMw = function (ctx, next) {
  setTimeout(next, 0);
};

var syncMw = function (ctx) {
  // do sync stuff
};

License

(The MIT License)

Copyright (c) 2014 Anton Shuvalov anton@shuvalov.info

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.