perf(reduce call volume): save a bunch of calls to infura when the tab is not focused, change infura IDs for v2

This commit is contained in:
Moody Salem 2020-05-26 13:35:05 -04:00
parent 4414134bb2
commit c9721c42bf
No known key found for this signature in database
GPG Key ID: 8CB5CD10385138DB
5 changed files with 30 additions and 7 deletions

2
.env

@ -1,2 +1,2 @@
REACT_APP_CHAIN_ID="1" REACT_APP_CHAIN_ID="1"
REACT_APP_NETWORK_URL="https://mainnet.infura.io/v3/b8800ce81b8c451698081d269b86692b" REACT_APP_NETWORK_URL="https://mainnet.infura.io/v3/acb7e55995d04c49bfb52b7141599467"

@ -1,5 +1,5 @@
REACT_APP_CHAIN_ID="1" REACT_APP_CHAIN_ID="1"
REACT_APP_NETWORK_URL="https://mainnet.infura.io/v3/2acb2baa4c06402792e0c701a3697d10" REACT_APP_NETWORK_URL="https://mainnet.infura.io/v3/febcb10ca2754433a61e0805bc6c047d"
REACT_APP_PORTIS_ID="c0e2bf01-4b08-4fd5-ac7b-8e26b58cd236" REACT_APP_PORTIS_ID="c0e2bf01-4b08-4fd5-ac7b-8e26b58cd236"
REACT_APP_FORTMATIC_KEY="pk_live_F937DF033A1666BF" REACT_APP_FORTMATIC_KEY="pk_live_F937DF033A1666BF"
REACT_APP_GOOGLE_ANALYTICS_ID="UA-128182339-4" REACT_APP_GOOGLE_ANALYTICS_ID="UA-128182339-4"

@ -71,10 +71,9 @@ Cypress.Commands.overwrite('visit', (original, url, options) => {
...options, ...options,
onBeforeLoad(win) { onBeforeLoad(win) {
options && options.onBeforeLoad && options.onBeforeLoad(win) options && options.onBeforeLoad && options.onBeforeLoad(win)
const provider = new JsonRpcProvider('https://rinkeby.infura.io/v3/b8800ce81b8c451698081d269b86692b', 4) const provider = new JsonRpcProvider('https://rinkeby.infura.io/v3/acb7e55995d04c49bfb52b7141599467', 4)
const signer = new Wallet(PRIVATE_KEY_TEST_NEVER_USE, provider) const signer = new Wallet(PRIVATE_KEY_TEST_NEVER_USE, provider)
const bridge = new CustomizedBridge(signer, provider) win.ethereum = new CustomizedBridge(signer, provider)
win.ethereum = bridge
} }
}) })
}) })

@ -0,0 +1,20 @@
import { useCallback, useEffect, useState } from 'react'
/**
* Returns whether the window is currently visible to the user.
*/
export default function useIsWindowVisible(): boolean {
const [focused, setFocused] = useState<boolean>(true)
const listener = useCallback(() => {
setFocused(document.visibilityState !== 'hidden')
}, [setFocused])
useEffect(() => {
document.addEventListener('visibilitychange', listener)
return () => {
document.removeEventListener('visibilitychange', listener)
}
}, [listener])
return focused
}

@ -1,5 +1,6 @@
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { useDebounce, useActiveWeb3React } from '../../hooks' import { useDebounce, useActiveWeb3React } from '../../hooks'
import useIsWindowVisible from '../../hooks/useIsWindowVisible'
import { updateBlockNumber } from './actions' import { updateBlockNumber } from './actions'
import { useDispatch } from 'react-redux' import { useDispatch } from 'react-redux'
@ -7,6 +8,7 @@ export default function Updater() {
const { library, chainId } = useActiveWeb3React() const { library, chainId } = useActiveWeb3React()
const dispatch = useDispatch() const dispatch = useDispatch()
const windowVisible = useIsWindowVisible()
const [maxBlockNumber, setMaxBlockNumber] = useState<number | null>(null) const [maxBlockNumber, setMaxBlockNumber] = useState<number | null>(null)
// because blocks arrive in bunches with longer polling periods, we just want // because blocks arrive in bunches with longer polling periods, we just want
// to process the latest one. // to process the latest one.
@ -38,8 +40,10 @@ export default function Updater() {
useEffect(() => { useEffect(() => {
if (!chainId || !debouncedMaxBlockNumber) return if (!chainId || !debouncedMaxBlockNumber) return
dispatch(updateBlockNumber({ chainId, blockNumber: debouncedMaxBlockNumber })) if (windowVisible) {
}, [chainId, debouncedMaxBlockNumber, dispatch]) dispatch(updateBlockNumber({ chainId, blockNumber: debouncedMaxBlockNumber }))
}
}, [chainId, debouncedMaxBlockNumber, windowVisible, dispatch])
return null return null
} }