-
Notifications
You must be signed in to change notification settings - Fork 146
Add recursive composition (semver-minor) #81
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #81 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 1 1
Lines 18 21 +3
Branches 5 6 +1
=====================================
+ Hits 18 21 +3
Continue to review full report at Codecov.
|
This isn't transient to children, correct? For instance, if Koa uses a router which uses more middleware, the wrapper will be attached to the router but not to the middleware. I'm not sure this is better than defining a helper function to wrap middleware and calling |
@PlasmaPower Yes, not transient - and I agree, not necessarily a game changing "feature", unless koa-router returned an POJO array (which it doesn't). |
index = i | ||
let fn = middleware[i] | ||
if (isArray(fn)) fn = compose(fn) | ||
if (i === middleware.length) fn = next |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my concern here is that we compose every time we call dispatch. this might have performance issues.
i'd prefer to flatten the array at compose()
, but that might not be what you want as you won't be able to edit nested middleware during runtime (which i don't think you should be doing)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I remember correctly, the point was not to mutate middleware at runtime, but to allow consumer (Koa) to pass nested/wrapped as an array - and instead of flattening the array I simply opted to compose recursively (saw it as a quality of life feature)
But you're right, I would compose on each dispatch. Maybe minuscule penalty, but definitely unnecessary as pre-flattening would avoid that.
But, this could be easily done by consumer as well. The whole point was to allow Koa to add some clean logic to wrap each middleware, but I'm not convinced this is of concern anymore. If anything, I think this can be closed and forgotten (I already had).
This is my take on the "wrapper hook/talk". It's minimally invasive.
The idea is to follow this up with:
Application.use()
accepts arrays with middleware, e.g.app.use([mw1, mw2])
.Application.wrap()
] to globally wrap allthis.middleware
with whatever's insidethis.wrappers
with a simple map returning[[wrapper1, mw], [wrapper1, mw2]]
.As far as I can foresee, this should be unrestrictive enough for developers to be creative as one see fit, simple e.g:
Related issues:
koajs/compose: #6
koajs/koa: koajs/koa#219
Related PRs:
koajs/compose: #52
koajs/compose: #53
koajs/koa: koajs/koa#707
koajs/koa: koajs/koa#694 introduces
context.wrappers
, but I personally would enjoy context not being part of the solution.