Skip to content

Commit 88c3f57

Browse files
committed
lib: refactor internal/socket_list
PR-URL: #11406 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent f04387e commit 88c3f57

File tree

1 file changed

+80
-79
lines changed

1 file changed

+80
-79
lines changed

lib/internal/socket_list.js

+80-79
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,105 @@
11
'use strict';
22

3-
module.exports = {SocketListSend, SocketListReceive};
4-
53
const EventEmitter = require('events');
6-
const util = require('util');
74

85
// This object keep track of the socket there are sended
9-
function SocketListSend(slave, key) {
10-
EventEmitter.call(this);
6+
class SocketListSend extends EventEmitter {
7+
constructor(child, key) {
8+
super();
9+
this.key = key;
10+
this.child = child;
11+
}
1112

12-
this.key = key;
13-
this.slave = slave;
14-
}
15-
util.inherits(SocketListSend, EventEmitter);
13+
_request(msg, cmd, callback) {
14+
var self = this;
15+
16+
if (!this.child.connected) return onclose();
17+
this.child.send(msg);
1618

17-
SocketListSend.prototype._request = function(msg, cmd, callback) {
18-
var self = this;
19+
function onclose() {
20+
self.child.removeListener('internalMessage', onreply);
21+
callback(new Error('child closed before reply'));
22+
}
1923

20-
if (!this.slave.connected) return onclose();
21-
this.slave.send(msg);
24+
function onreply(msg) {
25+
if (!(msg.cmd === cmd && msg.key === self.key)) return;
26+
self.child.removeListener('disconnect', onclose);
27+
self.child.removeListener('internalMessage', onreply);
2228

23-
function onclose() {
24-
self.slave.removeListener('internalMessage', onreply);
25-
callback(new Error('Slave closed before reply'));
29+
callback(null, msg);
30+
}
31+
32+
this.child.once('disconnect', onclose);
33+
this.child.on('internalMessage', onreply);
2634
}
2735

28-
function onreply(msg) {
29-
if (!(msg.cmd === cmd && msg.key === self.key)) return;
30-
self.slave.removeListener('disconnect', onclose);
31-
self.slave.removeListener('internalMessage', onreply);
36+
close(callback) {
37+
this._request({
38+
cmd: 'NODE_SOCKET_NOTIFY_CLOSE',
39+
key: this.key
40+
}, 'NODE_SOCKET_ALL_CLOSED', callback);
41+
}
3242

33-
callback(null, msg);
43+
getConnections(callback) {
44+
this._request({
45+
cmd: 'NODE_SOCKET_GET_COUNT',
46+
key: this.key
47+
}, 'NODE_SOCKET_COUNT', function(err, msg) {
48+
if (err) return callback(err);
49+
callback(null, msg.count);
50+
});
3451
}
52+
}
3553

36-
this.slave.once('disconnect', onclose);
37-
this.slave.on('internalMessage', onreply);
38-
};
39-
40-
SocketListSend.prototype.close = function close(callback) {
41-
this._request({
42-
cmd: 'NODE_SOCKET_NOTIFY_CLOSE',
43-
key: this.key
44-
}, 'NODE_SOCKET_ALL_CLOSED', callback);
45-
};
46-
47-
SocketListSend.prototype.getConnections = function getConnections(callback) {
48-
this._request({
49-
cmd: 'NODE_SOCKET_GET_COUNT',
50-
key: this.key
51-
}, 'NODE_SOCKET_COUNT', function(err, msg) {
52-
if (err) return callback(err);
53-
callback(null, msg.count);
54-
});
55-
};
5654

5755
// This object keep track of the socket there are received
58-
function SocketListReceive(slave, key) {
59-
EventEmitter.call(this);
56+
class SocketListReceive extends EventEmitter {
57+
constructor(child, key) {
58+
super();
6059

61-
this.connections = 0;
62-
this.key = key;
63-
this.slave = slave;
60+
this.connections = 0;
61+
this.key = key;
62+
this.child = child;
6463

65-
function onempty(self) {
66-
if (!self.slave.connected) return;
64+
function onempty(self) {
65+
if (!self.child.connected) return;
6766

68-
self.slave.send({
69-
cmd: 'NODE_SOCKET_ALL_CLOSED',
70-
key: self.key
67+
self.child.send({
68+
cmd: 'NODE_SOCKET_ALL_CLOSED',
69+
key: self.key
70+
});
71+
}
72+
73+
this.child.on('internalMessage', (msg) => {
74+
if (msg.key !== this.key) return;
75+
76+
if (msg.cmd === 'NODE_SOCKET_NOTIFY_CLOSE') {
77+
// Already empty
78+
if (this.connections === 0) return onempty(this);
79+
80+
// Wait for sockets to get closed
81+
this.once('empty', onempty);
82+
} else if (msg.cmd === 'NODE_SOCKET_GET_COUNT') {
83+
if (!this.child.connected) return;
84+
this.child.send({
85+
cmd: 'NODE_SOCKET_COUNT',
86+
key: this.key,
87+
count: this.connections
88+
});
89+
}
7190
});
7291
}
7392

74-
this.slave.on('internalMessage', (msg) => {
75-
if (msg.key !== this.key) return;
76-
77-
if (msg.cmd === 'NODE_SOCKET_NOTIFY_CLOSE') {
78-
// Already empty
79-
if (this.connections === 0) return onempty(this);
80-
81-
// Wait for sockets to get closed
82-
this.once('empty', onempty);
83-
} else if (msg.cmd === 'NODE_SOCKET_GET_COUNT') {
84-
if (!this.slave.connected) return;
85-
this.slave.send({
86-
cmd: 'NODE_SOCKET_COUNT',
87-
key: this.key,
88-
count: this.connections
89-
});
90-
}
91-
});
92-
}
93-
util.inherits(SocketListReceive, EventEmitter);
93+
add(obj) {
94+
this.connections++;
9495

95-
SocketListReceive.prototype.add = function(obj) {
96-
this.connections++;
96+
// Notify previous owner of socket about its state change
97+
obj.socket.once('close', () => {
98+
this.connections--;
9799

98-
// Notify previous owner of socket about its state change
99-
obj.socket.once('close', () => {
100-
this.connections--;
100+
if (this.connections === 0) this.emit('empty', this);
101+
});
102+
}
103+
}
101104

102-
if (this.connections === 0) this.emit('empty', this);
103-
});
104-
};
105+
module.exports = {SocketListSend, SocketListReceive};

0 commit comments

Comments
 (0)