Skip to content
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

Merged
merged 1 commit into from
Dec 14, 2016
Merged

Refactor WebSocket to use class syntax #930

merged 1 commit into from
Dec 14, 2016

Conversation

lpinca
Copy link
Member

@lpinca lpinca commented Dec 9, 2016

  • Refactor WebSocket to use the ES6 class syntax.
  • Move cleanupWebsocketResources and establishConnection to WebSocket.prototype.
  • Rename establishConnection to setSocket and cleanupWebsocketResources to finalize.
  • Keep initAsServerClient and initAsClient as completely private API.

@lpinca
Copy link
Member Author

lpinca commented Dec 9, 2016

I think the new methods names (setSocket and finalize) are better than the original ones but I'm not very happy with them. If you have better ideas please share!

* @type {Number}
*/
get bufferedAmount () {
var amount = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let?

Copy link
Member Author

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.

Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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 = '';

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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?

Copy link
Member Author

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

ws/lib/Sender.js

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;
}
}
just check that data is truthy.

Copy link
Member Author

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 || {};

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?

@lpinca
Copy link
Member Author

lpinca commented Dec 9, 2016

@ericmdantas thank you for reviewing, this is greatly appreciated! Let me know if you have more suggestions.

@ericmdantas
Copy link

@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.

@lpinca
Copy link
Member Author

lpinca commented Dec 14, 2016

I'll merge this later today if there are no objections.

@lpinca lpinca merged commit 099c636 into master Dec 14, 2016
@lpinca lpinca deleted the use/class-syntax branch December 14, 2016 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants