From 4fb531c8244baf9101ad9a9411241f4f963be866 Mon Sep 17 00:00:00 2001 From: ebaauw Date: Wed, 19 Jun 2019 22:20:23 +0200 Subject: [PATCH] Update ZpListener.js Code cleanup --- lib/ZpListener.js | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/ZpListener.js b/lib/ZpListener.js index 25efbc4..640dfe2 100644 --- a/lib/ZpListener.js +++ b/lib/ZpListener.js @@ -33,17 +33,17 @@ class ZpListener extends events.EventEmitter { if ( alias.family === 'IPv4' && alias.internal === false ) { - myIps.push([alias.address, alias.netmask]) + myIps.push({ address: alias.address, netmask: alias.netmask }) } } } if (myIps.length === 1 && ip == null) { - return myIps[0][0] + return myIps[0].address } if (myIps.length > 0 && ip != null) { - for (const myIp of myIps) { - if (ZpListener._inSameNetwork(ip, myIp[0], myIp[1])) { - return myIp[0] + for (const { address, netmask } of myIps) { + if (ZpListener._inSameNetwork(ip, address, netmask)) { + return address } } } @@ -79,7 +79,7 @@ class ZpListener extends events.EventEmitter { const client = this._clients[array[2]] if ( array[1] === 'notify' && client !== null && - array[3] != null && array[4] != null + array[3] != null && array[4] != null && array[5] === 'Event' ) { // this._debug('%s %s event: %s', array[3], array[4], request.body) const payload = await this._parser.parse(request.body) @@ -95,7 +95,6 @@ class ZpListener extends events.EventEmitter { }) this._server.on('error', (error) => { this.emit('error', error) }) this._server.on('close', () => { - // Doesn't get triggered? this.emit('close', this._callbackUrl) delete this._callbackUrl }) @@ -133,25 +132,19 @@ class ZpListener extends events.EventEmitter { }) } - close () { - this.emit('close', this._callbackUrl) - delete this._callbackUrl - this._server.close() - } - async addClient (zpClient) { this._debug('addClient(%j)', zpClient) this._clients[zpClient.id] = zpClient - await this.listen(zpClient.ip) + await this.listen(zpClient.address) const callbackUrl = this._callbackUrl + '/' + zpClient.id this._debug('addClient(%j) => %j', zpClient, callbackUrl) return callbackUrl } async removeClient (zpClient) { - delete this._clients[zpClient._id] + delete this._clients[zpClient.id] if (Object.keys(this._clients).length === 0) { - this.close() + this._server.close() } } }