Skip to content

Commit

Permalink
chore: check if some activity changed before updating store (#2451)
Browse files Browse the repository at this point in the history
* 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
MarkNerdi and nicole-obrien authored May 9, 2024
1 parent 9e00b46 commit a0f540e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 43 deletions.
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { get, writable } from 'svelte/store'
import { AsyncData, BaseStardustActivity, Activity } from '../types'
import { NetworkNamespace } from '@core/network'

export const allAccountActivities = writable<Activity[][]>([])
export const allAccountActivities = writable<{ [accountIndex: number]: Activity[] }>({})

export function addEmptyAccountActivities(accountIndex: number): void {
setAccountActivities(accountIndex, [])
Expand Down Expand Up @@ -98,5 +98,20 @@ export function updateAsyncDataByTransactionId(
}

export function clearAccountActivities(): void {
allAccountActivities.set([])
allAccountActivities.set({})
}

export function updateAccountActivitiesInAllAccountActivities(accountActivitiesToUpdate: {
[accountIndex: number]: Activity[]
}): void {
allAccountActivities.update((state) => {
for (const _accountIndex of Object.keys(accountActivitiesToUpdate)) {
const accountIndex = parseInt(_accountIndex)
for (const activity of accountActivitiesToUpdate[accountIndex]) {
const index = state[accountIndex].findIndex((_activity) => _activity.id === activity.id)
state[accountIndex][index] = activity
}
}
return state
})
}

0 comments on commit a0f540e

Please sign in to comment.