From d5f01e9713b5c7338662900f8679e631e598880e Mon Sep 17 00:00:00 2001 From: Sebastiaan Pasma Date: Tue, 21 Aug 2018 21:36:55 +0200 Subject: [PATCH] stop fetching the wallet history when everything is fetched --- src/js/controllers/walletDetails.js | 54 +++++-------------- src/js/services/wallet-history.service.js | 63 +++++++++++++++++++++-- 2 files changed, 72 insertions(+), 45 deletions(-) diff --git a/src/js/controllers/walletDetails.js b/src/js/controllers/walletDetails.js index d22ab388f..fece19906 100644 --- a/src/js/controllers/walletDetails.js +++ b/src/js/controllers/walletDetails.js @@ -13,6 +13,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun gettingCachedHistory: true, gettingInitialHistory: true, updatingTxHistory: false, + fetchedAllTxHistory: false, //updateTxHistoryError: false updateTxHistoryFailed: false }; @@ -108,6 +109,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun $scope.updatingStatus = true; $scope.updateStatusError = null; $scope.walletNotRegistered = false; + $scope.vm.fetchedAllTxHistory = false; walletService.getStatus($scope.wallet, { force: !!force, @@ -198,44 +200,6 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun }); }; - - var updateTxHistory = function(cb) { - if (!cb) cb = function() {}; - $scope.vm.updateTxHistoryFailed = false; - $scope.updatingTxHistoryProgress = 0; - - feeService.getFeeLevels($scope.wallet.coin, function(err, levels) { - walletService.getTxHistory($scope.wallet, { - feeLevels: levels - }, function(err, txHistory) { - $scope.vm.gettingInitialHistory = false; - if (err) { - $scope.txHistory = null; - $scope.vm.updateTxHistoryFailed = true; - return; - } - - applyCurrencyAliases(txHistory); - - var config = configService.getSync(); - var fiatCode = config.wallet.settings.alternativeIsoCode; - lodash.each(txHistory, function(t) { - var r = rateService.toFiat(t.amount, fiatCode, $scope.wallet.coin); - t.alternativeAmountStr = r.toFixed(2) + ' ' + fiatCode; - }); - console.log('pagination Got tx history old way'); - completeTxHistory = txHistory; - - //$scope.showHistory(); - $timeout(function() { - $scope.$apply(); - }); - return cb(); - }); - }); - }; - - function applyCurrencyAliases(txHistory) { var defaults = configService.getDefaults(); var configCache = configService.getSync(); @@ -289,7 +253,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun function fetchAndShowTxHistory(getLatest, flushCacheOnNew) { $scope.vm.updatingTxHistory = true; - walletHistoryService.updateLocalTxHistoryByPage($scope.wallet, getLatest, flushCacheOnNew, function onUpdateLocalTxHistoryByPage(err, txHistory) { + walletHistoryService.updateLocalTxHistoryByPage($scope.wallet, getLatest, flushCacheOnNew, function onUpdateLocalTxHistoryByPage(err, txHistory, fetchedAllTransactions) { console.log('pagination returned'); $scope.vm.gettingInitialHistory = false; $scope.vm.updatingTxHistory = false; @@ -300,6 +264,12 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun $scope.vm.updateTxHistoryFailed = true; return; } + + if (fetchedAllTransactions) { + console.log("All transactions seem to be fetched.."); + $scope.vm.fetchedAllTxHistory = true; + } + console.log('pagination txs returned in history: ' + txHistory.length); formatTxHistoryForDisplay(txHistory); @@ -313,7 +283,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun function showHistory(showAll) { if (completeTxHistory) { $scope.txHistory = showAll ? completeTxHistory : completeTxHistory.slice(0, (currentTxHistoryDisplayPage + 1) * DISPLAY_PAGE_SIZE); - $scope.vm.allowInfiniteScroll = completeTxHistory.length > $scope.txHistory.length || !$scope.vm.gettingInitialHistory; + $scope.vm.allowInfiniteScroll = !$scope.vm.fetchedAllTxHistory;//(completeTxHistory.length > $scope.txHistory.length || !$scope.vm.gettingInitialHistory) || (!$scope.vm.gettingInitialHistory && !$scope.vm.fetchedAllTxHistory); console.log('pagination Showing txs: ', $scope.txHistory.length); } else { $scope.vm.allowInfiniteScroll = false; @@ -392,7 +362,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun $timeout(function() { $scope.$broadcast('scroll.refreshComplete'); }, 300); - $scope.updateAll(true); + $scope.updateAll(true, true, false); }; $scope.updateAll = function(forceStatusUpdate, getLatestTx, flushTxCacheOnNew)  { @@ -544,7 +514,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun updateTxHistoryFromCachedData(); $scope.updateAll(false, true, true); refreshAmountSection(); - //refreshInterval = $interval($scope.onRefresh, 10 * 1000); + refreshInterval = $interval($scope.onRefresh, 10 * 1000); //refreshInterval = $interval($scope.onRefresh, 120 * 1000); // For testing }); diff --git a/src/js/services/wallet-history.service.js b/src/js/services/wallet-history.service.js index 00847cbbf..e8ba8cf52 100644 --- a/src/js/services/wallet-history.service.js +++ b/src/js/services/wallet-history.service.js @@ -18,12 +18,46 @@ var SAFE_CONFIRMATIONS = 6; + var allTransactionsFetched = false; var service = { getCachedTxHistory: getCachedTxHistory, updateLocalTxHistoryByPage: updateLocalTxHistoryByPage, }; return service; +/* + function hasAllTransactionsFetched(walletId, cachedTxs, newTxs) { + var cachedTxIds = {}; + cachedTxs.forEach(function forCachedTx(tx){ + cachedTxIds[tx.txid] = true; + }); + + var someTransactionWereNew = false; + var overlappingTxsCount = 0; + + newTxs.forEach(function forNewTx(tx){ + if (cachedTxIds[tx.txid]) { + overlappingTxsCount++; + } else { + someTransactionWereNew = true; + } + }); + + console.log('pagination Overlapping transactions:', overlappingTxsCount); + if (overlappingTxsCount >= MIN_KNOWN_TX_OVERLAP) { // We are good + if (!someTransactionWereNew && overlappingTxsCount === newTxs.length) { + console.log("We probably have all of the transactions fetched!!") + return true; + } + } else { + console.log("Something went wrong") + return true; // Something went wrong, so stop fetching.. + } + return false; + + } +*/ + function addEarlyTransactions(walletId, cachedTxs, newTxs) { var cachedTxIds = {}; @@ -48,6 +82,9 @@ if (someTransactionWereNew) { console.log('pagination someTransactionsWereNew'); saveTxHistory(walletId, cachedTxs); + } else if (overlappingTxsCount === newTxs.length) { + console.log('We probably have all transactions now'); + allTransactionsFetched = true; } return cachedTxs; } else { @@ -60,7 +97,26 @@ } - function addLatestTransactions(cachedTxs, newTxs) { + function addLatestTransactions(walletId, cachedTxs, newTxs) { + var cachedTxIds = {}; + var someTransactionWereNew = false; + cachedTxs.forEach(function forCachedTx(tx){ + cachedTxIds[tx.txid] = true; + }); + + newTxs.forEach(function forNewTx(tx){ + if (!cachedTxIds[tx.txid]) { + console.log("Brand new transactions pushed to top of the cache.") + someTransactionWereNew = true; + cachedTxs.unshift(tx); + } + }); + + if (someTransactionWereNew) { + saveTxHistory(walletId, cachedTxs); + } + + return cachedTxs; } // Only clear the cache once we have received new transactions from the server. @@ -199,7 +255,7 @@ } if (fetchedTxs.length === 0) { - return cb(null, cachedTxs); + return cb(null, cachedTxs, true /*fetchedAllTransactions*/); } var txs = []; @@ -207,11 +263,12 @@ txs = addLatestTransactions(wallet.id, cachedTxs, fetchedTxs); } else { txs = addEarlyTransactions(wallet.id, cachedTxs, fetchedTxs); + return cb(null, txs, allTransactionsFetched/*, hasAllTransactionsFetched(wallet.id, cachedTxs, fetchedTxs)*/); } return cb(null, txs); }); - + }); } }