Skip to content

Commit

Permalink
perf(webapp): reduce the number of queries performed to get blocks data
Browse files Browse the repository at this point in the history
  • Loading branch information
Torresmorah committed Feb 22, 2023
1 parent b5aed40 commit faca5d0
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions webapp/src/context/state.context.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useCallback } from 'react'
import React, { useState, useEffect, useCallback } from 'react'

import useLightUAL from '../hooks/useUAL'
import { ualConfig } from '../config'
Expand Down Expand Up @@ -107,6 +107,7 @@ export const SharedStateProvider = ({ ...props }) => {

export const useSharedState = () => {
const context = React.useContext(SharedStateContext)
const [lastBlock, setLastBlock] = useState()

if (!context) {
throw new Error(`useSharedState must be used within a SharedStateContext`)
Expand Down Expand Up @@ -197,17 +198,15 @@ export const useSharedState = () => {
}
}, [dispatch, state.tpb, state.tps, state.tpsWaitingBlock])

useEffect(() => {
let block = state.info.head_block_num

if (!block || !state.info.infoInterval) return
useEffect(()=>{
if(!lastBlock) return

const updateTransactions = async () => {
await getBlock(block)
await getBlock(lastBlock)
}

updateTransactions()
}, [state.info, getBlock])
},[lastBlock, getBlock])

const startTrackingProducerSchedule = async ({ interval = 120 } = {}) => {
if (scheduleInterval) {
Expand Down Expand Up @@ -237,9 +236,11 @@ export const useSharedState = () => {

dispatch({
type: 'updateInfo',
payload: { ...info, infoInterval },
payload: { ...info },
})
} catch (error) {

setLastBlock(info.head_block_num)
} catch (error) {
console.error(error)
}
}
Expand Down

0 comments on commit faca5d0

Please sign in to comment.