Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refresh frequency changes if the bee is in error state #409

Merged
merged 1 commit into from
Jun 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions src/providers/Bee.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import { WalletAddress } from '../utils/wallet'
import { Context as SettingsContext } from './Settings'
import { Context as TopUpContext } from './TopUp'

const REFRESH_WHEN_OK = 30_000
const REFRESH_WHEN_ERROR = 5_000

export enum CheckState {
OK = 'OK',
WARNING = 'Warning',
Expand Down Expand Up @@ -217,7 +220,7 @@ export function Provider({ children }: Props): ReactElement {

setApiHealth(false)

refresh()
if (beeApi !== null) refresh()
}, [beeApi]) // eslint-disable-line react-hooks/exhaustive-deps

useEffect(() => {
Expand All @@ -235,7 +238,7 @@ export function Provider({ children }: Props): ReactElement {
setSettlements(null)
setChainState(null)

refresh()
if (beeDebugApi !== null) refresh()
}, [beeDebugApi]) // eslint-disable-line react-hooks/exhaustive-deps

useEffect(() => {
Expand All @@ -245,7 +248,7 @@ export function Provider({ children }: Props): ReactElement {
}, [nodeAddresses, provider])

useEffect(() => {
const interval = setInterval(() => walletAddress?.refresh().then(setWalletAddress), 30_000)
const interval = setInterval(() => walletAddress?.refresh().then(setWalletAddress), REFRESH_WHEN_OK)

return () => clearInterval(interval)
}, [walletAddress])
Expand Down Expand Up @@ -378,13 +381,33 @@ export function Provider({ children }: Props): ReactElement {
}
}

const start = (freq = 30000) => setFrequency(freq)
const start = (freq = REFRESH_WHEN_OK) => {
refresh()
setFrequency(freq)
}
const stop = () => setFrequency(null)

// Start the update loop
const status = getStatus(
debugApiHealth,
nodeAddresses,
nodeInfo,
apiHealth,
topology,
chequebookAddress,
chequebookBalance,
error,
)

useEffect(() => {
refresh()
let newFrequency = REFRESH_WHEN_OK

if (status.all !== 'OK') newFrequency = REFRESH_WHEN_ERROR

if (newFrequency !== frequency) setFrequency(newFrequency)
}, [status.all, frequency])

// Start the update loop
useEffect(() => {
// Start autorefresh only if the frequency is set
if (frequency) {
const interval = setInterval(refresh, frequency)
Expand All @@ -396,16 +419,7 @@ export function Provider({ children }: Props): ReactElement {
return (
<Context.Provider
value={{
status: getStatus(
debugApiHealth,
nodeAddresses,
nodeInfo,
apiHealth,
topology,
chequebookAddress,
chequebookBalance,
error,
),
status,
balance: walletAddress,
latestUserVersion,
latestUserVersionExact,
Expand Down