Comments (19)
@jonathanong The problem occurs if you call app.use to add any middleware after you've already triggered a call of app.callback, since this.middleware, the argument that is passed to compose is a mutable array.
this would previously work fine as it would reference the now mutated array, but since the flatten changes the semantics by copying at call time of compose with the flatten, anything added to the middleware after the call to compose is effectively ignored.
I would think that even if this wasn't an envisioned use case, it still seems like a breaking change since it does, even if inadvertently, change the way the module works.
Thanks for all the hard work!
from compose.
Previously, one could mutate the array after calling compose.
Interesting, I had no idea that was possible. I'll rewrite flatten to be lazy.
from compose.
Interesting, I had no idea that was possible.
Yeah, admittedly our usage does feel a tad unusual..
I'll rewrite flatten to be lazy.
Thanks! I suppose it'd be a nice micro optimization (remove a copy
) if you do, regardless of 'dynamic' usage like this..
from compose.
Thanks! I suppose it'd be a nice micro optimization (remove a copy) if you do, regardless of 'dynamic' usage like this..
Actually, this is really bad for performance, as it means it has to be flattened every time (previously it could be cached). In fact, that makes me reconsider this, maybe we should just remove variadic argument support... making the next
PR anyway.
from compose.
Okay, I've opened #68 and #69 to solve this. However, I'm personally in favor of just reverting the variadic argument change, as the performance cost is now high.
from compose.
agreed, I would vote to revert the changes published in 2.5
from compose.
π +1οΌI'm having the same issue. It should be a break change.
from compose.
@jonathanong Might want to revert those changes sooner rather than later.
from compose.
can you guys explain your use-cases? this wasn't something we were ever supporting.
how much performance decrease are we talking about by doing the dynamic version?
from compose.
I'm not entirely sure how koa uses koa-compose but this change definitely breaks my application. Can you revert it?
from compose.
Having same issue, it is breaking our App.
from compose.
It seems that a patch along the following lines could preserve the object ownership, support variadic args, and not introduce any copying or dependencies:
function compose(middleware) {
if (arguments.length > 1) {
middleware = Array.prototype.slice.call(arguments);
}
// ...
}
This wouldn't support nested flattening, e.g. compose( [a,b], [c] )
. Not sure if that's considered an important feature.
from compose.
I think compose([a, b], [c])
is pretty useful if you're working with lists of middleware. Maybe we should run benchmarks on these different implementations or something.
from compose.
yeah i'm gonna revert it now.
The problem occurs if you call app.use to add any middleware after you've already triggered a call of app.callback
@conorhastings why are you doing this, though?
from compose.
also, if this breaks usage within a 3rd party middleware/router/etc., which ones?
from compose.
i have published v2.5.1
and v3.2.1
which reverts the change. v2.5.0
and v3.2.0
are marked as deprecated on npm. let me know if you have any issues with these releases.
- changing this topic to "officially support mutating
middleware
". i see this as code-smell and would like to understand the use-case - moving discussion to continuing variadic support in #63. this is still a feature i would like to add, but we will do so only in
koa-compose@4
from compose.
@jonathanong it was likely a mistake in our app code, not sure why we were doing it, definitely not purposefully relying on array mutation π . Have already refactored our app code so that it will work with 2.5 , haven't experienced anything caused by external modules myself.
In my opinion I think variadic support is much more important than supporting apps that rely on array mutation of an argument already passed into a function, would just like to see it published as a major version
from compose.
@conorhastings that's good to know!
yeah, you should be sure to do app.listen()
or app.callback()
only after you've added all your middleware. i think this is an oversight in the docs.
from compose.
instead of supporting it here, we will add an ability to configure your compose function in koa koajs/koa#1568
from compose.
Related Issues (20)
- If `fn` is an asynchronous function, this `try catch` will be useless οΌ HOT 2
- I found that some code in the source code like `Promise.reject(),Promise.resolve()` seems redundant HOT 1
- Behavior changes on 4.2.0 HOT 5
- Add debug logs before and after each middleware executes HOT 1
- License is not included
- I donβt understand how the error is thrown when I find that next is called repeatedly. HOT 2
- Why use Promise.resolve() to wrap the execution results of each middleware? HOT 2
- Abnormal middleware errors occurred and KOA could not catch error events. How about the following changes
- [Question/Proposal] Sync/Async execution HOT 1
- Set default branch to next, default npm release to v3 HOT 1
- guard against unhandled next()s in development HOT 15
- Reorganize tests, and switch to Jest HOT 5
- when i use koa-compose with koa-router, koa-router does not work. HOT 6
- [Q&A] About Promise in the source code. HOT 1
- Outdated yield tests
- I can't catch the error ~ HOT 6
- Can't use destructuring with async await. HOT 2
- Unit test to verify if it handles wrapped non-async functions is wrong. HOT 1
- Control passed to handler before completing middleware chain HOT 4
- Multiple next with non async function HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from compose.