batch requests
This commit is contained in:
parent
95a3b5a35f
commit
9da9bfdbd4
66
lib/web3.js
66
lib/web3.js
|
@ -37,7 +37,7 @@ var utils = require('./utils/utils');
|
||||||
var sha3 = require('./utils/sha3');
|
var sha3 = require('./utils/sha3');
|
||||||
var extend = require('./web3/extend');
|
var extend = require('./web3/extend');
|
||||||
var Batch = require('./web3/batch');
|
var Batch = require('./web3/batch');
|
||||||
var Contract = require('./web3/contract');
|
var Property = require('./web3/property');
|
||||||
|
|
||||||
function Web3 (provider) {
|
function Web3 (provider) {
|
||||||
this._requestManager = new RequestManager(provider);
|
this._requestManager = new RequestManager(provider);
|
||||||
|
@ -51,6 +51,9 @@ function Web3 (provider) {
|
||||||
version: version.version
|
version: version.version
|
||||||
};
|
};
|
||||||
this._extend = extend(this);
|
this._extend = extend(this);
|
||||||
|
this._extend({
|
||||||
|
properties: properties()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Web3.prototype.setProvider = function (provider) {
|
Web3.prototype.setProvider = function (provider) {
|
||||||
|
@ -62,8 +65,6 @@ Web3.prototype.reset = function () {
|
||||||
this.settings = new Settings();
|
this.settings = new Settings();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Web3.prototype.toHex = utils.toHex;
|
Web3.prototype.toHex = utils.toHex;
|
||||||
Web3.prototype.toAscii = utils.toAscii;
|
Web3.prototype.toAscii = utils.toAscii;
|
||||||
Web3.prototype.toUtf8 = utils.toUtf8;
|
Web3.prototype.toUtf8 = utils.toUtf8;
|
||||||
|
@ -86,38 +87,37 @@ Web3.prototype.fromICAP = function (icap) {
|
||||||
return iban.address();
|
return iban.address();
|
||||||
};
|
};
|
||||||
|
|
||||||
//var web3Properties = [
|
var properties = function () {
|
||||||
//new Property({
|
return [
|
||||||
//name: 'version.client',
|
new Property({
|
||||||
//getter: 'web3_clientVersion'
|
name: 'version.client',
|
||||||
//}),
|
getter: 'web3_clientVersion'
|
||||||
//new Property({
|
}),
|
||||||
//name: 'version.network',
|
new Property({
|
||||||
//getter: 'net_version',
|
name: 'version.network',
|
||||||
//inputFormatter: utils.toDecimal
|
getter: 'net_version',
|
||||||
//}),
|
inputFormatter: utils.toDecimal
|
||||||
//new Property({
|
}),
|
||||||
//name: 'version.ethereum',
|
new Property({
|
||||||
//getter: 'eth_protocolVersion',
|
name: 'version.ethereum',
|
||||||
//inputFormatter: utils.toDecimal
|
getter: 'eth_protocolVersion',
|
||||||
//}),
|
inputFormatter: utils.toDecimal
|
||||||
//new Property({
|
}),
|
||||||
//name: 'version.whisper',
|
new Property({
|
||||||
//getter: 'shh_version',
|
name: 'version.whisper',
|
||||||
//inputFormatter: utils.toDecimal
|
getter: 'shh_version',
|
||||||
//})
|
inputFormatter: utils.toDecimal
|
||||||
//];
|
})
|
||||||
|
];
|
||||||
//setupProperties(Web3.prototype, web3Properties);
|
|
||||||
|
|
||||||
//Web3.prototype.isConnected = function(){
|
|
||||||
//return (this.currentProvider && this.currentProvider.isConnected());
|
|
||||||
//};
|
|
||||||
|
|
||||||
Web3.prototype.createBatch = function () {
|
|
||||||
return new Batch();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Web3.prototype.isConnected = function(){
|
||||||
|
return (this.currentProvider && this.currentProvider.isConnected());
|
||||||
|
};
|
||||||
|
|
||||||
|
Web3.prototype.createBatch = function () {
|
||||||
|
return new Batch(this);
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = Web3;
|
module.exports = Web3;
|
||||||
|
|
||||||
|
|
|
@ -20,11 +20,11 @@
|
||||||
* @date 2015
|
* @date 2015
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var RequestManager = require('./requestmanager');
|
|
||||||
var Jsonrpc = require('./jsonrpc');
|
var Jsonrpc = require('./jsonrpc');
|
||||||
var errors = require('./errors');
|
var errors = require('./errors');
|
||||||
|
|
||||||
var Batch = function () {
|
var Batch = function (web3) {
|
||||||
|
this.requestManager = web3._requestManager;
|
||||||
this.requests = [];
|
this.requests = [];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ Batch.prototype.add = function (request) {
|
||||||
*/
|
*/
|
||||||
Batch.prototype.execute = function () {
|
Batch.prototype.execute = function () {
|
||||||
var requests = this.requests;
|
var requests = this.requests;
|
||||||
RequestManager.getInstance().sendBatch(requests, function (err, results) {
|
this.requestManager.sendBatch(requests, function (err, results) {
|
||||||
results = results || [];
|
results = results || [];
|
||||||
requests.map(function (request, index) {
|
requests.map(function (request, index) {
|
||||||
return results[index] || {};
|
return results[index] || {};
|
||||||
|
|
|
@ -135,7 +135,7 @@ Method.prototype.attachToObject = function (obj) {
|
||||||
|
|
||||||
Method.prototype.buildCall = function() {
|
Method.prototype.buildCall = function() {
|
||||||
var method = this;
|
var method = this;
|
||||||
return function send() {
|
var send = function () {
|
||||||
var payload = method.toPayload(Array.prototype.slice.call(arguments));
|
var payload = method.toPayload(Array.prototype.slice.call(arguments));
|
||||||
if (payload.callback) {
|
if (payload.callback) {
|
||||||
return method.requestManager.sendAsync(payload, function (err, result) {
|
return method.requestManager.sendAsync(payload, function (err, result) {
|
||||||
|
@ -144,6 +144,22 @@ Method.prototype.buildCall = function() {
|
||||||
}
|
}
|
||||||
return method.formatOutput(method.requestManager.send(payload));
|
return method.formatOutput(method.requestManager.send(payload));
|
||||||
};
|
};
|
||||||
|
send.request = this.request.bind(this);
|
||||||
|
return send;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should be called to create pure JSONRPC request which can be used in batch request
|
||||||
|
*
|
||||||
|
* @method request
|
||||||
|
* @param {...} params
|
||||||
|
* @return {Object} jsonrpc request
|
||||||
|
*/
|
||||||
|
Method.prototype.request = function () {
|
||||||
|
var payload = this.toPayload(Array.prototype.slice.call(arguments));
|
||||||
|
payload.format = this.formatOutput.bind(this);
|
||||||
|
return payload;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = Method;
|
module.exports = Method;
|
||||||
|
|
||||||
|
|
|
@ -111,13 +111,33 @@ Property.prototype.buildGet = function () {
|
||||||
|
|
||||||
Property.prototype.buildAsyncGet = function () {
|
Property.prototype.buildAsyncGet = function () {
|
||||||
var property = this;
|
var property = this;
|
||||||
return function get(callback) {
|
var get = function (callback) {
|
||||||
property.requestManager.sendAsync({
|
property.requestManager.sendAsync({
|
||||||
method: property.getter
|
method: property.getter
|
||||||
}, function (err, result) {
|
}, function (err, result) {
|
||||||
callback(err, property.formatOutput(result));
|
callback(err, property.formatOutput(result));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
get.request = this.request.bind(this);
|
||||||
|
return get;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should be called to create pure JSONRPC request which can be used in batch request
|
||||||
|
*
|
||||||
|
* @method request
|
||||||
|
* @param {...} params
|
||||||
|
* @return {Object} jsonrpc request
|
||||||
|
*/
|
||||||
|
Property.prototype.request = function () {
|
||||||
|
var payload = {
|
||||||
|
method: this.getter,
|
||||||
|
params: [],
|
||||||
|
callback: this.extractCallback(Array.prototype.slice.call(arguments))
|
||||||
|
};
|
||||||
|
payload.format = this.formatOutput.bind(this);
|
||||||
|
return payload;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = Property;
|
module.exports = Property;
|
||||||
|
|
||||||
|
|
|
@ -36,26 +36,12 @@ var errors = require('./errors');
|
||||||
* Singleton
|
* Singleton
|
||||||
*/
|
*/
|
||||||
var RequestManager = function (provider) {
|
var RequestManager = function (provider) {
|
||||||
// singleton pattern
|
|
||||||
//if (arguments.callee._singletonInstance) {
|
|
||||||
//return arguments.callee._singletonInstance;
|
|
||||||
//}
|
|
||||||
//arguments.callee._singletonInstance = this;
|
|
||||||
|
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
this.polls = {};
|
this.polls = {};
|
||||||
this.timeout = null;
|
this.timeout = null;
|
||||||
this.isPolling = false;
|
this.isPolling = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* @return {RequestManager} singleton
|
|
||||||
*/
|
|
||||||
RequestManager.getInstance = function () {
|
|
||||||
var instance = new RequestManager();
|
|
||||||
return instance;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should be used to synchronously send request
|
* Should be used to synchronously send request
|
||||||
*
|
*
|
||||||
|
|
|
@ -5,7 +5,6 @@ var web3 = new Web3();
|
||||||
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
|
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
|
||||||
var bn = require('bignumber.js');
|
var bn = require('bignumber.js');
|
||||||
|
|
||||||
/*
|
|
||||||
describe('lib/web3/batch', function () {
|
describe('lib/web3/batch', function () {
|
||||||
describe('execute', function () {
|
describe('execute', function () {
|
||||||
it('should execute batch request', function (done) {
|
it('should execute batch request', function (done) {
|
||||||
|
@ -201,4 +200,3 @@ describe('lib/web3/batch', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
|
@ -10,8 +10,7 @@ describe('lib/web3/requestmanager', function () {
|
||||||
describe('send', function () {
|
describe('send', function () {
|
||||||
it('should return expected result synchronously', function () {
|
it('should return expected result synchronously', function () {
|
||||||
var provider = new FakeHttpProvider();
|
var provider = new FakeHttpProvider();
|
||||||
var manager = RequestManager.getInstance();
|
var manager = new RequestManager(provider);
|
||||||
manager.setProvider(provider);
|
|
||||||
var expected = 'hello_world';
|
var expected = 'hello_world';
|
||||||
provider.injectResult(expected);
|
provider.injectResult(expected);
|
||||||
|
|
||||||
|
@ -25,8 +24,7 @@ describe('lib/web3/requestmanager', function () {
|
||||||
|
|
||||||
it('should return expected result asynchronously', function (done) {
|
it('should return expected result asynchronously', function (done) {
|
||||||
var provider = new FakeHttpProvider();
|
var provider = new FakeHttpProvider();
|
||||||
var manager = RequestManager.getInstance();
|
var manager = new RequestManager(provider);
|
||||||
manager.setProvider(provider);
|
|
||||||
var expected = 'hello_world';
|
var expected = 'hello_world';
|
||||||
provider.injectResult(expected);
|
provider.injectResult(expected);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue