Add reconnecting notification, change subscribed list to dict, update balance after reconnect

This commit is contained in:
Yemel Jardi 2014-08-29 15:01:07 -03:00
commit b97a332ae9
6 changed files with 49 additions and 26 deletions

View file

@ -29,7 +29,7 @@ var preconditions = require('preconditions').singleton();
var Insight = function (opts) {
this.status = this.STATUS.DISCONNECTED;
this.subscribed = [];
this.subscribed = {};
this.listeningBlocks = false;
preconditions.checkArgument(opts).shouldBeObject(opts)
@ -112,7 +112,7 @@ Insight.prototype.requestPost = function(path, data, cb) {
Insight.prototype.destroy = function() {
this.socket.destroy();
this.subscribed = [];
this.subscribed = {};
this.status = this.STATUS.DESTROYED;
this.removeAllListeners();
};
@ -124,7 +124,7 @@ Insight.prototype.subscribe = function(addresses) {
function handlerFor(self, address) {
return function (txid) {
// verify the address is still subscribed
if (self.subscribed.indexOf(address) == -1) return;
if (!self.subscribed[address]) return;
self.emit('tx', {address: address, txid: txid});
}
}
@ -133,14 +133,18 @@ Insight.prototype.subscribe = function(addresses) {
preconditions.checkArgument(new bitcore.Address(address).isValid());
// skip already subscibed
if (self.subscribed.indexOf(address) == -1) {
self.subscribed.push(address);
if (!self.subscribed[address]) {
self.subscribed[address] = true;
self.socket.emit('subscribe', address);
self.socket.on(address, handlerFor(self, address));
}
});
};
Insight.prototype.getSubscriptions = function(addresses) {
return Object.keys(this.subscribed);
}
Insight.prototype.unsubscribe = function(addresses) {
addresses = Array.isArray(addresses) ? addresses : [addresses];
var self = this;
@ -148,15 +152,12 @@ Insight.prototype.unsubscribe = function(addresses) {
addresses.forEach(function(address) {
preconditions.checkArgument(new bitcore.Address(address).isValid());
self.socket.removeEventListener(address);
});
this.subscribed = this.subscribed.filter(function(a) {
return addresses.indexOf(a) == -1;
delete self.subscribed[address];
});
};
Insight.prototype.unsubscribeAll = function() {
this.unsubscribe(this.subscribed);
this.unsubscribe(this.getSubscriptions());
};
Insight.prototype.broadcast = function(rawtx, cb) {

View file

@ -66,6 +66,7 @@ angular.module('copayApp.services')
uriHandler.register();
$rootScope.unitName = config.unitName;
$rootScope.txAlertCount = 0;
$rootScope.reconnecting = false;
$rootScope.isCollapsed = true;
$rootScope.$watch('txAlertCount', function(txAlertCount) {
if (txAlertCount && txAlertCount > 0) {
@ -293,10 +294,16 @@ angular.module('copayApp.services')
wallet.blockchain.on('connect', function(attempts) {
if (attempts == 0) return;
notification.success('Networking restored', 'Connection to Insight re-established');
$rootScope.reconnecting = false;
root.updateBalance(function() {
$rootScope.$digest();
});
});
wallet.blockchain.on('disconnect', function() {
notification.error('Networking problem', 'Connection to Insight lost, trying to reconnect...');
$rootScope.reconnecting = true;
$rootScope.$digest();
});
wallet.blockchain.on('tx', function(tx) {
@ -319,18 +326,17 @@ angular.module('copayApp.services')
if (!$rootScope.wallet) return;
root.updateAddressList();
var currentAddrs = $rootScope.wallet.blockchain.subscribed;
var currentAddrs = $rootScope.wallet.blockchain.getSubscriptions();
var allAddrs = $rootScope.addrInfos;
var newAddrs = [];
for (var i in allAddrs) {
var a = allAddrs[i];
if (!currentAddrs[a.addressStr] && !a.isChange)
newAddrs.push(a);
}
for (var i = 0; i < newAddrs.length; i++) {
$rootScope.wallet.blockchain.subscribe(newAddrs[i].addressStr);
newAddrs.push(a.addressStr);
}
$rootScope.wallet.blockchain.subscribe(newAddrs);
};
return root;
});