add timeout to the providor

This commit is contained in:
Fabian Vogelsteller 2015-06-30 11:43:18 +02:00
parent bedacacf09
commit ccfae818e2
1 changed files with 28 additions and 1 deletions

View File

@ -25,6 +25,8 @@
var utils = require('../utils/utils');
var errors = require('./errors');
var errorTimeout = '{"jsonrpc": "2.0", "error": {"code": -32603, "message": "FRONTEND Request timed out for method \'__method__\'"}, "id": "__id__"}';
var IpcProvider = function (path, net) {
var _this = this;
@ -37,6 +39,12 @@ var IpcProvider = function (path, net) {
this.connection.on('error', function(e){
console.error('IPC Connection error', e);
_this._timeout();
});
this.connection.on('end', function(e){
console.error('IPC Connection ended', e);
_this._timeout();
});
@ -63,7 +71,6 @@ var IpcProvider = function (path, net) {
id = result.id;
}
// fire the callback
if(_this.responseCallbacks[id]) {
_this.responseCallbacks[id](null, result);
@ -81,11 +88,31 @@ which will be called if a response matching the response Id will arrive.
*/
IpcProvider.prototype._getResponse = function(payload, callback) {
var id = payload.id || payload[0].id;
var method = payload.method || payload[0].method;
this.responseCallbacks[id] = callback;
this.responseCallbacks[id].method = method;
};
/**
Timeout all requests when the end/error event is fired
@method _timeout
*/
IpcProvider.prototype._timeout = function() {
for(key in this.responseCallbacks) {
if(this.responseCallback.hasOwnProperty(key)){
this.responseCallbacks[key](errorTimeout.replace('__id__', key).replace('__method__', this.responseCallbacks[key].method));
}
}
};
/**
Check if the current connection is still valid.
@method isConnected
*/
IpcProvider.prototype.isConnected = function() {
// try reconnect, when connection is gone
if(!this.connection.writable)