web3.js/lib/requestmanager.js

137 lines
4.1 KiB
JavaScript
Raw Normal View History

2015-02-06 00:02:14 +01:00
/*
This file is part of ethereum.js.
ethereum.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethereum.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file requestmanager.js
* @authors:
* Jeffrey Wilcke <jeff@ethdev.com>
* Marek Kotewicz <marek@ethdev.com>
* Marian Oancea <marian@ethdev.com>
* Gav Wood <g@ethdev.com>
* @date 2014
*/
var jsonrpc = require('./jsonrpc');
2015-02-06 10:20:41 +01:00
var c = require('./const');
2015-02-06 00:02:14 +01:00
/**
* It's responsible for passing messages to providers
* It's also responsible for polling the ethereum node for incoming messages
* Default poll timeout is 1 second
*/
var requestManager = function() {
var polls = [];
var provider;
2015-03-02 13:00:20 +01:00
var send = function (data, callback) {
/*jshint maxcomplexity: 7 */
2015-02-24 13:00:24 +01:00
// format the input before sending
if(typeof data.inputFormatter === 'function') {
data.params = Array.prototype.map.call(data.params, function(item){
return data.inputFormatter(item);
});
}
2015-02-06 00:02:14 +01:00
var payload = jsonrpc.toPayload(data.method, data.params);
if (!provider) {
console.error('provider is not set');
return null;
}
2015-03-02 13:00:20 +01:00
// ASYNC (only when callback is given, and it a HttpProvidor)
if(typeof callback === 'function' && provider.host){
provider.send(payload, function(result){
2015-02-06 00:02:14 +01:00
2015-03-02 13:00:20 +01:00
if (!jsonrpc.isValidResponse(result)) {
console.log(result);
if(typeof result === 'object' && result.error && result.error.message)
console.error(result.error.message);
return null;
}
// format the output
callback((typeof data.outputFormatter === 'function') ? data.outputFormatter(result.result) : result.result);
});
// SYNC
} else {
var result = provider.send(payload);
if (!jsonrpc.isValidResponse(result)) {
console.log(result);
if(typeof result === 'object' && result.error && result.error.message)
console.error(result.error.message);
return null;
}
// format the output
return (typeof data.outputFormatter === 'function') ? data.outputFormatter(result.result) : result.result;
2015-02-06 00:02:14 +01:00
}
};
var setProvider = function (p) {
provider = p;
};
2015-02-25 15:53:28 +01:00
/*jshint maxparams:4 */
2015-02-06 10:39:06 +01:00
var startPolling = function (data, pollId, callback, uninstall) {
polls.push({data: data, id: pollId, callback: callback, uninstall: uninstall});
2015-02-06 00:02:14 +01:00
};
2015-02-25 15:53:28 +01:00
/*jshint maxparams:3 */
2015-02-06 00:02:14 +01:00
var stopPolling = function (pollId) {
for (var i = polls.length; i--;) {
var poll = polls[i];
if (poll.id === pollId) {
polls.splice(i, 1);
}
}
};
2015-02-06 10:39:06 +01:00
var reset = function () {
polls.forEach(function (poll) {
poll.uninstall(poll.id);
});
polls = [];
};
2015-02-06 00:02:14 +01:00
var poll = function () {
polls.forEach(function (data) {
var result = send(data.data);
if (!(result instanceof Array) || result.length === 0) {
return;
}
data.callback(result);
});
2015-02-06 10:20:41 +01:00
setTimeout(poll, c.ETH_POLLING_TIMEOUT);
2015-02-06 00:02:14 +01:00
};
poll();
return {
send: send,
setProvider: setProvider,
startPolling: startPolling,
2015-02-06 10:39:06 +01:00
stopPolling: stopPolling,
reset: reset
2015-02-06 00:02:14 +01:00
};
};
module.exports = requestManager;