fix: block number memoization (#7331)

This commit is contained in:
Zach Pomerantz 2023-09-18 13:08:51 -07:00 committed by GitHub
parent 33c93b5ded
commit c528c6169e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -41,9 +41,8 @@ export function BlockNumberProvider({ children }: { children: ReactNode }) {
chainId?: number chainId?: number
block?: number block?: number
mainnetBlock?: number mainnetBlock?: number
}>({ }>({})
chainId: activeChainId, const activeBlock = chainId === activeChainId ? block : undefined
})
const onChainBlock = useCallback((chainId: number, block: number) => { const onChainBlock = useCallback((chainId: number, block: number) => {
setChainBlock((chainBlock) => { setChainBlock((chainBlock) => {
@ -65,12 +64,13 @@ export function BlockNumberProvider({ children }: { children: ReactNode }) {
let stale = false let stale = false
if (provider && activeChainId && windowVisible) { if (provider && activeChainId && windowVisible) {
// If chainId hasn't changed, don't clear the block. This prevents re-fetching still valid data. setChainBlock((chainBlock) => {
setChainBlock((chainBlock) => // If chainId hasn't changed, don't clear the block. This prevents re-fetching still valid data.
chainBlock.chainId === activeChainId if (chainBlock.chainId !== activeChainId) {
? chainBlock return { chainId: activeChainId, mainnetBlock: chainBlock.mainnetBlock }
: { chainId: activeChainId, mainnetBlock: chainBlock.mainnetBlock } }
) return chainBlock
})
provider provider
.getBlockNumber() .getBlockNumber()
@ -107,7 +107,7 @@ export function BlockNumberProvider({ children }: { children: ReactNode }) {
const value = useMemo( const value = useMemo(
() => ({ () => ({
fastForward: (update: number) => { fastForward: (update: number) => {
if (block && update > block) { if (activeBlock && update > activeBlock) {
setChainBlock({ setChainBlock({
chainId: activeChainId, chainId: activeChainId,
block: update, block: update,
@ -115,10 +115,10 @@ export function BlockNumberProvider({ children }: { children: ReactNode }) {
}) })
} }
}, },
block: chainId === activeChainId ? block : undefined, block: activeBlock,
mainnetBlock, mainnetBlock,
}), }),
[activeChainId, block, chainId, mainnetBlock] [activeBlock, activeChainId, mainnetBlock]
) )
return <BlockNumberContext.Provider value={value}>{children}</BlockNumberContext.Provider> return <BlockNumberContext.Provider value={value}>{children}</BlockNumberContext.Provider>
} }