-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Refactor WebSocket
to use class syntax
#930
Conversation
ce4c369
to
16e4656
Compare
I think the new methods names ( |
* @type {Number} | ||
*/ | ||
get bufferedAmount () { | ||
var amount = 0; |
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.
let
?
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.
Not yet as it causes deoptimizations.
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.
I thought only compound let
statements caused deoptimization?
source: http://stackoverflow.com/questions/34595356/what-does-compound-let-const-assignment-mean
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.
I think it's still better to keep var
as it is consistent with the rest of the code base and we are 100% safe.
Switching to let
should be very easy with an eslint rule when we are sure it doesn't cause any harm.
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.
Makes sense. I wish there was documentation somewhere for v8's optimizations. It would make things like this so much easier.
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.
cleanupWebsocketResources.call(this, true); | ||
} | ||
}; | ||
if (!data) data = ''; |
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.
Use default param value instead?
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.
Does not work on Node 4.
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.
@lpinca Now that node 6 is the new LTS, maybe we should use that our minimum which also allows us to use default params.
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.
@3rd-Eden I think that dropping Node 4 support is a bit too much. For example AWS Lambda does not support Node 6 yet.
Now using ws with Lambda does not make much sense 😄 but I think you get my point.
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.
@lpinca ok, we'll wait another week. Or until node 4 LTS runs out completely.
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.
Or until node 4 LTS runs out completely.
Yeah that's my idea but that will not happen before April 2017 and after that there is 1 more year of maintenance.
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.
Maybe use === undefined
to mirror the behavior of default parameters, so that making the eventual switch doesn't cause any unintended side effects?
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.
@JoshuaWise no strong opinion on this but for example
Lines 154 to 163 in 5a8ead5
if (data && !Buffer.isBuffer(data)) { | |
if (data instanceof ArrayBuffer) { | |
data = Buffer.from(data); | |
} else if (ArrayBuffer.isView(data)) { | |
data = viewToBuffer(data); | |
} else { | |
data = Buffer.from(typeof data === 'number' ? data.toString() : data); | |
readOnly = false; | |
} | |
} |
data
is truthy.
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.
We should probably also allow 0
which doesn't work atm, so this makes sense but maybe it's better to fix the issue in another PR/commit as we should fix the edge case also in Sender
.
throw new Error('not opened'); | ||
} | ||
|
||
options = options || {}; |
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.
Use default param value instead?
@ericmdantas thank you for reviewing, this is greatly appreciated! Let me know if you have more suggestions. |
@lpinca, no problem. I'm a bit busy at the moment, but I'll take a closer look when possible. But, from what I've seen, it's good to go. |
16e4656
to
6e4f364
Compare
I'll merge this later today if there are no objections. |
WebSocket
to use the ES6 class syntax.cleanupWebsocketResources
andestablishConnection
toWebSocket.prototype
.establishConnection
tosetSocket
andcleanupWebsocketResources
tofinalize
.initAsServerClient
andinitAsClient
as completely private API.