From 8e259c0e09ae1a51dda96c9a572fdb0944e64e2b Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Wed, 8 Apr 2015 15:18:28 -0300 Subject: [PATCH] simplify locking syntax --- lib/server.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/server.js b/lib/server.js index abf662a..948b7c0 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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. @@ -193,7 +198,7 @@ WalletService.prototype.replaceTemporaryRequestKey = function(opts, cb) { if (opts.isTemporaryRequestKey) 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) { if (err) return cb(err); @@ -301,7 +306,8 @@ WalletService.prototype.joinWallet = function(opts, cb) { if (_.isEmpty(opts.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) { if (err) return cb(err); @@ -360,7 +366,7 @@ WalletService.prototype.joinWallet = function(opts, cb) { WalletService.prototype.createAddress = function(opts, cb) { var self = this; - self.lock.runLocked(self.walletId, cb, function(cb) { + self._runLocked(cb, function(cb) { self.getWallet({}, function(err, wallet) { if (err) return cb(err); if (!wallet.isComplete()) @@ -615,7 +621,7 @@ WalletService.prototype.createTx = function(opts, cb) { if (!Utils.checkRequired(opts, ['toAddress', 'amount', 'proposalSignature'])) 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) { if (err) return cb(err); 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) { var self = this; - self.lock.runLocked(self.walletId, cb, function(cb) { + self._runLocked(cb, function(cb) { self.storage.removeWallet(self.walletId, cb); }); }; @@ -723,7 +729,7 @@ WalletService.prototype.removePendingTx = function(opts, cb) { if (!Utils.checkRequired(opts, ['txProposalId'])) return cb(new ClientError('Required argument missing')); - self.lock.runLocked(self.walletId, cb, function(cb) { + self._runLocked(cb, function(cb) { self.getTx({ 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) { if (err) return cb(err); if (!wallet.isComplete()) return cb(new ClientError('Wallet is not complete'));