koajs / compose

Middleware composition utility
MIT License
1k stars 147 forks source link

add compose.hook or similar #6

Open tj opened 10 years ago

tj commented 10 years ago

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

mcwhittemore commented 10 years ago

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");
  }
});
tj commented 10 years ago

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

mcwhittemore commented 10 years ago

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/koajs/compose/issues/6#issuecomment-37492228 .

PlasmaPower commented 8 years ago

Implemented in #51 and #52

PlasmaPower commented 8 years ago

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

PlasmaPower commented 8 years ago

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

fl0w commented 7 years ago

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?

PlasmaPower commented 7 years ago

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

fl0w commented 7 years ago

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

bertho-zero commented 3 years ago

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