JSFiddle - React, Tailwind, and code Playground

by blesh

JavaScript

function Middleware() {
    var wares = [],
        context;

    this.use = function (fn) {
        var nextIndex = wares.length + 1;
        wares.push(function () {
            fn(context, (wares[nextIndex] || noop));
        });
    };

    this.run = function (x) {
        context = x || {}
        if (wares.length > 0) {
            wares[0](context);
        }
    };
}

function noop() {}

var mid = new Middleware();

mid.use(function (x, next) {
    x.foo = 'bar';
    next();
});
mid.use(function (x, next) {
    x.foo += '!!!';
    next();
});

var ctxt = {};
mid.run(ctxt);
console.log(ctxt);