diff options
author | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-02-04 05:24:17 +0800 |
---|---|---|
committer | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-02-04 05:24:17 +0800 |
commit | 45134de7401b58f0118deb16bf0644495715fbdb (patch) | |
tree | ebe0ba3fe2de88ae74ca3fa009b3b4a0d7c64623 /lib/providermanager.js | |
parent | f3ce1f07c42924a116e9c215ea95fb0a2b0217f0 (diff) | |
download | go-tangerine-45134de7401b58f0118deb16bf0644495715fbdb.tar go-tangerine-45134de7401b58f0118deb16bf0644495715fbdb.tar.gz go-tangerine-45134de7401b58f0118deb16bf0644495715fbdb.tar.bz2 go-tangerine-45134de7401b58f0118deb16bf0644495715fbdb.tar.lz go-tangerine-45134de7401b58f0118deb16bf0644495715fbdb.tar.xz go-tangerine-45134de7401b58f0118deb16bf0644495715fbdb.tar.zst go-tangerine-45134de7401b58f0118deb16bf0644495715fbdb.zip |
jsonrpc.js file && batch polling
Diffstat (limited to 'lib/providermanager.js')
-rw-r--r-- | lib/providermanager.js | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/lib/providermanager.js b/lib/providermanager.js index 38c0f6c46..ce208917d 100644 --- a/lib/providermanager.js +++ b/lib/providermanager.js @@ -23,7 +23,9 @@ * @date 2014 */ -var web3 = require('./web3'); // jshint ignore:line +var web3 = require('./web3'); +var jsonrpc = require('./jsonrpc'); + /** * Provider manager object prototype @@ -37,21 +39,34 @@ var web3 = require('./web3'); // jshint ignore:line var ProviderManager = function() { this.polls = []; this.provider = undefined; - this.id = 1; var self = this; var poll = function () { if (self.provider) { - self.polls.forEach(function (data) { - var result = self.send(data.data); - + var pollsBatch = self.polls.map(function (data) { + return data.data; + }); + + var payload = jsonrpc.toBatchPayload(pollsBatch); + var results = self.provider.send(payload); + + self.polls.forEach(function (data, index) { + var result = results[index]; + + if (!jsonrpc.isValidResponse(result)) { + return; + } + + result = result.result; // dont call the callback if result is not an array, or empty one if (!(result instanceof Array) || result.length === 0) { return; } data.callback(result); + }); + } setTimeout(poll, 1000); }; @@ -61,21 +76,16 @@ var ProviderManager = function() { /// sends outgoing requests /// @params data - an object with at least 'method' property ProviderManager.prototype.send = function(data) { - - data.jsonrpc = '2.0'; - data.params = data.params || []; - data.id = this.id++; + var payload = jsonrpc.toPayload(data.method, data.params); if (this.provider === undefined) { console.error('provider is not set'); return null; } - //TODO: handle error here? - var result = this.provider.send(data); - result = JSON.parse(result); + var result = this.provider.send(payload); - if (result.error) { + if (!jsonrpc.isValidResponse(result)) { console.log(result.error); return null; } |