From 1422107c6e91230780ffadf63adaac1288a45c19 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Thu, 30 Apr 2015 20:31:45 -0300 Subject: [PATCH] return after generating notification --- lib/server.js | 98 +++++++++++++++++++++++--------------- test/integration/server.js | 1 - 2 files changed, 59 insertions(+), 40 deletions(-) diff --git a/lib/server.js b/lib/server.js index 99aa833..84f922e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -260,11 +260,11 @@ WalletService.prototype.replaceTemporaryRequestKey = function(opts, cb) { walletId: opts.walletId, copayerId: self.copayerId, copayerName: opts.name, - }); - - return cb(null, { - copayerId: self.copayerId, - wallet: wallet + }, false, function() { + return cb(null, { + copayerId: self.copayerId, + wallet: wallet + }); }); }); }); @@ -297,7 +297,7 @@ WalletService.prototype._emit = function(eventName, args) { * @param {Object} data * @param {Boolean} isGlobal - If true, the notification is not issued on behalf of any particular copayer (defaults to false) */ -WalletService.prototype._notify = function(type, data, isGlobal) { +WalletService.prototype._notify = function(type, data, isGlobal, cb) { var self = this; log.debug('Notification', type, data); @@ -316,6 +316,7 @@ WalletService.prototype._notify = function(type, data, isGlobal) { }); this.storage.storeNotification(walletId, n, function() { self._emit('notification', n); + if (cb) return cb(); }); }; @@ -379,10 +380,11 @@ WalletService.prototype.joinWallet = function(opts, cb) { walletId: opts.walletId, copayerId: copayer.id, copayerName: copayer.name, - }); - return cb(null, { - copayerId: copayer.id, - wallet: wallet + }, false, function() { + return cb(null, { + copayerId: copayer.id, + wallet: wallet + }); }); }); }); @@ -411,8 +413,9 @@ WalletService.prototype.createAddress = function(opts, cb) { self._notify('NewAddress', { address: address.address, + }, false, function() { + return cb(null, address); }); - return cb(null, address); }); }); }); @@ -710,8 +713,9 @@ WalletService.prototype.createTx = function(opts, cb) { self._notify('NewTxProposal', { amount: opts.amount + }, false, function() { + return cb(null, txp); }); - return cb(null, txp); }); }); }); @@ -783,8 +787,9 @@ WalletService.prototype.removePendingTx = function(opts, cb) { if (actors.length > 1 || (actors.length == 1 && actors[0] !== self.copayerId)) return cb(new ClientError('TXACTIONED', 'Cannot remove a proposal signed/rejected by other copayers')); - self._notify('TxProposalRemoved'); - self.storage.removeTx(self.walletId, txp.id, cb); + self._notify('TxProposalRemoved', {}, false, function() { + self.storage.removeTx(self.walletId, txp.id, cb); + }); }); }); }; @@ -839,18 +844,26 @@ WalletService.prototype.signTx = function(opts, cb) { self.storage.storeTx(self.walletId, txp, function(err) { if (err) return cb(err); - self._notify('TxProposalAcceptedBy', { - txProposalId: opts.txProposalId, - copayerId: self.copayerId, + async.parallel([ + + function(done) { + self._notify('TxProposalAcceptedBy', { + txProposalId: opts.txProposalId, + copayerId: self.copayerId, + }, false, done); + }, + function(done) { + if (txp.isAccepted()) { + self._notify('TxProposalFinallyAccepted', { + txProposalId: opts.txProposalId, + }, false, done); + } else { + done(); + } + }, + ], function() { + return cb(null, txp); }); - - if (txp.isAccepted()) { - self._notify('TxProposalFinallyAccepted', { - txProposalId: opts.txProposalId, - }); - } - - return cb(null, txp); }); }); }); @@ -892,9 +905,9 @@ WalletService.prototype.broadcastTx = function(opts, cb) { self._notify('NewOutgoingTx', { txProposalId: opts.txProposalId, txid: txid + }, false, function() { + return cb(null, txp); }); - - return cb(null, txp); }); }); }); @@ -932,19 +945,26 @@ WalletService.prototype.rejectTx = function(opts, cb) { self.storage.storeTx(self.walletId, txp, function(err) { if (err) return cb(err); - self._notify('TxProposalRejectedBy', { - txProposalId: opts.txProposalId, - copayerId: self.copayerId, + async.parallel([ + + function(done) { + self._notify('TxProposalRejectedBy', { + txProposalId: opts.txProposalId, + copayerId: self.copayerId, + }, false, done); + }, + function(done) { + if (txp.status == 'rejected') { + self._notify('TxProposalFinallyRejected', { + txProposalId: opts.txProposalId, + }, false, done); + } else { + done(); + } + }, + ], function() { + return cb(null, txp); }); - - - if (txp.status == 'rejected') { - self._notify('TxProposalFinallyRejected', { - txProposalId: opts.txProposalId, - }); - }; - - return cb(null, txp); }); }); }; diff --git a/test/integration/server.js b/test/integration/server.js index 641204e..ea2f16d 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -3014,7 +3014,6 @@ describe('Wallet service', function() { should.not.exist(err); var copayerId2 = result.copayerId; - helpers.getAuthServer(copayerId, function(server) { server.getWallet({}, function(err, wallet) {