Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
re-run tokens fetch if network changes while there's an in-flight req…
Browse files Browse the repository at this point in the history
…uest
  • Loading branch information
ramirotw committed Jun 28, 2022
1 parent 5192bd7 commit a352180
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/hooks/useErc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ export function useMultipleErc20(
// check what on globalState has not been fetched yet
const toFetch = useMemo(() => addresses.filter((address) => !erc20s[address]), [addresses, erc20s])
// flow control
const running = useRef(false)
const running = useRef({ networkId, isRunning: false })

const updateErc20s = useCallback(async (): Promise<void> => {
if (!networkId || toFetch.length === 0) {
return
}

running.current = true
running.current = { networkId, isRunning: true }

setIsLoading(true)
setErrors({})
Expand All @@ -146,15 +146,15 @@ export function useMultipleErc20(
saveErc20s(fetched.filter(Boolean) as TokenErc20[])

setIsLoading(false)
running.current = false
running.current = { networkId, isRunning: false }
}, [networkId, saveErc20s, toFetch])

useEffect(() => {
// only trigger network query if not yet running
if (!running.current) {
// only trigger network query if not yet running or the network has changed
if (!running.current.isRunning || running.current.networkId !== networkId) {
updateErc20s()
}
}, [updateErc20s, saveErc20s])
}, [updateErc20s, saveErc20s, networkId])

return { isLoading, error: errors, value: erc20s }
}

0 comments on commit a352180

Please sign in to comment.