From 38a7eedeb97924f3cf830961df9110dc0ca610c1 Mon Sep 17 00:00:00 2001 From: Nadav Ivgi Date: Sat, 21 Oct 2017 09:32:37 +0300 Subject: [PATCH] Simplify request/response association using EventEmitter's once() --- index.js | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index f65d3e3..c0bd016 100644 --- a/index.js +++ b/index.js @@ -2,9 +2,10 @@ const path = require('path'); const net = require('net'); +const {EventEmitter} = require('events'); const _ = require('lodash'); -class LightningClient { +class LightningClient extends EventEmitter { constructor(rpcPath) { if (!path.isAbsolute(rpcPath)) { throw new Error('The rpcPath must be an absolute path'); @@ -14,6 +15,7 @@ class LightningClient { console.log(`Connecting to ${rpcPath}`); + super(); this.rpcPath = rpcPath; this.reconnectWait = 0.5; this.reconnectTimeout = null; @@ -41,8 +43,6 @@ class LightningClient { }); }); - this.waitingFor = {}; - this.client.on('data', data => { _.each(LightningClient.splitJSON(data.toString()), str => { let dataObject = {}; @@ -52,12 +52,7 @@ class LightningClient { return; } - if (!_.isFunction(_self.waitingFor[dataObject.id])) { - return; - } - - _self.waitingFor[dataObject.id].call(_self, dataObject); - delete _self.waitingFor[dataObject.id]; + _self.emit('res:' + dataObject.id, dataObject); }); }); } @@ -131,22 +126,20 @@ class LightningClient { // Wait for the client to connect return this.clientConnectionPromise - .then(() => { + .then(() => new Promise((resolve, reject) => { // Wait for a response - return new Promise((resolve, reject) => { - this.waitingFor[callInt] = response => { - if (_.isNil(response.error)) { - resolve(response.result); - return; - } - - reject(new Error(response.error)); - }; - - // Send the command - _self.client.write(JSON.stringify(sendObj)); + this.once('res:' + callInt, response => { + if (_.isNil(response.error)) { + resolve(response.result); + return; + } + + reject(new Error(response.error)); }); - }); + + // Send the command + _self.client.write(JSON.stringify(sendObj)); + })); } devBlockheight() {