default long polling in socket.io (in config.js)

This commit is contained in:
Matias Alejo Garcia 2014-10-06 16:13:21 -03:00
commit 923a0bb975
7 changed files with 75 additions and 43 deletions

View file

@ -80,11 +80,9 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
if (err) { if (err) {
notification.warning('Could not open wallet'); notification.warning('Could not open wallet');
} else { } else {
iden.closeWallet($rootScope.wallet.id, function() { $scope.loading = false;
$scope.loading = false; $rootScope.wallet = w;
$rootScope.wallet = w; controllerUtils.bindWallet(w, $scope);
controllerUtils.bindWallet(w, $scope);
});
} }
}); });
}; };

View file

@ -264,7 +264,7 @@ Network.prototype._setupConnectionHandlers = function(opts, cb) {
self.socket.on('connect', function() { self.socket.on('connect', function() {
var pubkey = self.getKey().public.toString('hex'); var pubkey = self.getKey().public.toString('hex');
log.debug('Async subcribing to:pubkey:',pubkey); log.debug('Async subscribing to:pubkey:',pubkey);
self.socket.emit('subscribe', pubkey); self.socket.emit('subscribe', pubkey);
@ -325,7 +325,10 @@ Network.prototype.start = function(opts, openCallback) {
preconditions.checkState(this.connectedPeers && this.connectedPeers.length === 0); preconditions.checkState(this.connectedPeers && this.connectedPeers.length === 0);
if (this.started) return openCallback(); if (this.started) {
log.debug('Async: Networing already started for this wallet.')
return openCallback();
}
this.privkey = opts.privkey; this.privkey = opts.privkey;
this.setCopayerId(opts.copayerId); this.setCopayerId(opts.copayerId);
@ -336,8 +339,7 @@ Network.prototype.start = function(opts, openCallback) {
}; };
Network.prototype.createSocket = function() { Network.prototype.createSocket = function() {
log.debug('Async: Connecting to socket:', this.url, this.socketOptions);
console.log('Async: Connecting to socket:', this.url, this.socketOptions); //TODO
return io.connect(this.url, this.socketOptions); return io.connect(this.url, this.socketOptions);
}; };

View file

@ -127,7 +127,8 @@ Identity.create = function(email, password, opts, cb) {
cb(null, iden); cb(null, iden);
// default wallet // default wallet
var wopts = _.extend(opts.walletDefaults, { var dflt = _.clone(opts.walletDefaults);
var wopts = _.extend(dflt, {
nickname: email, nickname: email,
networkName: opts.networkName, networkName: opts.networkName,
requiredCopayers: 1, requiredCopayers: 1,
@ -447,7 +448,6 @@ Identity.prototype._checkVersion = function(inVersion) {
* @return * @return
*/ */
Identity.prototype.openWallet = function(walletId, password, cb) { Identity.prototype.openWallet = function(walletId, password, cb) {
console.log('[Identity.js.434:openWallet:]', walletId); //TODO
preconditions.checkArgument(cb); preconditions.checkArgument(cb);
var self = this; var self = this;

View file

@ -124,6 +124,7 @@ Insight.prototype.subscribeToBlocks = function() {
/** @private */ /** @private */
Insight.prototype._getSocketIO = function(url, opts) { Insight.prototype._getSocketIO = function(url, opts) {
log.debug('Insight: Connecting to socket:', this.url, this.opts);
return io(this.url, this.opts); return io(this.url, this.opts);
}; };

View file

@ -869,6 +869,12 @@ Wallet.prototype.netStart = function() {
var self = this; var self = this;
var net = this.network; var net = this.network;
if (net.started) {
log.debug('Wallet networking was ready')
self.emit('ready', net.getPeer());
return;
}
net.removeAllListeners(); net.removeAllListeners();
net.on('connect', self._onConnect.bind(self)); net.on('connect', self._onConnect.bind(self));
net.on('data', self._onData.bind(self)); net.on('data', self._onData.bind(self));
@ -2539,6 +2545,7 @@ Wallet.prototype.indexDiscovery = function(start, change, copayerIndex, gap, cb)
*/ */
Wallet.prototype.close = function(cb) { Wallet.prototype.close = function(cb) {
var self = this; var self = this;
this.network.cleanUp();
log.debug('## CLOSING Wallet: ' + this.id); log.debug('## CLOSING Wallet: ' + this.id);
this.lock.release(function() { this.lock.release(function() {
if (cb) return cb(); if (cb) return cb();

View file

@ -40,20 +40,29 @@ angular.module('copayApp.services')
} }
}; };
root.isFocusedWallet = function(wid) {
return wid === $rootScope.wallet.getId();
};
root.installWalletHandlers = function(w, $scope) { root.installWalletHandlers = function(w, $scope) {
var wid = w.getId();
w.on('connectionError', function() { w.on('connectionError', function() {
var message = "Could not connect to the Insight server. Check your settings and network configuration"; if (root.isFocusedWallet(wid)) {
notification.error('Networking Error', message); var message = "Could not connect to the Insight server. Check your settings and network configuration";
root.onErrorDigest($scope); notification.error('Networking Error', message);
}); root.onErrorDigest($scope);
w.on('ready', function() { }
$scope.loading = false;
}); });
w.on('corrupt', function(peerId) { w.on('corrupt', function(peerId) {
notification.error('Error', $filter('translate')('Received corrupt message from ') + peerId); if (root.isFocusedWallet(wid)) {
notification.error('Error', $filter('translate')('Received corrupt message from ') + peerId);
}
}); });
w.on('ready', function(myPeerID) { w.on('ready', function(myPeerID) {
$scope.loading = false;
$rootScope.wallet = w; $rootScope.wallet = w;
if ($rootScope.initialConnection) { if ($rootScope.initialConnection) {
$rootScope.initialConnection = false; $rootScope.initialConnection = false;
@ -66,38 +75,48 @@ angular.module('copayApp.services')
}); });
w.on('publicKeyRingUpdated', function(dontDigest) { w.on('publicKeyRingUpdated', function(dontDigest) {
root.updateAddressList(); if (root.isFocusedWallet(wid)) {
if (!dontDigest) { root.updateAddressList();
$rootScope.$digest(); if (!dontDigest) {
$rootScope.$digest();
}
} }
}); });
w.on('tx', function(address, isChange) { w.on('tx', function(address, isChange) {
if (!isChange) { if (root.isFocusedWallet(wid)) {
notification.funds('Funds received!', address); if (!isChange) {
notification.funds('Funds received!', address);
}
root.updateBalance(function() {
$rootScope.$digest();
});
} }
root.updateBalance(function() {
$rootScope.$digest();
});
}); });
w.on('balanceUpdated', function() { w.on('balanceUpdated', function() {
root.updateBalance(function() { if (root.isFocusedWallet(wid)) {
$rootScope.$digest(); root.updateBalance(function() {
}); $rootScope.$digest();
});
}
}); });
w.on('insightReconnected', function() { w.on('insightReconnected', function() {
$rootScope.reconnecting = false; if (root.isFocusedWallet(wid)) {
root.updateAddressList(); $rootScope.reconnecting = false;
root.updateBalance(function() { root.updateAddressList();
$rootScope.$digest(); root.updateBalance(function() {
}); $rootScope.$digest();
});
}
}); });
w.on('insightError', function() { w.on('insightError', function() {
$rootScope.reconnecting = true; if (root.isFocusedWallet(wid)) {
$rootScope.$digest(); $rootScope.reconnecting = true;
$rootScope.$digest();
}
}); });
var updateTxsAndBalance = _.debounce(function() { var updateTxsAndBalance = _.debounce(function() {
@ -108,11 +127,14 @@ angular.module('copayApp.services')
}, 3000); }, 3000);
w.on('txProposalsUpdated', function(dontDigest) { w.on('txProposalsUpdated', function(dontDigest) {
updateTxsAndBalance(); if (root.isFocusedWallet(wid)) {
updateTxsAndBalance();
}
}); });
w.on('txProposalEvent', function(e) { w.on('txProposalEvent', function(e) {
// TODO: add wallet name notification
var user = w.publicKeyRing.nicknameForCopayer(e.cId); var user = w.publicKeyRing.nicknameForCopayer(e.cId);
switch (e.type) { switch (e.type) {
case 'signed': case 'signed':
@ -127,8 +149,10 @@ angular.module('copayApp.services')
} }
}); });
w.on('addressBookUpdated', function(dontDigest) { w.on('addressBookUpdated', function(dontDigest) {
if (!dontDigest) { if (root.isFocusedWallet(wid)) {
$rootScope.$digest(); if (!dontDigest) {
$rootScope.$digest();
}
} }
}); });
w.on('connect', function(peerID) { w.on('connect', function(peerID) {
@ -156,7 +180,7 @@ angular.module('copayApp.services')
root.unbindWallet = function($scope) { root.unbindWallet = function($scope) {
var w =$rootScope.wallet; var w = $rootScope.wallet;
w.removeAllListeners(); w.removeAllListeners();
}; };

View file

@ -72,7 +72,7 @@ describe('Identity model', function() {
var password = 'password'; var password = 'password';
var config = { var config = {
wallet: { walletDefaults: {
requiredCopayers: 3, requiredCopayers: 3,
totalCopayers: 5, totalCopayers: 5,
spendUnconfirmed: 1, spendUnconfirmed: 1,
@ -107,7 +107,7 @@ describe('Identity model', function() {
it('should create an identity', function() { it('should create an identity', function() {
var iden = new Identity(email, password, config); var iden = new Identity(email, password, config);
should.exist(iden); should.exist(iden);
iden.walletDefaults.should.deep.equal(config.wallet); iden.walletDefaults.should.deep.equal(config.walletDefaults);
}); });
}); });
@ -183,7 +183,7 @@ describe('Identity model', function() {
it('should add wallet to profile', function(done) { it('should add wallet to profile', function(done) {
iden.createWallet(null, function(err, w) { iden.createWallet(null, function(err, w) {
profile.addWallet.getCall(0).args[0].should.contain('spy#'); profile.addWallet.getCall(0).args[0].should.contain('wid:123');
done(); done();
}); });
}); });