Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SWR Cache Quota #223

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/extension/src/shared/account/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export const getAccountSelector = memoize(
accountsEqual(account, baseAccount),
)

export const getOtherAccountsSelector = memoize(
(baseAccount: BaseWalletAccount) => (account: StoredWalletAccount) =>
!accountsEqual(account, baseAccount),
)

export const getNetworkSelector = memoize(
(networkId: string) => (account: StoredWalletAccount) =>
account.networkId === networkId,
Expand Down
25 changes: 25 additions & 0 deletions packages/extension/src/shared/transactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { WalletAccount } from "../wallet.model"
import { AlephiumExplorerTransaction } from "../explorer/type"
import { mapAlephiumTransactionToTransaction } from "./transformers"
import { getNetwork } from "../network"
import { walletStore } from "../wallet/walletStore"
import { getOtherAccountsSelector, withoutHiddenSelector } from "../account/selectors"
import { getAccounts } from "../account/store"

export type Status = 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'ACCEPTED_ON_MEMPOOL' | 'ACCEPTED_ON_L2' | 'ACCEPTED_ON_CHAIN' | 'REJECTED' | 'REMOVED_FROM_MEMPOOL';

Expand Down Expand Up @@ -129,3 +132,25 @@ function buildGetTransactionsFn(metadataTransactions: Transaction[]) {
)
}
}

export async function pruneTransactionsInfiniteScrollCache() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this function try to clear the cache of unselected accounts?

Copy link
Member Author

@h0ngcha0 h0ngcha0 Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if we have a selected account (we normally have unless this is a new wallet), then it will clear the cache for infinite scroll transaction for all other unselected accounts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I switch accounts twice, does it mean all account cache for scrolling would be removed?

Copy link
Member Author

@h0ngcha0 h0ngcha0 Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we switch account and close & open the extension wallet, it will clear all the account cache for transactions infinite scroll. Only switching account won't clear all scrolling cache.

The consequence of not having the infinite scroll cache is that if we click the activity tab to look at transaction history, it will try to fetch from the explorer backend immediately

Do you think this is too aggressive?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does SWR support LRU cache with a limited capacity? LRU cache can handle clearing automatically.

Copy link
Member Author

@h0ngcha0 h0ngcha0 Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SWR doesn't seem to have built-in support for LRU cache with limited capacity. I can investigate if we can integrate with custom cache provider to achieve this.

const selectedAccount = await walletStore.get("selected")
const accountSelector = selectedAccount ? getOtherAccountsSelector(selectedAccount) : withoutHiddenSelector
const accounts = await getAccounts(accountSelector)

for (const account of accounts) {
const network = await getNetwork(account.networkId)
const explorerApiUrl = network?.explorerApiUrl
if (!explorerApiUrl) {
continue
}

const transactionsInfiniteScrollCacheKey = `${explorerApiUrl}/addresses/${account.address}/transactions`
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i)
if (key && key.includes(transactionsInfiniteScrollCacheKey)) {
localStorage.removeItem(key)
}
}
}
}
2 changes: 2 additions & 0 deletions packages/extension/src/ui/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import SoftReloadProvider from "./services/resetAndReload"
import { useSentryInit } from "./services/sentry"
import { swrCacheProvider } from "./services/swr"
import { ThemeProvider, muiTheme } from "./theme"
import { pruneTransactionsInfiniteScrollCache } from "../shared/transactions"

export const App: FC = () => {
useTracking()
useSentryInit()
updateTokenList()
pruneTransactionsInfiniteScrollCache()

return (
<SoftReloadProvider>
Expand Down
Loading