simplify locking syntax

This commit is contained in:
Ivan Socolsky 2015-04-08 15:18:28 -03:00
parent 1bbdd4c14c
commit 8e259c0e09
1 changed files with 13 additions and 7 deletions

View File

@ -91,6 +91,11 @@ WalletService.getInstanceWithAuth = function(opts, cb) {
}); });
}; };
WalletService.prototype._runLocked = function(cb, task) {
$.checkState(this.walletId);
this.lock.runLocked(this.walletId, cb, task);
};
/** /**
* Creates a new wallet. * Creates a new wallet.
@ -193,7 +198,7 @@ WalletService.prototype.replaceTemporaryRequestKey = function(opts, cb) {
if (opts.isTemporaryRequestKey) if (opts.isTemporaryRequestKey)
return cb(new ClientError('Bad arguments')); return cb(new ClientError('Bad arguments'));
self.lock.runLocked(self.walletId, cb, function(cb) { self._runLocked(cb, function(cb) {
self.storage.fetchWallet(self.walletId, function(err, wallet) { self.storage.fetchWallet(self.walletId, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
@ -301,7 +306,8 @@ WalletService.prototype.joinWallet = function(opts, cb) {
if (_.isEmpty(opts.name)) if (_.isEmpty(opts.name))
return cb(new ClientError('Invalid copayer name')); return cb(new ClientError('Invalid copayer name'));
self.lock.runLocked(opts.walletId, cb, function(cb) { self.walletId = opts.walletId;
self._runLocked(cb, function(cb) {
self.storage.fetchWallet(opts.walletId, function(err, wallet) { self.storage.fetchWallet(opts.walletId, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
@ -360,7 +366,7 @@ WalletService.prototype.joinWallet = function(opts, cb) {
WalletService.prototype.createAddress = function(opts, cb) { WalletService.prototype.createAddress = function(opts, cb) {
var self = this; var self = this;
self.lock.runLocked(self.walletId, cb, function(cb) { self._runLocked(cb, function(cb) {
self.getWallet({}, function(err, wallet) { self.getWallet({}, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
if (!wallet.isComplete()) if (!wallet.isComplete())
@ -615,7 +621,7 @@ WalletService.prototype.createTx = function(opts, cb) {
if (!Utils.checkRequired(opts, ['toAddress', 'amount', 'proposalSignature'])) if (!Utils.checkRequired(opts, ['toAddress', 'amount', 'proposalSignature']))
return cb(new ClientError('Required argument missing')); return cb(new ClientError('Required argument missing'));
self.lock.runLocked(self.walletId, cb, function(cb) { self._runLocked(cb, function(cb) {
self.getWallet({}, function(err, wallet) { self.getWallet({}, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
if (!wallet.isComplete()) return cb(new ClientError('Wallet is not complete')); if (!wallet.isComplete()) return cb(new ClientError('Wallet is not complete'));
@ -705,7 +711,7 @@ WalletService.prototype.getTx = function(opts, cb) {
WalletService.prototype.removeWallet = function(opts, cb) { WalletService.prototype.removeWallet = function(opts, cb) {
var self = this; var self = this;
self.lock.runLocked(self.walletId, cb, function(cb) { self._runLocked(cb, function(cb) {
self.storage.removeWallet(self.walletId, cb); self.storage.removeWallet(self.walletId, cb);
}); });
}; };
@ -723,7 +729,7 @@ WalletService.prototype.removePendingTx = function(opts, cb) {
if (!Utils.checkRequired(opts, ['txProposalId'])) if (!Utils.checkRequired(opts, ['txProposalId']))
return cb(new ClientError('Required argument missing')); return cb(new ClientError('Required argument missing'));
self.lock.runLocked(self.walletId, cb, function(cb) { self._runLocked(cb, function(cb) {
self.getTx({ self.getTx({
txProposalId: opts.txProposalId, txProposalId: opts.txProposalId,
@ -1181,7 +1187,7 @@ WalletService.prototype.scan = function(opts, cb) {
}; };
self.lock.runLocked(self.walletId, cb, function(cb) { self._runLocked(cb, function(cb) {
self.getWallet({}, function(err, wallet) { self.getWallet({}, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
if (!wallet.isComplete()) return cb(new ClientError('Wallet is not complete')); if (!wallet.isComplete()) return cb(new ClientError('Wallet is not complete'));