Refactor controllers
This commit is contained in:
parent
33801e9587
commit
16091bd330
9 changed files with 50 additions and 47 deletions
|
|
@ -9,8 +9,7 @@ angular.module('copayApp.controllers').controller('AddressesController',
|
||||||
$scope.loading = true;
|
$scope.loading = true;
|
||||||
w.generateAddress(null, function() {
|
w.generateAddress(null, function() {
|
||||||
$timeout(function() {
|
$timeout(function() {
|
||||||
controllerUtils.setSocketHandlers();
|
controllerUtils.updateGlobalAddresses();
|
||||||
controllerUtils.updateAddressList();
|
|
||||||
$scope.loading = false;
|
$scope.loading = false;
|
||||||
}, 1);
|
}, 1);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -57,9 +57,6 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
|
||||||
return new Array(num);
|
return new Array(num);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init socket handlers (with no wallet yet)
|
|
||||||
controllerUtils.setSocketHandlers();
|
|
||||||
|
|
||||||
if ($rootScope.wallet) {
|
if ($rootScope.wallet) {
|
||||||
$scope.$on('$idleWarn', function(a,countdown) {
|
$scope.$on('$idleWarn', function(a,countdown) {
|
||||||
if (!(countdown%5))
|
if (!(countdown%5))
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ var Insight = function (opts) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.socket.on('connect', function() {
|
this.socket.on('connect', function() {
|
||||||
self.status = self.STATUS.CONNECTED;
|
self.status = self.STATUS.CONNECTED;
|
||||||
self.suscribeToBlocks();
|
self.subscribeToBlocks();
|
||||||
self.emit('connect', 0);
|
self.emit('connect', 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -82,7 +82,7 @@ Insight.prototype.STATUS = {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @private */
|
/** @private */
|
||||||
Insight.prototype.suscribeToBlocks = function() {
|
Insight.prototype.subscribeToBlocks = function() {
|
||||||
if (this.listeningBlocks || !this.socket.connected) return;
|
if (this.listeningBlocks || !this.socket.connected) return;
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
@ -132,9 +132,12 @@ Insight.prototype.subscribe = function(addresses) {
|
||||||
addresses.forEach(function(address) {
|
addresses.forEach(function(address) {
|
||||||
preconditions.checkArgument(new bitcore.Address(address).isValid());
|
preconditions.checkArgument(new bitcore.Address(address).isValid());
|
||||||
|
|
||||||
self.subscribed.push(address);
|
// skip already subscibed
|
||||||
self.socket.emit('subscribe', address);
|
if (self.subscribed.indexOf(address) == -1) {
|
||||||
self.socket.on(address, handlerFor(self, address));
|
self.subscribed.push(address);
|
||||||
|
self.socket.emit('subscribe', address);
|
||||||
|
self.socket.on(address, handlerFor(self, address));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -805,8 +805,6 @@ Wallet.prototype.sendTx = function(ntxid, cb) {
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
this.blockchain.broadcast(txHex, function(err, txid) {
|
this.blockchain.broadcast(txHex, function(err, txid) {
|
||||||
if(err) throw err;
|
|
||||||
|
|
||||||
self.log('BITCOIND txid:', txid);
|
self.log('BITCOIND txid:', txid);
|
||||||
if (txid) {
|
if (txid) {
|
||||||
self.txProposals.get(ntxid).setSent(txid);
|
self.txProposals.get(ntxid).setSent(txid);
|
||||||
|
|
|
||||||
|
|
@ -99,7 +99,7 @@ angular.module('copayApp.services')
|
||||||
root.startNetwork = function(w, $scope) {
|
root.startNetwork = function(w, $scope) {
|
||||||
root.setupRootVariables();
|
root.setupRootVariables();
|
||||||
root.installStartupHandlers(w, $scope);
|
root.installStartupHandlers(w, $scope);
|
||||||
root.setSocketHandlers();
|
root.updateGlobalAddresses();
|
||||||
|
|
||||||
var handlePeerVideo = function(err, peerID, url) {
|
var handlePeerVideo = function(err, peerID, url) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
@ -120,7 +120,7 @@ angular.module('copayApp.services')
|
||||||
});
|
});
|
||||||
w.on('ready', function(myPeerID) {
|
w.on('ready', function(myPeerID) {
|
||||||
$rootScope.wallet = w;
|
$rootScope.wallet = w;
|
||||||
|
root.setConnectionListeners();
|
||||||
|
|
||||||
if ($rootScope.pendingPayment) {
|
if ($rootScope.pendingPayment) {
|
||||||
$location.path('send');
|
$location.path('send');
|
||||||
|
|
@ -132,7 +132,7 @@ angular.module('copayApp.services')
|
||||||
});
|
});
|
||||||
|
|
||||||
w.on('publicKeyRingUpdated', function(dontDigest) {
|
w.on('publicKeyRingUpdated', function(dontDigest) {
|
||||||
root.setSocketHandlers();
|
root.updateGlobalAddresses();
|
||||||
if (!dontDigest) {
|
if (!dontDigest) {
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
}
|
}
|
||||||
|
|
@ -304,44 +304,38 @@ angular.module('copayApp.services')
|
||||||
wallet.blockchain.on('disconnect', function() {
|
wallet.blockchain.on('disconnect', function() {
|
||||||
notification.error('Networking problem', 'Connection to Insight lost, trying to reconnect...');
|
notification.error('Networking problem', 'Connection to Insight lost, trying to reconnect...');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
wallet.blockchain.on('tx', function(tx) {
|
||||||
|
notification.funds('Funds received!', tx.address);
|
||||||
|
root.updateBalance(function() {
|
||||||
|
$rootScope.$digest();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!$rootScope.wallet.spendUnconfirmed) {
|
||||||
|
wallet.blockchain.on('block', function(block) {
|
||||||
|
root.updateBalance(function() {
|
||||||
|
$rootScope.$digest();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
root.setSocketHandlers = function() {
|
root.updateGlobalAddresses = function() {
|
||||||
root.updateAddressList();
|
|
||||||
|
|
||||||
if (!$rootScope.wallet) return;
|
if (!$rootScope.wallet) return;
|
||||||
|
|
||||||
var currentAddrs = Socket.getListeners();
|
root.updateAddressList();
|
||||||
|
var currentAddrs = $rootScope.wallet.blockchain.getListeners();
|
||||||
var allAddrs = $rootScope.addrInfos;
|
var allAddrs = $rootScope.addrInfos;
|
||||||
|
|
||||||
var newAddrs = [];
|
var newAddrs = [];
|
||||||
for (var i in allAddrs) {
|
for (var i in allAddrs) {
|
||||||
var a = allAddrs[i];
|
var a = allAddrs[i];
|
||||||
if (!currentAddrs[a.addressStr])
|
if (!currentAddrs[a.addressStr] && !a.isChange)
|
||||||
newAddrs.push(a);
|
newAddrs.push(a);
|
||||||
}
|
}
|
||||||
for (var i = 0; i < newAddrs.length; i++) {
|
for (var i = 0; i < newAddrs.length; i++) {
|
||||||
Socket.emit('subscribe', newAddrs[i].addressStr);
|
$rootScope.wallet.blockchain.subscribe(newAddrs[i].addressStr);
|
||||||
}
|
|
||||||
newAddrs.forEach(function(a) {
|
|
||||||
Socket.on(a.addressStr, function(txid) {
|
|
||||||
|
|
||||||
if (!a.isChange)
|
|
||||||
notification.funds('Funds received!', a.addressStr);
|
|
||||||
|
|
||||||
root.updateBalance(function() {
|
|
||||||
$rootScope.$digest();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!$rootScope.wallet.spendUnconfirmed && !Socket.isListeningBlocks()) {
|
|
||||||
Socket.emit('subscribe', 'inv');
|
|
||||||
Socket.on('block', function(block) {
|
|
||||||
root.updateBalance(function() {
|
|
||||||
$rootScope.$digest();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return root;
|
return root;
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,10 @@ function FakeBlockchain(opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FakeBlockchain.prototype.getTransaction = function(txid, cb) {
|
||||||
|
return cb(null, {txid: txid});
|
||||||
|
};
|
||||||
|
|
||||||
FakeBlockchain.prototype.getTransactions = function(addresses, cb) {
|
FakeBlockchain.prototype.getTransactions = function(addresses, cb) {
|
||||||
return cb(null, []);
|
return cb(null, []);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -752,7 +752,7 @@ describe('Wallet model', function() {
|
||||||
var utxo = createUTXO(w);
|
var utxo = createUTXO(w);
|
||||||
w.blockchain.fixUnspent(utxo);
|
w.blockchain.fixUnspent(utxo);
|
||||||
w.createTx(toAddress, amountSatStr, null, function(ntxid) {
|
w.createTx(toAddress, amountSatStr, null, function(ntxid) {
|
||||||
sinon.stub(w.blockchain, 'sendRawTransaction').yields(undefined);
|
sinon.stub(w.blockchain, 'broadcast').yields({statusCode: 303});
|
||||||
var spyCheckSentTx = sinon.spy(w, '_checkSentTx');
|
var spyCheckSentTx = sinon.spy(w, '_checkSentTx');
|
||||||
w.sendTx(ntxid, function () {});
|
w.sendTx(ntxid, function () {});
|
||||||
chai.expect(spyCheckSentTx.calledOnce).to.be.true;
|
chai.expect(spyCheckSentTx.calledOnce).to.be.true;
|
||||||
|
|
|
||||||
|
|
@ -98,6 +98,14 @@ describe('Insight model', function() {
|
||||||
emitSpy.calledWith('subscribe', 'mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
|
emitSpy.calledWith('subscribe', 'mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should subscribe to an address once', function() {
|
||||||
|
var blockchain = new Insight(FAKE_OPTS);
|
||||||
|
|
||||||
|
blockchain.subscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
|
||||||
|
blockchain.subscribe('mg7UbtKgMvWAixTNMbC8soyUnwFk1qxEuM');
|
||||||
|
blockchain.subscribed.length.should.equal(1);
|
||||||
|
});
|
||||||
|
|
||||||
it('should subscribe to a list of addresses', function() {
|
it('should subscribe to a list of addresses', function() {
|
||||||
var blockchain = new Insight(FAKE_OPTS);
|
var blockchain = new Insight(FAKE_OPTS);
|
||||||
var emitSpy = sinon.spy(blockchain.socket, 'emit');
|
var emitSpy = sinon.spy(blockchain.socket, 'emit');
|
||||||
|
|
|
||||||
|
|
@ -62,26 +62,26 @@ describe("Unit: controllerUtils", function() {
|
||||||
expect($rootScope.unitName).to.be.equal('bits');
|
expect($rootScope.unitName).to.be.equal('bits');
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
describe("Unit: controllerUtils #setSocketHandlers", function() {
|
describe("Unit: controllerUtils #updateGlobalAddresses", function() {
|
||||||
|
|
||||||
|
|
||||||
it(' should call updateAddressList ', inject(function(controllerUtils, $rootScope) {
|
it(' should call updateAddressList ', inject(function(controllerUtils, $rootScope) {
|
||||||
var spy = sinon.spy(controllerUtils, 'updateAddressList');
|
var spy = sinon.spy(controllerUtils, 'updateAddressList');
|
||||||
controllerUtils.setSocketHandlers();
|
controllerUtils.updateGlobalAddresses();
|
||||||
sinon.assert.callCount(spy, 1);
|
sinon.assert.callCount(spy, 1);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it('should update addresses', inject(function(controllerUtils, $rootScope) {
|
it('should update addresses', inject(function(controllerUtils, $rootScope) {
|
||||||
$rootScope.wallet = new FakeWallet();
|
$rootScope.wallet = new FakeWallet();
|
||||||
var Waddr = Object.keys($rootScope.wallet.balanceByAddr)[0];
|
var Waddr = Object.keys($rootScope.wallet.balanceByAddr)[0];
|
||||||
controllerUtils.setSocketHandlers();
|
controllerUtils.updateGlobalAddresses();
|
||||||
expect($rootScope.addrInfos[0].address).to.be.equal(Waddr);;
|
expect($rootScope.addrInfos[0].address).to.be.equal(Waddr);;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it('should set System Event Handlers', inject(function(controllerUtils, $rootScope, Socket) {
|
it('should set System Event Handlers', inject(function(controllerUtils, $rootScope, Socket) {
|
||||||
var spy = sinon.spy(Socket, 'sysOn');
|
var spy = sinon.spy(Socket, 'sysOn');
|
||||||
$rootScope.wallet = new FakeWallet();
|
$rootScope.wallet = new FakeWallet();
|
||||||
controllerUtils.setSocketHandlers();
|
controllerUtils.updateGlobalAddresses();
|
||||||
sinon.assert.callCount(spy, 5);
|
sinon.assert.callCount(spy, 5);
|
||||||
['error', 'reconnect_error', 'reconnect_failed', 'connect', 'reconnect'].forEach(function(e) {
|
['error', 'reconnect_error', 'reconnect_failed', 'connect', 'reconnect'].forEach(function(e) {
|
||||||
sinon.assert.calledWith(spy, e);
|
sinon.assert.calledWith(spy, e);
|
||||||
|
|
@ -92,7 +92,7 @@ describe("Unit: controllerUtils", function() {
|
||||||
var spy = sinon.spy(Socket, 'on');
|
var spy = sinon.spy(Socket, 'on');
|
||||||
$rootScope.wallet = new FakeWallet();
|
$rootScope.wallet = new FakeWallet();
|
||||||
var Waddr = Object.keys($rootScope.wallet.balanceByAddr)[0];
|
var Waddr = Object.keys($rootScope.wallet.balanceByAddr)[0];
|
||||||
controllerUtils.setSocketHandlers();
|
controllerUtils.updateGlobalAddresses();
|
||||||
sinon.assert.calledWith(spy, Waddr);
|
sinon.assert.calledWith(spy, Waddr);
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue