From 5adc4fd5d2429b4e3043e9cf49b4e91ef4e34c4b Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 31 Oct 2014 15:49:56 -0300 Subject: [PATCH] add debounce --- js/models/Identity.js | 41 ++++++++++++++++++++--------------------- js/models/Wallet.js | 4 ++-- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/js/models/Identity.js b/js/models/Identity.js index c3c6174c4..97832d96f 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -182,13 +182,6 @@ Identity.prototype.retrieveWalletFromStorage = function(walletId, opts, callback }); }; -/** - * TODO (matiu): What is this supposed to do? - */ -Identity.isAvailable = function(email, opts, cb) { - return cb(); -}; - /** * @param {Wallet} wallet * @param {Function} cb @@ -198,6 +191,7 @@ Identity.prototype.storeWallet = function(wallet, cb) { var val = wallet.toObj(); var key = wallet.getStorageKey(); + log.debug('Storing wallet:' + wallet.getName()); this.storage.setItem(key, val, function(err) { if (err) { @@ -208,6 +202,18 @@ Identity.prototype.storeWallet = function(wallet, cb) { }); }; + +/** + * @param {Identity} identity + * @param {Wallet} wallet + * @param {Function} cb + */ +Identity.storeWalletDebounced = _.debounce(function(identity, wallet, cb) { + identity.storeWallet(wallet,cb); +}, 3000); + + + Identity.prototype.toObj = function() { return _.extend({ walletIds: _.keys(this.wallets) @@ -373,36 +379,29 @@ Identity.prototype.bindWallet = function(w) { log.debug('Binding wallet ' + w.getName()); w.on('txProposalsUpdated', function() { - log.debug('> Wallet' + w.getName()); - self.storeWallet(w); + Identity.storeWalletDebounced(self, w); }); w.on('newAddresses', function() { - log.debug(' Wallet' + w.getName()); - self.storeWallet(w); + Identity.storeWalletDebounced(self, w); }); w.on('settingsUpdated', function() { - log.debug(' Wallet' + w.getName()); - self.storeWallet(w); + Identity.storeWalletDebounced(self, w); }); w.on('txProposalEvent', function() { - log.debug(' Wallet' + w.getName()); - self.storeWallet(w); + Identity.storeWalletDebounced(self, w); }); w.on('ready', function() { - log.debug(' Wallet' + w.getName()); self.store({ noWallets: true }, function() { - self.storeWallet(w); + Identity.storeWalletDebounced(self, w); }); }); w.on('addressBookUpdated', function() { - log.debug(' Wallet' + w.getName()); - self.storeWallet(w); + Identity.storeWalletDebounced(self, w); }); w.on('publicKeyRingUpdated', function() { - log.debug(' Wallet' + w.getName()); - self.storeWallet(w); + Identity.storeWalletDebounced(self, w); }); }; diff --git a/js/models/Wallet.js b/js/models/Wallet.js index f70633adf..5aeff5161 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -580,6 +580,7 @@ Wallet.prototype.updateFocusedTimestamp = function(ts) { this.focusedTimestamp = ts; }; + Wallet.prototype.updateSyncedTimestamp = function(ts) { preconditions.checkArgument(ts); preconditions.checkArgument(_.isNumber(ts)); @@ -588,7 +589,6 @@ Wallet.prototype.updateSyncedTimestamp = function(ts) { }; - /** * @desc Called when there are no messages in the server * Triggers a call to {@link Wallet#sendWalletReady} @@ -617,6 +617,7 @@ Wallet.prototype._onData = function(senderId, data, ts) { preconditions.checkArgument(_.isNumber(ts)); log.debug('Wallet:' + this.id + ' RECV', senderId, data); + this.updateSyncedTimestamp(ts); if (data.type !== 'walletId' && this.id !== data.walletId) { @@ -631,7 +632,6 @@ Wallet.prototype._onData = function(senderId, data, ts) { this.sendWalletReady(senderId); break; case 'walletReady': - if (this.lastMessageFrom[senderId] !== 'walletReady') { log.debug('Wallet:' + this.id + ' peer Sync received. since: ' + (data.sinceTs || 0)); this.sendPublicKeyRing(senderId);