comments in requestmanager

This commit is contained in:
Marek Kotewicz 2015-03-21 23:10:34 +01:00
parent 5c1e85f295
commit d408fb0867
1 changed files with 113 additions and 74 deletions

View File

@ -34,93 +34,132 @@ var InvalidResponse = new Error('jsonrpc response is not valid');
* It's also responsible for polling the ethereum node for incoming messages
* Default poll timeout is 1 second
*/
var requestManager = function() {
var polls = [];
var timeout = null;
var provider;
var RequestManager = function() {
this.polls = [];
this.timeout = null;
this.provider;
var send = function (data, callback) {
this.poll();
};
var payload = utils.isArray(data) ? jsonrpc.toBatchPayload(data) : jsonrpc.toPayload(data.method, data.params);
var result = provider.send(payload);
/**
* Should be used to synchronously send request
*
* @method send
* @param {Object|Array} data
* @return {Object}
*/
RequestManager.prototype.send = function (data) {
var payload = utils.isArray(data) ? jsonrpc.toBatchPayload(data) : jsonrpc.toPayload(data.method, data.params);
var result = this.provider.send(payload);
if (!jsonrpc.isValidResponse(result)) {
throw InvalidResponse;
}
return result.result;
};
/**
* Should be used to asynchronously send request
*
* @method sendAsync
* @param {Object|Array} data
* @param {Function} callback
*/
RequestManager.prototype.sendAsync = function (data, callback) {
var payload = utils.isArray(data) ? jsonrpc.toBatchPayload(data) : jsonrpc.toPayload(data.method, data.params);
this.provider.sendAsync(payload, function (err, result) {
if (err) {
return callback(err);
}
if (!jsonrpc.isValidResponse(result)) {
throw InvalidResponse;
return callback(InvalidResponse);
}
return result.result;
};
callback(null, result.result);
});
};
var sendAsync = function (data, callback) {
var payload = utils.isArray(data) ? jsonrpc.toBatchPayload(data) : jsonrpc.toPayload(data.method, data.params);
provider.sendAsync(payload, function (err, result) {
if (err) {
return callback(err);
}
/**
* Should be used to set provider of request manager
*
* @method setProvider
* @param {Object}
*/
RequestManager.prototype.setProvider = function (p) {
this.provider = p;
};
if (!jsonrpc.isValidResponse(result)) {
return callback(InvalidResponse);
}
/*jshint maxparams:4 */
callback(null, result.result);
});
};
/**
* Should be used to start polling
*
* @method startPolling
* @param data
* @param pollId
* @param callback
* @param uninstall
*
* @todo cleanup number of params
*/
RequestManager.prototype.startPolling = function (data, pollId, callback, uninstall) {
this.polls.push({data: data, id: pollId, callback: callback, uninstall: uninstall});
};
/*jshint maxparams:3 */
var setProvider = function (p) {
provider = p;
};
/*jshint maxparams:4 */
var startPolling = function (data, pollId, callback, uninstall) {
polls.push({data: data, id: pollId, callback: callback, uninstall: uninstall});
};
/*jshint maxparams:3 */
var stopPolling = function (pollId) {
for (var i = polls.length; i--;) {
var poll = polls[i];
if (poll.id === pollId) {
polls.splice(i, 1);
}
/**
* Should be used to stop polling for filter with given id
*
* @method stopPolling
* @param pollId
*/
RequestManager.prototype.stopPolling = function (pollId) {
for (var i = this.polls.length; i--;) {
var poll = this.polls[i];
if (poll.id === pollId) {
this.polls.splice(i, 1);
}
};
}
};
var reset = function () {
polls.forEach(function (poll) {
poll.uninstall(poll.id);
/**
* Should be called to reset polling mechanism of request manager
*
* @method reset
*/
RequestManager.prototype.reset = function () {
this.polls.forEach(function (poll) {
poll.uninstall(poll.id);
});
this.polls = [];
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
this.poll();
};
/**
* Should be called to poll for changes on filter with given id
*
* @method poll
*/
RequestManager.prototype.poll = function () {
this.polls.forEach(function (data) {
// send async
sendAsync(data.data, function(error, result){
if (error || !(isArray(result)) || result.length === 0) {
return;
}
data.callback(result);
});
polls = [];
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
poll();
};
var poll = function () {
polls.forEach(function (data) {
// send async
sendAsync(data.data, function(error, result){
if (error || !(isArray(result)) || result.length === 0) {
return;
}
data.callback(result);
});
});
timeout = setTimeout(poll, c.ETH_POLLING_TIMEOUT);
};
poll();
return {
send: send,
setProvider: setProvider,
startPolling: startPolling,
stopPolling: stopPolling,
reset: reset
};
});
timeout = setTimeout(poll, c.ETH_POLLING_TIMEOUT);
};
module.exports = requestManager;