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

child_process: refactor self=this in socket_list #5860

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions lib/internal/socket_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@ SocketListSend.prototype.getConnections = function getConnections(callback) {
function SocketListReceive(slave, key) {
EventEmitter.call(this);

var self = this;

this.connections = 0;
this.key = key;
this.slave = slave;

function onempty() {
function onempty(self) {
if (!self.slave.connected) return;

self.slave.send({
Expand All @@ -73,36 +71,34 @@ function SocketListReceive(slave, key) {
});
}

this.slave.on('internalMessage', function(msg) {
if (msg.key !== self.key) return;
this.slave.on('internalMessage', (msg) => {
if (msg.key !== this.key) return;

if (msg.cmd === 'NODE_SOCKET_NOTIFY_CLOSE') {
// Already empty
if (self.connections === 0) return onempty();
if (this.connections === 0) return onempty(this);

// Wait for sockets to get closed
self.once('empty', onempty);
this.once('empty', onempty);
} else if (msg.cmd === 'NODE_SOCKET_GET_COUNT') {
if (!self.slave.connected) return;
self.slave.send({
if (!this.slave.connected) return;
this.slave.send({
cmd: 'NODE_SOCKET_COUNT',
key: self.key,
count: self.connections
key: this.key,
count: this.connections
});
}
});
}
util.inherits(SocketListReceive, EventEmitter);

SocketListReceive.prototype.add = function(obj) {
var self = this;

this.connections++;

// Notify previous owner of socket about its state change
obj.socket.once('close', function() {
self.connections--;
obj.socket.once('close', () => {
this.connections--;

if (self.connections === 0) self.emit('empty');
if (this.connections === 0) this.emit('empty', this);
Copy link
Contributor

Choose a reason for hiding this comment

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

Where does the passed this end up here? Seems unnecessary?

Copy link
Contributor

Choose a reason for hiding this comment

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

It gets passed to onempty() which is the empty event handler added on line 82.

Choose a reason for hiding this comment

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

To send the context as a parameter seems ambigous. Since we are using arrow functions, why not just binding the method l.82 ? onempty.bind(this)

Copy link
Member Author

Choose a reason for hiding this comment

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

@ludoblues bind is slow and it makes the resulting function slow too. It is also very scarcely used in Node where this pattern is very common. See http://stackoverflow.com/questions/17638305/why-is-bind-slower-than-a-closure

});
};