simplified polling && jsonrpc payload creation

This commit is contained in:
Marek Kotewicz 2015-02-03 21:43:39 +01:00
parent ddc17196da
commit f3ce1f07c4
7 changed files with 36 additions and 90 deletions

57
dist/ethereum.js vendored
View File

@ -683,7 +683,7 @@ var Filter = function(options, impl) {
this.callbacks = []; this.callbacks = [];
this.id = impl.newFilter(options); this.id = impl.newFilter(options);
web3.provider.startPolling({call: impl.changed, args: [this.id]}, this.id, this.trigger.bind(this)); web3.provider.startPolling({method: impl.changed, params: [this.id]}, this.id, this.trigger.bind(this));
}; };
/// alias for changed* /// alias for changed*
@ -916,37 +916,12 @@ var HttpSyncProvider = function (host) {
this.host = host || 'http://localhost:8080'; this.host = host || 'http://localhost:8080';
}; };
/// Transforms inner message to proper jsonrpc object
/// @param inner message object
/// @returns jsonrpc object
function formatJsonRpcObject(object) {
return {
jsonrpc: '2.0',
method: object.call,
params: object.args,
id: object._id
};
}
/// Transforms jsonrpc object to inner message
/// @param incoming jsonrpc message
/// @returns inner message object
function formatJsonRpcMessage(message) {
var object = JSON.parse(message);
return {
_id: object.id,
data: object.result,
error: object.error
};
}
HttpSyncProvider.prototype.send = function (payload) { HttpSyncProvider.prototype.send = function (payload) {
var data = formatJsonRpcObject(payload); //var data = formatJsonRpcObject(payload);
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
request.open('POST', this.host, false); request.open('POST', this.host, false);
request.send(JSON.stringify(data)); request.send(JSON.stringify(payload));
// check request.status // check request.status
return request.responseText; return request.responseText;
@ -1001,18 +976,14 @@ var ProviderManager = function() {
var poll = function () { var poll = function () {
if (self.provider) { if (self.provider) {
self.polls.forEach(function (data) { self.polls.forEach(function (data) {
data.data._id = self.id; var result = self.send(data.data);
self.id++;
var result = self.provider.send(data.data);
result = JSON.parse(result);
// dont call the callback if result is not an array, or empty one // dont call the callback if result is not an array, or empty one
if (result.error || !(result.result instanceof Array) || result.result.length === 0) { if (!(result instanceof Array) || result.length === 0) {
return; return;
} }
data.callback(result.result); data.callback(result);
}); });
} }
setTimeout(poll, 1000); setTimeout(poll, 1000);
@ -1021,10 +992,12 @@ var ProviderManager = function() {
}; };
/// sends outgoing requests /// sends outgoing requests
/// @params data - an object with at least 'method' property
ProviderManager.prototype.send = function(data) { ProviderManager.prototype.send = function(data) {
data.args = data.args || []; data.jsonrpc = '2.0';
data._id = this.id++; data.params = data.params || [];
data.id = this.id++;
if (this.provider === undefined) { if (this.provider === undefined) {
console.error('provider is not set'); console.error('provider is not set');
@ -1465,8 +1438,8 @@ var setupMethods = function (obj, methods) {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
var call = typeof method.call === 'function' ? method.call(args) : method.call; var call = typeof method.call === 'function' ? method.call(args) : method.call;
return web3.provider.send({ return web3.provider.send({
call: call, method: call,
args: args params: args
}); });
}; };
}); });
@ -1479,15 +1452,15 @@ var setupProperties = function (obj, properties) {
var proto = {}; var proto = {};
proto.get = function () { proto.get = function () {
return web3.provider.send({ return web3.provider.send({
call: property.getter method: property.getter
}); });
}; };
if (property.setter) { if (property.setter) {
proto.set = function (val) { proto.set = function (val) {
return web3.provider.send({ return web3.provider.send({
call: property.setter, method: property.setter,
args: [val] params: [val]
}); });
}; };
} }

10
dist/ethereum.js.map vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -56,7 +56,7 @@ var Filter = function(options, impl) {
this.callbacks = []; this.callbacks = [];
this.id = impl.newFilter(options); this.id = impl.newFilter(options);
web3.provider.startPolling({call: impl.changed, args: [this.id]}, this.id, this.trigger.bind(this)); web3.provider.startPolling({method: impl.changed, params: [this.id]}, this.id, this.trigger.bind(this));
}; };
/// alias for changed* /// alias for changed*

View File

@ -30,37 +30,12 @@ var HttpSyncProvider = function (host) {
this.host = host || 'http://localhost:8080'; this.host = host || 'http://localhost:8080';
}; };
/// Transforms inner message to proper jsonrpc object
/// @param inner message object
/// @returns jsonrpc object
function formatJsonRpcObject(object) {
return {
jsonrpc: '2.0',
method: object.call,
params: object.args,
id: object._id
};
}
/// Transforms jsonrpc object to inner message
/// @param incoming jsonrpc message
/// @returns inner message object
function formatJsonRpcMessage(message) {
var object = JSON.parse(message);
return {
_id: object.id,
data: object.result,
error: object.error
};
}
HttpSyncProvider.prototype.send = function (payload) { HttpSyncProvider.prototype.send = function (payload) {
var data = formatJsonRpcObject(payload); //var data = formatJsonRpcObject(payload);
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
request.open('POST', this.host, false); request.open('POST', this.host, false);
request.send(JSON.stringify(data)); request.send(JSON.stringify(payload));
// check request.status // check request.status
return request.responseText; return request.responseText;

View File

@ -43,18 +43,14 @@ var ProviderManager = function() {
var poll = function () { var poll = function () {
if (self.provider) { if (self.provider) {
self.polls.forEach(function (data) { self.polls.forEach(function (data) {
data.data._id = self.id; var result = self.send(data.data);
self.id++;
var result = self.provider.send(data.data);
result = JSON.parse(result);
// dont call the callback if result is not an array, or empty one // dont call the callback if result is not an array, or empty one
if (result.error || !(result.result instanceof Array) || result.result.length === 0) { if (!(result instanceof Array) || result.length === 0) {
return; return;
} }
data.callback(result.result); data.callback(result);
}); });
} }
setTimeout(poll, 1000); setTimeout(poll, 1000);
@ -63,10 +59,12 @@ var ProviderManager = function() {
}; };
/// sends outgoing requests /// sends outgoing requests
/// @params data - an object with at least 'method' property
ProviderManager.prototype.send = function(data) { ProviderManager.prototype.send = function(data) {
data.args = data.args || []; data.jsonrpc = '2.0';
data._id = this.id++; data.params = data.params || [];
data.id = this.id++;
if (this.provider === undefined) { if (this.provider === undefined) {
console.error('provider is not set'); console.error('provider is not set');

View File

@ -136,8 +136,8 @@ var setupMethods = function (obj, methods) {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
var call = typeof method.call === 'function' ? method.call(args) : method.call; var call = typeof method.call === 'function' ? method.call(args) : method.call;
return web3.provider.send({ return web3.provider.send({
call: call, method: call,
args: args params: args
}); });
}; };
}); });
@ -150,15 +150,15 @@ var setupProperties = function (obj, properties) {
var proto = {}; var proto = {};
proto.get = function () { proto.get = function () {
return web3.provider.send({ return web3.provider.send({
call: property.getter method: property.getter
}); });
}; };
if (property.setter) { if (property.setter) {
proto.set = function (val) { proto.set = function (val) {
return web3.provider.send({ return web3.provider.send({
call: property.setter, method: property.setter,
args: [val] params: [val]
}); });
}; };
} }