stak
Version:
59 lines (50 loc) • 1.55 kB
JavaScript
var slice = [].slice.call.bind([].slice);
var Stak = {
constructor: function constructor() {
this._stack = [];
this.use.apply(this, arguments);
return this;
},
use: function use() {
this._stack.push.apply(this._stack, arguments);
},
handle: function handle(hash) {
var context = hash || {},
floor = context.floor || this.floor,
ceil = context.ceil || this.ceil,
stack = slice(this._stack);
ceil && stack.unshift(ceil);
floor && stack.push(floor);
context.next = loop;
loop();
function loop() {
var handler = stack.shift();
if (handler && handler.handle) {
handler.handle(Object.create(context, {
floor: { value: loop }
}));
} else if (typeof handler === "function") {
handler.apply(context, arguments);
}
}
},
handler: function handler() {
return proxy.bind(this);
},
connect: function connect(f) {
return proxy;
function proxy() {
f(Object.getPrototypeOf(this), this.res, this.next);
}
},
make: function make() {
var o = Object.create(Stak);
return o.constructor.apply(o, arguments);
}
};
module.exports = Stak;
function proxy(req, res) {
this.handle(Object.create(req, {
res: { value: res }
}));
}