ADD: wallets autorefresh

This commit is contained in:
Overtorment 2018-07-02 14:51:24 +01:00
parent 40d57acb6b
commit 6a65f35a8b
3 changed files with 67 additions and 0 deletions

View file

@ -26,6 +26,31 @@ async function startAndDecrypt(retry) {
console.log('loaded from disk');
EV(EV.enum.WALLETS_COUNT_CHANGED);
EV(EV.enum.TRANSACTIONS_COUNT_CHANGED);
// now, lets try to fetch balance and txs for first wallet if it is time for it
let hadToRefresh = false;
let noErr = true;
try {
let wallets = BlueApp.getWallets();
if (wallets && wallets[0] && wallets[0].timeToRefresh()) {
console.log('time to refresh wallet #0');
let oldBalance = wallets[0].getBalance();
await wallets[0].fetchBalance();
if (oldBalance !== wallets.getBalance()) {
// balance changed, thus txs too
await wallets[0].fetchTransactions();
hadToRefresh = true;
EV(EV.enum.WALLETS_COUNT_CHANGED);
EV(EV.enum.TRANSACTIONS_COUNT_CHANGED);
}
} // end of timeToRefresh
} catch (Err) {
noErr = false;
console.warn(Err);
}
if (hadToRefresh && noErr) {
await BlueApp.saveToDisk(); // caching
}
}
if (!success && password) {

View file

@ -20,6 +20,12 @@ export class LegacyWallet extends AbstractWallet {
this._lastBalanceFetch = 0;
}
timeToRefresh() {
if (+new Date() - this._lastBalanceFetch >= 60 * 1000) {
return true;
}
}
generate() {
function myRng(c) {
let buf = Buffer.alloc(c);

View file

@ -133,6 +133,42 @@ export default class WalletsList extends Component {
50,
); // just to animate it, no real function
}
// now, lets try to fetch balance and txs for this wallet in case it has changed
this.lazyRefreshWallet(index);
}
/**
* Decides whether wallet with such index shoud be refreshed,
* refreshes if yes and redraws the screen
* @param index {Integer} Index of the wallet.
* @return {Promise.<void>}
*/
async lazyRefreshWallet(index) {
/** @type {Array.<AbstractWallet>} wallets */
let wallets = BlueApp.getWallets();
let oldBalance = wallets[index].getBalance();
let noErr = true;
try {
if (wallets && wallets[index] && wallets[index].timeToRefresh()) {
console.log('snapped to, and now its time to refresh wallet #', index);
await wallets[index].fetchBalance();
if (oldBalance !== wallets[index].getBalance()) {
// balance changed, thus txs too
await wallets[index].fetchTransactions();
this.refreshFunction();
}
}
} catch (Err) {
noErr = false;
console.warn(Err);
}
if (noErr && oldBalance !== wallets[index].getBalance()) {
// so we DID refresh
await BlueApp.saveToDisk(); // caching
}
}
render() {