-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Use ES6 classes - v2.x #605
Conversation
lib/application.js
Outdated
* | ||
* @api private | ||
*/ | ||
|
||
createContext(req, res) { |
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.
Instead of keeping createContext
around you could replace where it's used in the http callback with the call to the constructor.
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.
true. again, did it for reverse compatibility, but it doesn't make sense to keep it, you're right
@simplyianm the code as-is won't be merge. do you want to update it based on our comments?
app.Context = class Context extends app.Context {
whatever(){}
} Anyone have opinions on this signature before @simplyianm or anyone else updates this PR? |
FWIW i'm -1/2 on this PR because I don't really see any benefits and making it classes just makes it more confusing to me |
I'll update it
|
+1 it's easy to extend Application/Context/Request/Response rather than override |
ping @simplyianm |
15a1669
to
f14cd17
Compare
Fixed @tejasmanohar @jonathanong |
nice! probably best to squash so commit history isn't polluted :P |
ping @jonathanong |
@@ -17,7 +17,7 @@ const only = require('only'); | |||
* Prototype. | |||
*/ |
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.
The comment Prototype
should go since this is not a prototype object anymore, but a class (function) that has a prototype.
This is great imo. It is in line with the new |
other than that, I'm +0 |
I actually disagree with extending the base class -- things like that
|
well for one, people are using it: #652 middleware is actually an anti-pattern in koa unless it's something you really want to be executed on every request (like checking etags and setting the status as 304 if the body is fresh). everything should be a function that you can just middleware:
for example, instead of having a body parsing middleware that always sets now, back to extending the context. the two reasons for extending context is:
of course, this is prone to user error, so we never really recommended it strongly, but it's a feature that shouldn't be removed. |
similar philosophy: vercel/micro#8 koa is the same except we abstract |
I don't like this PR because it won't fix much given that delegate is still used (multiple inheritance isn't supported in ES6). |
@PlasmaPower that is true, but I don't think it has something to do with multiple inheritance. delegate only defines getters that delegate to another object. It could be replaced by writing the getters into the class manually, which would be much better for static analysis and IDEs to understand. |
@felixfbecker could you explain what you mean by "writing the getters into the class manually"? |
@PlasmaPower Well instead of doing this delegate(Context.prototype, 'response')
.method('attachment')
.method('redirect')
.method('remove')
.method('vary')
.method('set')
.method('append')
.access('status')
.access('message')
.access('body')
.access('length')
.access('type')
.access('lastModified')
.access('etag')
.getter('headerSent')
.getter('writable'); you simply write the getters and methods into the class manually: class Context {
attachment(filename) {
this.response.attachment(filename);
}
get status() {
return this.response.status;
}
set status(status) {
this.response.status = status;
}
} This is what delegate does. It is a lot more verbose to write it manually. I personally have never been a fan of this delegation because it is not clear for someone who reads the code wehter |
@felixfbecker yeah but that's a pain compared to simply extending Response and Request. |
@PlasmaPower Always favor composition over inheritance 😉 |
Drops |
hi, I'm going to close this for now. I don't think we need to move every single thing to ES6 — there's not much benefit in switching to ES6 classes. thanks! |
This converts
Context
,Request
, andResponse
to ES6 classes to make things more semantic.See #597 for the old PR.