Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
perf(app): prevent superfluous top level re-renders
Browse files Browse the repository at this point in the history
  • Loading branch information
korhaliv committed Apr 30, 2019
1 parent 91aa07a commit 0e9f763
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions renderer/components/App/App.js
@@ -1,9 +1,9 @@
import React, { useEffect, useState } from 'react' import React, { useEffect } from 'react'
import createScheduler from '@zap/utils/scheduler'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import Activity from 'containers/Activity' import Activity from 'containers/Activity'
import Wallet from 'containers/Wallet' import Wallet from 'containers/Wallet'
import { MainContent } from 'components/UI' import { MainContent } from 'components/UI'
import { useInterval } from 'hooks'


// Bitcoin blocks come on average every 10 mins // Bitcoin blocks come on average every 10 mins
// but we poll a lot more frequently to make UI a little bit more responsive // but we poll a lot more frequently to make UI a little bit more responsive
Expand All @@ -18,6 +18,9 @@ const MAX_REFETCH_INTERVAL = 1000 * 60 * 10
// Amount to increment re-fetch timer by after each fetch. // Amount to increment re-fetch timer by after each fetch.
const BACKOFF_SCHEDULE = 1.5 const BACKOFF_SCHEDULE = 1.5


// App scheduler / polling service
const appScheduler = createScheduler()

function App({ function App({
isAppReady, isAppReady,
modals, modals,
Expand All @@ -28,16 +31,32 @@ function App({
fetchTransactions, fetchTransactions,
setModals, setModals,
}) { }) {
const [nextFetchIn, setNextFetchIn] = useState(INITIAL_REFETCH_INTERVAL)
/** /**
* Fetch node data on an exponentially incrementing backoff schedule so that when the app is first mounted, we fetch * App scheduler / polling service setup. Add new app-wide polls here
* node data quite frequently but as time goes on the frequency is reduced down to a maximum of MAX_REFETCH_INTERVAL
*/ */
const fetchData = () => { useEffect(() => {
setNextFetchIn(Math.round(Math.min(nextFetchIn * BACKOFF_SCHEDULE, MAX_REFETCH_INTERVAL))) /**
// Fetch information about connected peers. * Fetch node data on an exponentially incrementing backoff schedule so that when the app is first mounted, we fetch
fetchPeers() * node data quite frequently but as time goes on the frequency is reduced down to a maximum of MAX_REFETCH_INTERVAL
} */
appScheduler.addTask({
task: fetchPeers,
taskId: 'fetchPeers',
baseDelay: INITIAL_REFETCH_INTERVAL,
maxDelay: MAX_REFETCH_INTERVAL,
backoff: BACKOFF_SCHEDULE,
})

appScheduler.addTask({
task: fetchTransactions,
taskId: 'fetchTransactions',
baseDelay: TX_REFETCH_INTERVAL,
})

return () => {
appScheduler.removeAllTasks()
}
}, [fetchPeers, fetchTransactions])


useEffect(() => { useEffect(() => {
// Set wallet open state. // Set wallet open state.
Expand All @@ -57,9 +76,6 @@ function App({
} }
}, [payReq, isAppReady, modals, setModals]) }, [payReq, isAppReady, modals, setModals])


useInterval(fetchData, nextFetchIn)
useInterval(fetchTransactions, TX_REFETCH_INTERVAL)

if (!isAppReady) { if (!isAppReady) {
return null return null
} }
Expand Down

0 comments on commit 0e9f763

Please sign in to comment.