koajs / compose

Middleware composition utility

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

0 and 1 length array cases

jonathanong opened this issue · comments

want to handle them specially? throw if length 0, return the first element if length 1.

hmm we could, not sure it would ever be a real case though, often at least

You can mod the array passed to compose after compose has created its wrapper. This might be a good way to create injection points in a koa stack. Doing different things when length is zero or one would hurt that functionality

var compose = require("koa-compose");
var koa = require("koa");

var app = koa();

var BASE_UNTIL = 1200;
var until = BASE_UNTIL;

var middleware = [];

app.use(function*(next){
    this.body = "Time till next add "+ (until/1000) + "sec(s)";
    this.body += "\n\tMiddleware Length " + middleware.length;
    yield next;
});

app.use(compose(middleware));

app.listen(3000);

setInterval(function(){

    until = until - 300;
    if(until<=0){
        until = BASE_UNTIL; 
        middleware.push(function*(next){
            this.body += "\n\t\tadded one";
            yield next;
        });
    }

}, 300);

.... why would you do that? lol

I can make up some crazy reason if you want? :) It will probs have to do with a router. Seems to my my go to with compose.

My real point is that you could view this as functionality and while its a bit weird people can implement this check themselves and will need to if its going to toss an error so... why not keep the functionality?