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
parent 0623c87727
commit b97a332ae9
6 changed files with 49 additions and 26 deletions

View File

@ -128,6 +128,19 @@ input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill, inpu
-webkit-box-shadow: 0 0 0px 1000px white inset;
}
.status {
border: 1px solid #f0c36d;
background-color: #f9edbe;
position: absolute;
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
top: 10px;
width: 215px;
padding: 7px;
}
.join label,
.open label,
.setup label {

View File

@ -13,7 +13,7 @@
<div class="page">
<div class="off-canvas-wrap">
<div class="inner-wrap">
<span class="status" ng-if="$root.reconnecting">Attempting to reconnect...</span>
<nav class="tab-bar" ng-class="{'hide-tab-bar' : !$root.wallet ||
!$root.wallet.isReady() || $root.wallet.isLocked}">
<section class="left-small">

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;
});

View File

@ -36,7 +36,10 @@ var FakeWallet = function() {
return true;
}
};
this.blockchain = {subscribed: [], subscribe: function(){}};
this.blockchain = {
getSubscriptions: function(){ return []; },
subscribe: function(){}
};
this.privateKey = new FakePrivateKey();
};

View File

@ -77,9 +77,9 @@ describe('Insight model', function() {
blockchain.status.should.be.equal('disconnected');
blockchain.on('connect', function() {
blockchain.subscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
blockchain.subscribed.length.should.equal(1);
blockchain.getSubscriptions().length.should.equal(1);
blockchain.destroy();
blockchain.subscribed.length.should.equal(0);
blockchain.getSubscriptions().length.should.equal(0);
blockchain.status.should.be.equal('destroyed');
done();
});
@ -90,7 +90,7 @@ describe('Insight model', function() {
var emitSpy = sinon.spy(blockchain.socket, 'emit');
blockchain.subscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
blockchain.subscribed.length.should.equal(1);
blockchain.getSubscriptions().length.should.equal(1);
emitSpy.calledWith('subscribe', 'mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
});
@ -99,7 +99,7 @@ describe('Insight model', function() {
blockchain.subscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
blockchain.subscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
blockchain.subscribed.length.should.equal(1);
blockchain.getSubscriptions().length.should.equal(1);
});
it('should subscribe to a list of addresses', function() {
@ -110,7 +110,7 @@ describe('Insight model', function() {
'mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM',
'2NBBHBjB5sd7HFqKtout1L7d6dPhwJgP2j8'
]);
blockchain.subscribed.length.should.equal(2);
blockchain.getSubscriptions().length.should.equal(2);
emitSpy.calledWith('subscribe', 'mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
emitSpy.calledWith('subscribe', '2NBBHBjB5sd7HFqKtout1L7d6dPhwJgP2j8');
});
@ -118,19 +118,19 @@ describe('Insight model', function() {
it('should unsubscribe to an address', function() {
var blockchain = new Insight(FAKE_OPTS);
blockchain.subscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
blockchain.subscribed.length.should.equal(1);
blockchain.getSubscriptions().length.should.equal(1);
blockchain.unsubscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
blockchain.subscribed.length.should.equal(0);
blockchain.getSubscriptions().length.should.equal(0);
});
it('should unsubscribe to all addresses', function() {
var blockchain = new Insight(FAKE_OPTS);
blockchain.subscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
blockchain.subscribe('2NBBHBjB5sd7HFqKtout1L7d6dPhwJgP2j8');
blockchain.subscribed.length.should.equal(2);
blockchain.getSubscriptions().length.should.equal(2);
blockchain.unsubscribeAll('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
blockchain.subscribed.length.should.equal(0);
blockchain.getSubscriptions().length.should.equal(0);
});
it('should broadcast a raw transaction', function(done) {