-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
function *(next) middleware signature #1
Conversation
the problem with this is that it still only does the linear walk, more like Connect, so we'd have to do the more complex dispatch that yields control back to each one in reverse like we had before |
can you be specific? i can't think of a case that koa currently covers but this doesn't. i should add tests for errors, though. |
ill take another look in the morning maybe i read it wrong |
nice! |
@jonathanong yeah the problem is like in our original attempt to do this sort of syntax, where you have to manually perform the "stack" behaviour that is naturally occurring in what we have implemented right now. This PR loops forward but doesn't loop in reverse to resume generators the second time for the "unwinding" portion of the stack. We could definitely do it, but there's certainly a decrease in elegance from a composition/internal point a view (I'm not biased to either really) |
Is there a test case for what you're talking about? Not sure what unwinding the stack is because I don't have a CS background so my jargon is limited. Would help if you had tests for compose as well :P |
haha yeah tests would help. ill dig into this in a little bit |
finally working on a real-world koa app :D haha |
koajs/route is one example of why I don't think we reallllly need to go this route, once a base set of decent middleware are established people won't really be writing them often, so it may be overkill to try and optimize for that, but we'll see |
you making the component registry a koa app? or slate? or both!? |
yeah, especially since i would be using context methods way more than middleware unlike connect/express. |
neither of those two actually but maybe the registry later. yeah totally, yield makes that stuff a lot nicer |
just realized. if we do: function *next(){
yield middleware[i++].call(this, next);
} then we can do |
maybe I'm misunderstanding, but isn't the unwinding covered here? Line 42 in 779d13f
|
yes, i assume so, which is why i'm confused :P i added an error handling test as well. not sure what other cases there are. |
nvm yeah you guys are right I read the implementation wrong haha |
going to see what kind of impact this has on the benchmarks but otherwise looks good! |
try this too: function *next(){
yield middleware[i++].call(ctx, next);
} might be a little slower, but it should be backwards compatible (so we can delay changing everything for now). |
with this branch:
koa's current master:
more or less the same :D |
pretty funny how it's backwards compatible due to how co works hahha |
we might want to warn when people pass a non-generator function that is a bit of a silent WTF, I'm just refactoring koa's tests then we just have to update docs and it should be good to go |
function *(next) middleware signature
Hmmmm I wonder why the dispatcher was so much faster |
a lot less function calls, especially since this goes through |
wooo! if we merge this, middleware signature would be:
if you want to delegate middleware, you would have to do:
we could add a context helper or do
next(fn)
.it kind of has an error check when there are double
next()
s. it will also work only if you doyield next();
, notreturn next()
oryield next
, which is more strict than before, a good thing.this is much more elegant than the other versions we tried. plus, i didn't like special casing
'next'
. it's also easier to use:(unless you want to memoize the composition, which i would prefer).