n onempty(self) { if (!self.child.connected) return; self.child._send({ cmd: 'NODE_SOCKET_ALL_CLOSED', key: self.key }, undefined, true); } this.child.on('internalMessage', (msg) => { if (msg.key !== this.key) return; if (msg.cmd === 'NODE_SOCKET_NOTIFY_CLOSE') { // Already empty if (this.connections === 0) return onempty(this); // Wait for sockets to get closed this.once('empty', onempty); } else if (msg.cmd === 'NODE_SOCKET_GET_COUNT') { if (!this.child.connected) return; this.child._send({ cmd: 'NODE_SOCKET_COUNT', key: this.key, count: this.connections }); } }); } add(obj) { this.connections++; // Notify the previous owner of the socket about its state change obj.socket.once('close', () => { this.connections--; if (this.connections === 0) this.emit('empty', this); }); } } module.exports = { SocketListSend, SocketListReceive };