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(usd-amount): delete token from queue only if there are no subscribers left #3121

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,51 @@ export interface UsdRawPriceState {

export type UsdRawPrices = { [tokenAddress: string]: UsdRawPriceState }

const usdPriceQueueSubscribersCountAtom = atom<{ [tokenAddress: string]: number }>({})

export const currenciesUsdPriceQueueAtom = atom<{ [tokenAddress: string]: Token }>({})

export const usdRawPricesAtom = atom<UsdRawPrices>({})

export const addCurrencyToUsdPriceQueue = atom(null, (get, set, currency: Token) => {
const currencyAddress = currency.address.toLowerCase()
const currenciesToLoadUsdPrice = get(currenciesUsdPriceQueueAtom)
export const addCurrencyToUsdPriceQueue = atom(null, (get, set, token: Token) => {
const currencyAddress = token.address.toLowerCase()
const usdPriceQueueSubscribersCount = get(usdPriceQueueSubscribersCountAtom)
const currenciesToLoadUsdPrice = { ...get(currenciesUsdPriceQueueAtom) }

const subscribersCount = (usdPriceQueueSubscribersCount[currencyAddress] || 0) + 1

// Increase the subscribers count
set(usdPriceQueueSubscribersCountAtom, {
...usdPriceQueueSubscribersCount,
[currencyAddress]: subscribersCount,
})

if (!currenciesToLoadUsdPrice[currencyAddress]) {
set(currenciesUsdPriceQueueAtom, {
...currenciesToLoadUsdPrice,
[currencyAddress]: currency,
[currencyAddress]: token,
})
}
})

export const removeCurrencyToUsdPriceFromQueue = atom(null, (get, set, currency: Token) => {
const currencyAddress = currency.address.toLowerCase()
const currenciesToLoadUsdPrice = get(currenciesUsdPriceQueueAtom)
export const removeCurrencyToUsdPriceFromQueue = atom(null, (get, set, token: Token) => {
const currencyAddress = token.address.toLowerCase()
const usdPriceQueueSubscribersCount = get(usdPriceQueueSubscribersCountAtom)
const currenciesToLoadUsdPrice = { ...get(currenciesUsdPriceQueueAtom) }

const subscribersCount = Math.max((usdPriceQueueSubscribersCount[currencyAddress] || 0) - 1, 0)

// Decrease the subscribers count
set(usdPriceQueueSubscribersCountAtom, {
...usdPriceQueueSubscribersCount,
[currencyAddress]: subscribersCount,
})

if (currenciesToLoadUsdPrice[currencyAddress]) {
const stateCopy = { ...currenciesToLoadUsdPrice }
delete stateCopy[currencyAddress]
// If there are no subscribers, then delete the token from queue
if (subscribersCount === 0) {
delete currenciesToLoadUsdPrice[currencyAddress]

set(currenciesUsdPriceQueueAtom, stateCopy)
set(currenciesUsdPriceQueueAtom, currenciesToLoadUsdPrice)
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ms from 'ms.macro'
import useSWR, { SWRConfiguration } from 'swr'

import { USDC } from 'legacy/constants/tokens'
import useDebounce from 'legacy/hooks/useDebounce'

import { useWalletInfo } from 'modules/wallet'

Expand All @@ -31,6 +32,8 @@ const swrOptions: SWRConfiguration = {
revalidateOnFocus: true,
}

const USD_PRICES_QUEUE_DEBOUNCE_TIME = ms`0.5s`

export function UsdPricesUpdater() {
const { chainId } = useWalletInfo()
const setUsdPrices = useSetAtom(usdRawPricesAtom)
Expand All @@ -40,15 +43,17 @@ export function UsdPricesUpdater() {

const queue = useMemo(() => Object.values(currenciesUsdPriceQueue), [currenciesUsdPriceQueue])

const debouncedQueue = useDebounce(queue, USD_PRICES_QUEUE_DEBOUNCE_TIME)

const swrResponse = useSWR<UsdRawPrices | null>(
['UsdPricesUpdater', queue, chainId],
['UsdPricesUpdater', debouncedQueue, chainId],
() => {
const getUsdcPrice = usdcPriceLoader(chainId)

setUsdPricesLoading(queue)
setUsdPricesLoading(debouncedQueue)

return processQueue(queue, getUsdcPrice).catch((error) => {
resetUsdPrices(queue)
return processQueue(debouncedQueue, getUsdcPrice).catch((error) => {
resetUsdPrices(debouncedQueue)

return Promise.reject(error)
})
Expand Down