From 3934b750f262faebe138fffcb066383cbecb6543 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 3 Jun 2014 11:54:07 -0300 Subject: [PATCH] polish reconnect --- js/models/core/Wallet.js | 29 ++++++++++++++++------------- js/models/network/WebRTC.js | 6 +++++- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 7bdd86ec7..9e01d1661 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -65,7 +65,7 @@ Wallet.prototype.seedCopayer = function(pubKey) { Wallet.prototype.connectToAll = function() { var all = this.publicKeyRing.getAllCopayerIds(); -console.log('[Wallet.js.58] connecting'); //TODO + console.log('[Wallet.js.58] connecting'); //TODO this.network.connectToCopayers(all); if (this.seededCopayerId) { this.sendWalletReady(this.seededCopayerId); @@ -101,7 +101,7 @@ Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) { var inTxp = copay.TxProposals.fromObj(data.txProposals); var ids = inTxp.getNtxids(); - if (ids.lenght>1) { + if (ids.lenght > 1) { this.emit('badMessage', senderId); this.log('Received BAD TxProposal messsage FROM:', senderId); //TODO return; @@ -165,12 +165,13 @@ Wallet.prototype._optsToObj = function() { spendUnconfirmed: this.spendUnconfirmed, requiredCopayers: this.requiredCopayers, totalCopayers: this.totalCopayers, + reconnectDelay: this.reconnectDelay, name: this.name, netKey: this.netKey, version: this.version, }; - if (this.token){ + if (this.token) { obj.token = this.token; obj.tokenTime = new Date().getTime(); } @@ -244,7 +245,7 @@ Wallet.prototype.netStart = function() { net.start(startOpts, function() { self.emit('ready', net.getPeer()); self.token = net.peer.options.token; - setTimeout(function(){ + setTimeout(function() { console.log('[EMIT publicKeyRingUpdated:]'); //TODO self.emit('publicKeyRingUpdated', true); console.log('[CONNECT:]'); //TODO @@ -252,17 +253,19 @@ Wallet.prototype.netStart = function() { console.log('[EMIT TxProposal]'); //TODO self.emit('txProposalsUpdated'); self.store(); - },10); + }, 10); }); }; Wallet.prototype.scheduleConnect = function() { var self = this; - self.connectToAll(); - setTimeout(function() { - self.scheduleConnect(); - }, - self.reconnectDelay); + if (self.network.isOnline()) { + self.connectToAll(); + setTimeout(function() { + self.scheduleConnect(); + }, + self.reconnectDelay); + } } Wallet.prototype.getOnlinePeerIDs = function() { @@ -341,7 +344,7 @@ Wallet.prototype.sendTxProposals = function(recipients, ntxid) { var last = toSend[toSend]; - for(var i in toSend) { + for (var i in toSend) { var id = toSend[i]; var lastInBatch = (i == toSend.length - 1); this.network.send(recipients, { @@ -455,7 +458,7 @@ Wallet.prototype.sign = function(ntxid, cb) { ret = true; } if (cb) return cb(ret); - },10); + }, 10); }; Wallet.prototype.sendTx = function(ntxid, cb) { @@ -635,7 +638,7 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, utxos, opts) { }]); var selectedUtxos = b.getSelectedUnspent(); - + var inputChainPaths = selectedUtxos.map(function(utxo) { return pkr.pathForAddress(utxo.address); }); diff --git a/js/models/network/WebRTC.js b/js/models/network/WebRTC.js index 912f35864..a7cf412c8 100644 --- a/js/models/network/WebRTC.js +++ b/js/models/network/WebRTC.js @@ -313,8 +313,8 @@ Network.prototype.setCopayerId = function(copayerId) { }; +// TODO cache this. Network.prototype.peerFromCopayer = function(hex) { - // TODO cache this. var SIN = bitcore.SIN; return new SIN(new Buffer(hex,'hex')).toString(); }; @@ -409,6 +409,10 @@ Network.prototype.send = function(copayerIds, payload, cb) { }; +Network.prototype.isOnline = function() { + return !!this.peer; +}; + Network.prototype.connectTo = function(copayerId) { var self = this;