mirror of https://github.com/status-im/web3.js.git
add timeout to the providor
This commit is contained in:
parent
bedacacf09
commit
ccfae818e2
|
@ -25,6 +25,8 @@
|
||||||
var utils = require('../utils/utils');
|
var utils = require('../utils/utils');
|
||||||
var errors = require('./errors');
|
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 IpcProvider = function (path, net) {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
|
@ -37,6 +39,12 @@ var IpcProvider = function (path, net) {
|
||||||
|
|
||||||
this.connection.on('error', function(e){
|
this.connection.on('error', function(e){
|
||||||
console.error('IPC Connection error', 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;
|
id = result.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// fire the callback
|
// fire the callback
|
||||||
if(_this.responseCallbacks[id]) {
|
if(_this.responseCallbacks[id]) {
|
||||||
_this.responseCallbacks[id](null, result);
|
_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) {
|
IpcProvider.prototype._getResponse = function(payload, callback) {
|
||||||
var id = payload.id || payload[0].id;
|
var id = payload.id || payload[0].id;
|
||||||
|
var method = payload.method || payload[0].method;
|
||||||
|
|
||||||
this.responseCallbacks[id] = callback;
|
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() {
|
IpcProvider.prototype.isConnected = function() {
|
||||||
// try reconnect, when connection is gone
|
// try reconnect, when connection is gone
|
||||||
if(!this.connection.writable)
|
if(!this.connection.writable)
|
||||||
|
|
Loading…
Reference in New Issue