mirror of
https://github.com/status-im/web3.js.git
synced 2025-02-22 11:08:33 +00:00
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 extend = require('./web3/extend');
|
||||
var Batch = require('./web3/batch');
|
||||
var Contract = require('./web3/contract');
|
||||
var Property = require('./web3/property');
|
||||
|
||||
function Web3 (provider) {
|
||||
this._requestManager = new RequestManager(provider);
|
||||
@ -51,6 +51,9 @@ function Web3 (provider) {
|
||||
version: version.version
|
||||
};
|
||||
this._extend = extend(this);
|
||||
this._extend({
|
||||
properties: properties()
|
||||
});
|
||||
}
|
||||
|
||||
Web3.prototype.setProvider = function (provider) {
|
||||
@ -62,8 +65,6 @@ Web3.prototype.reset = function () {
|
||||
this.settings = new Settings();
|
||||
};
|
||||
|
||||
|
||||
|
||||
Web3.prototype.toHex = utils.toHex;
|
||||
Web3.prototype.toAscii = utils.toAscii;
|
||||
Web3.prototype.toUtf8 = utils.toUtf8;
|
||||
@ -86,38 +87,37 @@ Web3.prototype.fromICAP = function (icap) {
|
||||
return iban.address();
|
||||
};
|
||||
|
||||
//var web3Properties = [
|
||||
//new Property({
|
||||
//name: 'version.client',
|
||||
//getter: 'web3_clientVersion'
|
||||
//}),
|
||||
//new Property({
|
||||
//name: 'version.network',
|
||||
//getter: 'net_version',
|
||||
//inputFormatter: utils.toDecimal
|
||||
//}),
|
||||
//new Property({
|
||||
//name: 'version.ethereum',
|
||||
//getter: 'eth_protocolVersion',
|
||||
//inputFormatter: utils.toDecimal
|
||||
//}),
|
||||
//new Property({
|
||||
//name: 'version.whisper',
|
||||
//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();
|
||||
var properties = function () {
|
||||
return [
|
||||
new Property({
|
||||
name: 'version.client',
|
||||
getter: 'web3_clientVersion'
|
||||
}),
|
||||
new Property({
|
||||
name: 'version.network',
|
||||
getter: 'net_version',
|
||||
inputFormatter: utils.toDecimal
|
||||
}),
|
||||
new Property({
|
||||
name: 'version.ethereum',
|
||||
getter: 'eth_protocolVersion',
|
||||
inputFormatter: utils.toDecimal
|
||||
}),
|
||||
new Property({
|
||||
name: 'version.whisper',
|
||||
getter: 'shh_version',
|
||||
inputFormatter: utils.toDecimal
|
||||
})
|
||||
];
|
||||
};
|
||||
|
||||
Web3.prototype.isConnected = function(){
|
||||
return (this.currentProvider && this.currentProvider.isConnected());
|
||||
};
|
||||
|
||||
Web3.prototype.createBatch = function () {
|
||||
return new Batch(this);
|
||||
};
|
||||
|
||||
module.exports = Web3;
|
||||
|
||||
|
@ -20,11 +20,11 @@
|
||||
* @date 2015
|
||||
*/
|
||||
|
||||
var RequestManager = require('./requestmanager');
|
||||
var Jsonrpc = require('./jsonrpc');
|
||||
var errors = require('./errors');
|
||||
|
||||
var Batch = function () {
|
||||
var Batch = function (web3) {
|
||||
this.requestManager = web3._requestManager;
|
||||
this.requests = [];
|
||||
};
|
||||
|
||||
@ -45,7 +45,7 @@ Batch.prototype.add = function (request) {
|
||||
*/
|
||||
Batch.prototype.execute = function () {
|
||||
var requests = this.requests;
|
||||
RequestManager.getInstance().sendBatch(requests, function (err, results) {
|
||||
this.requestManager.sendBatch(requests, function (err, results) {
|
||||
results = results || [];
|
||||
requests.map(function (request, index) {
|
||||
return results[index] || {};
|
||||
|
@ -135,7 +135,7 @@ Method.prototype.attachToObject = function (obj) {
|
||||
|
||||
Method.prototype.buildCall = function() {
|
||||
var method = this;
|
||||
return function send() {
|
||||
var send = function () {
|
||||
var payload = method.toPayload(Array.prototype.slice.call(arguments));
|
||||
if (payload.callback) {
|
||||
return method.requestManager.sendAsync(payload, function (err, result) {
|
||||
@ -144,6 +144,22 @@ Method.prototype.buildCall = function() {
|
||||
}
|
||||
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;
|
||||
|
||||
|
@ -111,13 +111,33 @@ Property.prototype.buildGet = function () {
|
||||
|
||||
Property.prototype.buildAsyncGet = function () {
|
||||
var property = this;
|
||||
return function get(callback) {
|
||||
var get = function (callback) {
|
||||
property.requestManager.sendAsync({
|
||||
method: property.getter
|
||||
}, function (err, 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;
|
||||
|
||||
|
@ -36,26 +36,12 @@ var errors = require('./errors');
|
||||
* Singleton
|
||||
*/
|
||||
var RequestManager = function (provider) {
|
||||
// singleton pattern
|
||||
//if (arguments.callee._singletonInstance) {
|
||||
//return arguments.callee._singletonInstance;
|
||||
//}
|
||||
//arguments.callee._singletonInstance = this;
|
||||
|
||||
this.provider = provider;
|
||||
this.polls = {};
|
||||
this.timeout = null;
|
||||
this.isPolling = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* @return {RequestManager} singleton
|
||||
*/
|
||||
RequestManager.getInstance = function () {
|
||||
var instance = new RequestManager();
|
||||
return instance;
|
||||
};
|
||||
|
||||
/**
|
||||
* Should be used to synchronously send request
|
||||
*
|
||||
|
@ -5,7 +5,6 @@ var web3 = new Web3();
|
||||
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
|
||||
var bn = require('bignumber.js');
|
||||
|
||||
/*
|
||||
describe('lib/web3/batch', function () {
|
||||
describe('execute', function () {
|
||||
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 () {
|
||||
it('should return expected result synchronously', function () {
|
||||
var provider = new FakeHttpProvider();
|
||||
var manager = RequestManager.getInstance();
|
||||
manager.setProvider(provider);
|
||||
var manager = new RequestManager(provider);
|
||||
var expected = 'hello_world';
|
||||
provider.injectResult(expected);
|
||||
|
||||
@ -25,8 +24,7 @@ describe('lib/web3/requestmanager', function () {
|
||||
|
||||
it('should return expected result asynchronously', function (done) {
|
||||
var provider = new FakeHttpProvider();
|
||||
var manager = RequestManager.getInstance();
|
||||
manager.setProvider(provider);
|
||||
var manager = new RequestManager(provider);
|
||||
var expected = 'hello_world';
|
||||
provider.injectResult(expected);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user