Skip to content

Commit

Permalink
Merge pull request #17 from novasamatech/fix/balance-provider
Browse files Browse the repository at this point in the history
Fix balance provider
  • Loading branch information
ERussel authored Dec 8, 2023
2 parents b065077 + 6ca1d48 commit d3456de
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/common/balances/BalanceProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@ export const BalanceProvider = ({ children }: PropsWithChildren) => {
const unsubscribe = await service.subscribe(address, (balance: IAssetBalance) => {
console.log(`New balance: ${balance.total().toString()}`);

updateBalance(account, balance);
notifySubscribers(account, balance);
});

if (!setupUnsubscribeOnState(account, unsubscribe)) {
console.warn(`Can't setup unsubscribe ${chainAssetAccountIdToString(account)}`);
// this should never happen but let's cleanup remote subscription anyway

// looks like we have already unsubscribed
console.warn(`Can't setup unsubscribe ${chainAssetAccountIdToString(account)}`);
unsubscribe();
}
}
Expand All @@ -128,6 +129,18 @@ export const BalanceProvider = ({ children }: PropsWithChildren) => {
}
}

function updateBalance(account: ChainAssetAccount, newBalance: IAssetBalance) {
const stateKey = chainAssetAccountIdToString(account);
const state = accountToState.current[stateKey];

if (state) {
state.current = newBalance;
accountToState.current[stateKey] = state;
} else {
console.warn(`Can't update balance for state key ${stateKey}`);
}
}

function notifySubscribers(account: ChainAssetAccount, balance: IAssetBalance) {
const stateKey = chainAssetAccountIdToString(account);
const state = accountToState.current[stateKey];
Expand Down

0 comments on commit d3456de

Please sign in to comment.