Skip to content

Commit

Permalink
child_process: refactor self=this in socket_list
Browse files Browse the repository at this point in the history
The socket list module (used by child_process) currently uses the
`var self = this;` pattern for context in several places, this PR
replaces this with arrow functions or passing a parameter in where
appropriate.

Note that the `var self = this` in the _request is intentioanlly
left in place since it is not trivial to refactor it and the current
pattern isn't bad given the use case.

PR-URL: #5860
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Brian White <[email protected]>
  • Loading branch information
benjamingr authored and evanlucas committed Mar 31, 2016
1 parent 58b5c1e commit 660ec9f
Showing 1 changed file with 12 additions and 16 deletions.
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);
});
};

0 comments on commit 660ec9f

Please sign in to comment.