add debounce

This commit is contained in:
Matias Alejo Garcia 2014-10-31 15:49:56 -03:00
commit 5adc4fd5d2
2 changed files with 22 additions and 23 deletions

View file

@ -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('<txProposalsUpdated>> Wallet' + w.getName());
self.storeWallet(w);
Identity.storeWalletDebounced(self, w);
});
w.on('newAddresses', function() {
log.debug('<newAddresses> Wallet' + w.getName());
self.storeWallet(w);
Identity.storeWalletDebounced(self, w);
});
w.on('settingsUpdated', function() {
log.debug('<newAddresses> Wallet' + w.getName());
self.storeWallet(w);
Identity.storeWalletDebounced(self, w);
});
w.on('txProposalEvent', function() {
log.debug('<txProposalEvent> Wallet' + w.getName());
self.storeWallet(w);
Identity.storeWalletDebounced(self, w);
});
w.on('ready', function() {
log.debug('<ready> Wallet' + w.getName());
self.store({
noWallets: true
}, function() {
self.storeWallet(w);
Identity.storeWalletDebounced(self, w);
});
});
w.on('addressBookUpdated', function() {
log.debug('<addressBookUpdated> Wallet' + w.getName());
self.storeWallet(w);
Identity.storeWalletDebounced(self, w);
});
w.on('publicKeyRingUpdated', function() {
log.debug('<publicKeyRingUpdated> Wallet' + w.getName());
self.storeWallet(w);
Identity.storeWalletDebounced(self, w);
});
};

View file

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