koajs / compose

Middleware composition utility

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

add compose.hook or similar

tj opened this issue · comments

some API for extending externally, for example the new debug stuff I just added could live in a different module. I'd like to provide a nicer alternative that outputs HTML documents since the terminal quickly becomes a clusterfuck, especially with parallel requests

One thing I'm not sure about is what to call "events". The current debug code is using up and down but I'm not sure that is 100% clear to the end user. Client side talks about this as capturing and bubbling, but that doesn't seem right either. Is start and stop clear?

using on

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

compose.on("down", function(ctx, name){ console.log(name, "has started"); });
compose.on("up", function(ctx, name){ console.log(name, "has finished"); });

using hook

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

compose.hook(function(ctx, name, direction){
  if(direction=="down"){
    console.log(name, "has started");
  }
  else{
    console.log(name, "has finished");
  }
});

we could implement it as more middleware that get injected inbetween, so you'd just do a normal yield. it feels super dirty to do this at the compose level though, I'd almost rather remove this library and have it be a thing in Koa. There's no guarantee that people will use compose() to compose middleware so unless we make it a formal thing it seems awkward

I see the value of adding this to koa but how would koa inject debugging
into something like compose?

On Wed, Mar 12, 2014 at 9:59 PM, TJ Holowaychuk notifications@github.comwrote:

we could implement it as more middleware that get injected inbetween, so
you'd just do a normal yield. it feels super dirty to do this at the
compose level though, I'd almost rather remove this library and have it
be a thing in Koa. There's no guarantee that people will use compose() to
compose middleware so unless we make it a formal thing it seems awkward

Reply to this email directly or view it on GitHubhttps://github.com//issues/6#issuecomment-37492228
.

Implemented in #51 and #52

Is there any consensus on this yet? I'd like to get an implementation merged.

With the Koa v2 release in sight, I'd like to get a wrapper implementation merged. How should I work towards this goal?

Is this still an open issue, or could there be consensus on the hope of node 8's experimental async_hook to rescue the situation soon enough?

I think async_hook is more diagnostic oriented. While useful, I think wrappers will have other uses.

Heh, yea. To be fair, that was the only use case I had in mind anyway.

There is @feathersjs/hooks which allows hooking any async function, based on koa-compose.