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 * It's also responsible for polling the ethereum node for incoming messages
* Default poll timeout is 1 second * Default poll timeout is 1 second
*/ */
var requestManager = function() { var RequestManager = function() {
var polls = []; this.polls = [];
var timeout = null; this.timeout = null;
var provider; this.provider;
this.poll();
};
var send = function (data, callback) { /**
* 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);
var payload = utils.isArray(data) ? jsonrpc.toBatchPayload(data) : jsonrpc.toPayload(data.method, data.params); if (!jsonrpc.isValidResponse(result)) {
var result = provider.send(payload); 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)) { 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); * Should be used to set provider of request manager
provider.sendAsync(payload, function (err, result) { *
if (err) { * @method setProvider
return callback(err); * @param {Object}
} */
RequestManager.prototype.setProvider = function (p) {
if (!jsonrpc.isValidResponse(result)) { this.provider = p;
return callback(InvalidResponse); };
}
callback(null, result.result); /*jshint maxparams:4 */
});
};
var setProvider = function (p) { /**
provider = p; * 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 */
/*jshint maxparams:4 */ /**
var startPolling = function (data, pollId, callback, uninstall) { * Should be used to stop polling for filter with given id
polls.push({data: data, id: pollId, callback: callback, uninstall: uninstall}); *
}; * @method stopPolling
/*jshint maxparams:3 */ * @param pollId
*/
var stopPolling = function (pollId) { RequestManager.prototype.stopPolling = function (pollId) {
for (var i = polls.length; i--;) { for (var i = this.polls.length; i--;) {
var poll = polls[i]; var poll = this.polls[i];
if (poll.id === pollId) { if (poll.id === pollId) {
polls.splice(i, 1); this.polls.splice(i, 1);
}
} }
}; }
};
var reset = function () { /**
polls.forEach(function (poll) { * Should be called to reset polling mechanism of request manager
poll.uninstall(poll.id); *
* @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 = []; });
timeout = setTimeout(poll, c.ETH_POLLING_TIMEOUT);
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
};
}; };
module.exports = requestManager; module.exports = requestManager;