Commit 600aeaaf authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

fix: polling memory leak (#3676)

* chore: clarify stale callback

* fix: polling memory leak
parent 3bfbc74e
...@@ -92,7 +92,7 @@ export default function useClientSideSmartOrderRouterTrade<TTradeType extends Tr ...@@ -92,7 +92,7 @@ export default function useClientSideSmartOrderRouterTrade<TTradeType extends Tr
const getIsValidBlock = useGetIsValidBlock() const getIsValidBlock = useGetIsValidBlock()
const { data: quoteResult, error } = usePoll(getQuoteResult, JSON.stringify(queryArgs), { const { data: quoteResult, error } = usePoll(getQuoteResult, JSON.stringify(queryArgs), {
debounce: isDebouncing, debounce: isDebouncing,
staleCallback: useCallback(({ data }) => !getIsValidBlock(Number(data?.blockNumber) || 0), [getIsValidBlock]), isStale: useCallback(({ data }) => !getIsValidBlock(Number(data?.blockNumber) || 0), [getIsValidBlock]),
}) ?? { }) ?? {
error: undefined, error: undefined,
} }
......
...@@ -9,7 +9,7 @@ interface PollingOptions<T> { ...@@ -9,7 +9,7 @@ interface PollingOptions<T> {
debounce?: boolean debounce?: boolean
// If stale, any cached result will be returned, and a new fetch will be initiated. // If stale, any cached result will be returned, and a new fetch will be initiated.
staleCallback?: (value: T) => boolean isStale?: (value: T) => boolean
pollingInterval?: number pollingInterval?: number
keepUnusedDataFor?: number keepUnusedDataFor?: number
...@@ -25,7 +25,7 @@ export default function usePoll<T>( ...@@ -25,7 +25,7 @@ export default function usePoll<T>(
key = '', key = '',
{ {
debounce = false, debounce = false,
staleCallback, isStale,
pollingInterval = DEFAULT_POLLING_INTERVAL, pollingInterval = DEFAULT_POLLING_INTERVAL,
keepUnusedDataFor = DEFAULT_KEEP_UNUSED_DATA_FOR, keepUnusedDataFor = DEFAULT_KEEP_UNUSED_DATA_FOR,
}: PollingOptions<T> }: PollingOptions<T>
...@@ -39,11 +39,10 @@ export default function usePoll<T>( ...@@ -39,11 +39,10 @@ export default function usePoll<T>(
let timeout: number let timeout: number
const entry = cache.get(key) const entry = cache.get(key)
const isStale = staleCallback && entry?.result !== undefined ? staleCallback(entry.result) : false
if (entry) { if (entry) {
// If there is not a pending fetch (and there should be), queue one. // If there is not a pending fetch (and there should be), queue one.
if (entry.ttl) { if (entry.ttl) {
if (isStale) { if (isStale && entry?.result !== undefined ? isStale(entry.result) : false) {
poll() // stale results should be refetched immediately poll() // stale results should be refetched immediately
} else if (entry.ttl && entry.ttl + keepUnusedDataFor > Date.now()) { } else if (entry.ttl && entry.ttl + keepUnusedDataFor > Date.now()) {
timeout = setTimeout(poll, Math.max(0, entry.ttl - Date.now())) timeout = setTimeout(poll, Math.max(0, entry.ttl - Date.now()))
...@@ -57,6 +56,7 @@ export default function usePoll<T>( ...@@ -57,6 +56,7 @@ export default function usePoll<T>(
return () => { return () => {
clearTimeout(timeout) clearTimeout(timeout)
timeout = 0
} }
async function poll(ttl = Date.now() + pollingInterval) { async function poll(ttl = Date.now() + pollingInterval) {
...@@ -66,9 +66,9 @@ export default function usePoll<T>( ...@@ -66,9 +66,9 @@ export default function usePoll<T>(
// Always set the result in the cache, but only set it as data if the key is still being queried. // Always set the result in the cache, but only set it as data if the key is still being queried.
const result = await fetch() const result = await fetch()
cache.set(key, { ttl, result }) cache.set(key, { ttl, result })
setData((data) => (data.key === key ? { key, result } : data)) if (timeout) setData((data) => (data.key === key ? { key, result } : data))
} }
}, [cache, debounce, fetch, keepUnusedDataFor, key, pollingInterval, staleCallback]) }, [cache, debounce, fetch, isStale, keepUnusedDataFor, key, pollingInterval])
useEffect(() => { useEffect(() => {
// Cleanup stale entries when a new key is used. // Cleanup stale entries when a new key is used.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment