Skip to content

Commit

Permalink
Prevent duplicate renders of delegations by aborting earlier requests…
Browse files Browse the repository at this point in the history
… when new ones start (#721)
  • Loading branch information
jessepinho authored Mar 12, 2024
1 parent c3903a5 commit 69a2683
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions apps/minifront/src/state/staking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface StakingSlice {
* `delegationsByAccount`. Should be called each time `account` is changed.
*/
loadDelegationsForCurrentAccount: () => Promise<void>;
loadDelegationsForCurrentAccountAbortController?: AbortController;
/**
* Build and submit the Delegate transaction.
*/
Expand Down Expand Up @@ -168,6 +169,13 @@ export const createStakingSlice = (): SliceCreator<StakingSlice> => (set, get) =
unstakedTokensByAccount: new Map(),
unbondingTokensByAccount: new Map(),
loadDelegationsForCurrentAccount: async () => {
const existingAbortController = get().staking.loadDelegationsForCurrentAccountAbortController;
if (existingAbortController) existingAbortController.abort();
const newAbortController = new AbortController();
set(state => {
state.staking.loadDelegationsForCurrentAccountAbortController = newAbortController;
});

const addressIndex = new AddressIndex({ account: get().staking.account });
const validatorInfos: ValidatorInfo[] = [];

Expand Down Expand Up @@ -208,6 +216,11 @@ export const createStakingSlice = (): SliceCreator<StakingSlice> => (set, get) =
const throttledFlushToState = throttle(flushToState, THROTTLE_MS, { trailing: true });

for await (const delegation of getDelegationsForAccount(addressIndex)) {
if (newAbortController.signal.aborted) {
throttledFlushToState.cancel();
return;
}

delegationsToFlush.push(delegation);
validatorInfos.push(getValidatorInfoFromValueView(delegation));

Expand Down

0 comments on commit 69a2683

Please sign in to comment.