Skip to content

Commit

Permalink
fix(usd-amount): delete token from queue only if there are no subscri…
Browse files Browse the repository at this point in the history
…bers left
  • Loading branch information
shoom3301 committed Sep 7, 2023
1 parent 3f5e116 commit 91f2e87
Showing 1 changed file with 26 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { atom } from 'jotai'

import { Fraction, Token } from '@uniswap/sdk-core'

import ms from 'ms.macro'

import { deepEqual } from 'utils/deepEqual'

export interface UsdRawPriceState {
Expand All @@ -15,53 +13,51 @@ export interface UsdRawPriceState {

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

const DELETION_FROM_QUEUE_DEBOUNCE = ms`5s`
const usdPriceQueueSubscribersCountAtom = atom<{ [tokenAddress: string]: number }>({})

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

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

const currenciesToDeleteFromQueueAtom = atom<{ [tokenAddress: string]: Token | undefined }>({})
export const addCurrencyToUsdPriceQueue = atom(null, (get, set, token: Token) => {
const currencyAddress = token.address.toLowerCase()
const usdPriceQueueSubscribersCount = get(usdPriceQueueSubscribersCountAtom)
const currenciesToLoadUsdPrice = { ...get(currenciesUsdPriceQueueAtom) }

export const addCurrencyToUsdPriceQueue = atom(null, (get, set, currency: Token) => {
const currencyAddress = currency.address.toLowerCase()
const currenciesToLoadUsdPrice = get(currenciesUsdPriceQueueAtom)
const subscribersCount = (usdPriceQueueSubscribersCount[currencyAddress] || 0) + 1

// Remove the currency from deletion queue
set(currenciesToDeleteFromQueueAtom, { ...get(currenciesToDeleteFromQueueAtom), [currencyAddress]: undefined })
// 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)
const currenciesToDeleteFromQueue = get(currenciesToDeleteFromQueueAtom)

const isCurrencyInUsdPriceQueue = !!currenciesToLoadUsdPrice[currencyAddress]
const isCurrencyInDeletionQueue = !!currenciesToDeleteFromQueue[currencyAddress]

if (isCurrencyInUsdPriceQueue && !isCurrencyInDeletionQueue) {
// Add the currency from deletion queue
set(currenciesToDeleteFromQueueAtom, { ...currenciesToDeleteFromQueue, [currencyAddress]: currency })
export const removeCurrencyToUsdPriceFromQueue = atom(null, (get, set, token: Token) => {
const currencyAddress = token.address.toLowerCase()
const usdPriceQueueSubscribersCount = get(usdPriceQueueSubscribersCountAtom)
const currenciesToLoadUsdPrice = { ...get(currenciesUsdPriceQueueAtom) }

setTimeout(() => {
const currenciesToLoadUsdPrice = { ...get(currenciesUsdPriceQueueAtom) }
const subscribersCount = Math.max((usdPriceQueueSubscribersCount[currencyAddress] || 0) - 1, 0)

// Remove the currency from USD price queue only if it's not added again
if (get(currenciesToDeleteFromQueueAtom)[currencyAddress]) {
set(currenciesToDeleteFromQueueAtom, { ...get(currenciesToDeleteFromQueueAtom), [currencyAddress]: undefined })
// Decrease the subscribers count
set(usdPriceQueueSubscribersCountAtom, {
...usdPriceQueueSubscribersCount,
[currencyAddress]: subscribersCount,
})

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

set(currenciesUsdPriceQueueAtom, currenciesToLoadUsdPrice)
}
}, DELETION_FROM_QUEUE_DEBOUNCE)
set(currenciesUsdPriceQueueAtom, currenciesToLoadUsdPrice)
}
})

Expand Down

0 comments on commit 91f2e87

Please sign in to comment.