Skip to content

Commit

Permalink
Improve background balance update
Browse files Browse the repository at this point in the history
  • Loading branch information
johny committed Jun 29, 2023
1 parent 996d728 commit 2ff295c
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions components/pages/ecosystem/widget/useTransferWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BN } from '@polkadot/util';
import { validateEVMAddress, validateSubstrateAddress, validateTokenAmount } from 'lib/crypto';
import { processEVMDeposit } from 'lib/crypto/deposit';
import { processEVMWithdrawal } from 'lib/crypto/withdrawal';
import { useEffect, useReducer, useRef } from 'react';
import { useCallback, useEffect, useReducer, useRef } from 'react';
import { useWeb3Context } from './web3-context';

// Shared types
Expand Down Expand Up @@ -397,14 +397,15 @@ export const useTransferWidget = () => {
setNetwork(network);
};

const handleBalanceUpdate = () => {
const handleBalanceUpdate = useCallback(() => {
// Balance will update by just connecting to the network again
async function update() {
console.log('Periodically updating balances');
await updateBalances(state.network);
}
[0, 5, 15, 30, 60, 120].map((s) => window.setTimeout(update, s * 1000));
};

window.setTimeout(update, 10_000);
}, [state.network, updateBalances]);

// useEffect hooks to synchronize hook state with useWeb3Context state
useEffect(() => {
Expand All @@ -425,7 +426,7 @@ export const useTransferWidget = () => {
if (state.formState === 'success') {
handleBalanceUpdate();
}
}, [state.formState]);
}, [state.formState, handleBalanceUpdate]);

return {
state,
Expand Down

0 comments on commit 2ff295c

Please sign in to comment.