A middleware control flow dispatcher similar to the middleware implementation of routes within expressjs.
npm install dispatcher-middleware
var Dispatcher = require('dispatcher-middleware').Dispatcher;
var dispatcher = new Dispatcher({
myAction: [middlewareFn1, middlewareFn2, finalFn],
myAsyncAction: [[asyncMiddlewareFn1, asyncMiddlewareFn2], finalFn]
});
Middleware functions are passed a data
object hash and a next
function. Middleware functions pass data by appending the
data
object.
Middleware functions take the format:
function (data, next) {
data.x = 3;
next();
}
And the final function call takes the format:
function (data) {
console.log(data.x); // 3
}
Any function can short circuit the middleware pipe by not calling next()
.
Middleware functions are synchronous unless nested within an array. (Other than the leading array).
Nested arrays of middleware will run asynchronously.
function asyncMw1(data, next) {
window.setTimeout(function () {
data.x = 4;
next();
}, 100);
}
function asyncMw2(data, next) {
console.log(data.x); // undefined
data.y = 3;
next();
}
function final(data) {
console.log(data.x + data.y); // 7
}
var Dispatcher = require('dispatcher-middleware').Dispatcher;
var dispatcher = new Dispatcher({
myAction: [[asyncMw1, asyncMw2], final]
});
var Pipe = require('dispatcher-middleware').Pipe;
new Pipe.execute([middlewareFn1, middlewareFn2, final]);
npm test
npm run-script coverage