From 45134de7401b58f0118deb16bf0644495715fbdb Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Tue, 3 Feb 2015 22:24:17 +0100 Subject: jsonrpc.js file && batch polling --- lib/providermanager.js | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'lib/providermanager.js') 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; } -- cgit v1.2.3