-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: check if some activity changed before updating store (#2451)
* check if some activity changed before updating store * refactor: cleanup update activities function Co-authored-by: Mark Nardi <[email protected]> --------- Co-authored-by: Nicole O'Brien <[email protected]>
- Loading branch information
1 parent
9e00b46
commit a0f540e
Showing
2 changed files
with
69 additions
and
43 deletions.
There are no files selected for viewing
93 changes: 52 additions & 41 deletions
93
packages/shared/src/lib/core/activity/actions/setAsyncStatusOfAccountActivities.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,71 @@ | ||
import { syncBalance } from '@core/account/actions/syncBalance' | ||
import { loadTokensForAllAccountBalances } from '@core/token/actions' | ||
import { StardustActivityAsyncStatus, ActivityDirection, StardustActivityType } from '../enums' | ||
import { allAccountActivities } from '../stores' | ||
import { allAccountActivities, updateAccountActivitiesInAllAccountActivities } from '../stores' | ||
import { getAsyncStatus } from '../utils/helper' | ||
import { NetworkNamespace } from '@core/network' | ||
import { StardustActivity } from '../types' | ||
import { Activity, StardustActivity } from '../types' | ||
import { updateNftForAccount } from '@core/nfts/stores' | ||
import { get } from 'svelte/store' | ||
|
||
export function setAsyncStatusOfAccountActivities(time: Date): void { | ||
const balancesToUpdate: number[] = [] | ||
allAccountActivities.update((state) => { | ||
state.forEach((accountActivities, accountIndex) => { | ||
const asyncActivities = accountActivities.filter( | ||
(_activity) => _activity.namespace === NetworkNamespace.Stardust && _activity.asyncData | ||
) as StardustActivity[] | ||
|
||
for (const activity of asyncActivities) { | ||
if (!activity.asyncData) { | ||
continue | ||
const updatedAccountActivities: { [accountIndex: number]: Activity[] } = {} | ||
const accountsToUpdate: Set<number> = new Set() | ||
|
||
for (const _accountIndex of Object.keys(get(allAccountActivities))) { | ||
const accountIndex = parseInt(_accountIndex) | ||
const accountActivities = get(allAccountActivities)[accountIndex] | ||
|
||
const asyncActivities = accountActivities.filter( | ||
(_activity) => _activity.namespace === NetworkNamespace.Stardust && _activity.asyncData | ||
) as StardustActivity[] | ||
|
||
for (const activity of asyncActivities) { | ||
if (!activity.asyncData) { | ||
continue | ||
} | ||
|
||
const oldAsyncStatus = activity.asyncData.asyncStatus | ||
if ( | ||
oldAsyncStatus === StardustActivityAsyncStatus.Claimed || | ||
oldAsyncStatus === StardustActivityAsyncStatus.Expired | ||
) { | ||
continue | ||
} | ||
activity.asyncData.asyncStatus = getAsyncStatus( | ||
false, | ||
activity.asyncData.expirationDate, | ||
activity.asyncData.timelockDate, | ||
!!activity.storageDeposit, | ||
time.getTime() | ||
) | ||
if (oldAsyncStatus !== null && oldAsyncStatus !== activity.asyncData.asyncStatus) { | ||
if (!updatedAccountActivities[accountIndex]) { | ||
updatedAccountActivities[accountIndex] = [] | ||
} | ||
updatedAccountActivities[accountIndex].push(activity) | ||
accountsToUpdate.add(accountIndex) | ||
|
||
const oldAsyncStatus = activity.asyncData.asyncStatus | ||
if ( | ||
oldAsyncStatus === StardustActivityAsyncStatus.Claimed || | ||
oldAsyncStatus === StardustActivityAsyncStatus.Expired | ||
activity.type === StardustActivityType.Nft && | ||
activity.asyncData.asyncStatus === StardustActivityAsyncStatus.Expired && | ||
activity.direction === ActivityDirection.Outgoing | ||
) { | ||
continue | ||
} | ||
activity.asyncData.asyncStatus = getAsyncStatus( | ||
false, | ||
activity.asyncData.expirationDate, | ||
activity.asyncData.timelockDate, | ||
!!activity.storageDeposit, | ||
time.getTime() | ||
) | ||
if (oldAsyncStatus !== null && oldAsyncStatus !== activity.asyncData.asyncStatus) { | ||
if (!balancesToUpdate.includes(accountIndex)) { | ||
balancesToUpdate.push(accountIndex) | ||
} | ||
|
||
if ( | ||
activity.type === StardustActivityType.Nft && | ||
activity.asyncData.asyncStatus === StardustActivityAsyncStatus.Expired && | ||
activity.direction === ActivityDirection.Outgoing | ||
) { | ||
updateNftForAccount(accountIndex, { id: activity.nftId, isSpendable: true }) | ||
} | ||
updateNftForAccount(accountIndex, { id: activity.nftId, isSpendable: true }) | ||
} | ||
} | ||
}) | ||
return state | ||
}) | ||
for (const accountIndex of balancesToUpdate) { | ||
} | ||
} | ||
|
||
if (Object.keys(updatedAccountActivities).length > 0) { | ||
updateAccountActivitiesInAllAccountActivities(updatedAccountActivities) | ||
} | ||
|
||
for (const accountIndex of accountsToUpdate) { | ||
syncBalance(accountIndex) | ||
} | ||
if (balancesToUpdate.length) { | ||
|
||
if (accountsToUpdate.size > 0) { | ||
void loadTokensForAllAccountBalances() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters