Merge pull request #5529 from ajp8164/bug/async-storage-upgrade

Replace forEach() with _asyncEach() to block execution until loop cal…
This commit is contained in:
Gustavo Maximiliano Cortez 2017-01-25 15:44:22 -03:00 committed by GitHub
commit 9521696b37

View file

@ -176,6 +176,7 @@ angular.module('copayApp.services')
}); });
}); });
} }
callback();
}, function() { }, function() {
// done // done
// Remove obsolete key. // Remove obsolete key.
@ -237,7 +238,7 @@ angular.module('copayApp.services')
'the following accounts validated OK: ' + (verified.length > 0 ? verified : 'none')); 'the following accounts validated OK: ' + (verified.length > 0 ? verified : 'none'));
}); });
} else { } else {
cb(null, (verified.length > 0 ? 'accounts OK: ' + verified : '')); cb(null, (verified.length > 0 ? 'accounts OK: ' + verified : 'no accounts found'));
} }
}); });
}); });
@ -332,8 +333,8 @@ angular.module('copayApp.services')
var errorMessage = undefined; var errorMessage = undefined;
var keys = Object.keys(_upgraders).sort(); var keys = Object.keys(_upgraders).sort();
var networks = ['livenet', 'testnet']; var networks = ['livenet', 'testnet'];
_asyncEach(keys, function(key, callback) { _asyncEach(keys, function(key, callback_keys) {
networks.forEach(function(network) { _asyncEach(networks, function(network, callback_networks) {
var storagekey = key.split('_')[1]; var storagekey = key.split('_')[1];
_upgraders[key](storagekey, network, function(err, msg) { _upgraders[key](storagekey, network, function(err, msg) {
if (err) { if (err) {
@ -342,11 +343,14 @@ angular.module('copayApp.services')
errorMessage = errorCount + ' storage upgrade failures'; errorMessage = errorCount + ' storage upgrade failures';
} }
if (msg) _handleUpgradeSuccess(storagekey + '-' + network, msg); if (msg) _handleUpgradeSuccess(storagekey + '-' + network, msg);
callback(); callback_networks();
}); });
}, function() {
// done - networks
callback_keys();
}); });
}, function() { }, function() {
//done //done - keys
cb(errorMessage); cb(errorMessage);
}); });
}; };
@ -697,15 +701,18 @@ angular.module('copayApp.services')
} }
bitpayAccounts = bitpayAccounts || {}; bitpayAccounts = bitpayAccounts || {};
var cards = []; var cards = [];
Object.keys(bitpayAccounts).forEach(function(email) { _asyncEach(Object.keys(bitpayAccounts), function(email, callback) {
// For the UI, add the account email to the card object. // For the UI, add the account email to the card object.
var acctCards = bitpayAccounts[email]['bitpayDebitCards-' + network] || []; var acctCards = bitpayAccounts[email]['bitpayDebitCards-' + network] || [];
for (var i = 0; i < acctCards.length; i++) { for (var i = 0; i < acctCards.length; i++) {
acctCards[i].email = email; acctCards[i].email = email;
} }
cards = cards.concat(acctCards); cards = cards.concat(acctCards);
callback();
}, function() {
// done
cb(err, cards);
}); });
cb(err, cards);
}); });
}; };
@ -727,7 +734,7 @@ angular.module('copayApp.services')
bitpayAccounts = JSON.parse(bitpayAccounts); bitpayAccounts = JSON.parse(bitpayAccounts);
} }
bitpayAccounts = bitpayAccounts || {}; bitpayAccounts = bitpayAccounts || {};
Object.keys(bitpayAccounts).forEach(function(email) { _asyncEach(Object.keys(bitpayAccounts), function(email, callback) {
var data = bitpayAccounts[email]['bitpayDebitCards-' + network]; var data = bitpayAccounts[email]['bitpayDebitCards-' + network];
var newCards = lodash.reject(data, { var newCards = lodash.reject(data, {
'eid': card.eid 'eid': card.eid
@ -741,12 +748,15 @@ angular.module('copayApp.services')
root.getBitpayDebitCards(network, function(err, cards) { root.getBitpayDebitCards(network, function(err, cards) {
if (err) cb(err); if (err) cb(err);
if (cards.length == 0) { if (cards.length == 0) {
root.removeNextStep('BitpayCard', cb); root.removeNextStep('BitpayCard', callback());
} else { } else {
cb(); callback()
} }
}); });
}); });
}, function() {
// done
cb();
}); });
}); });
}; };