From faca5d061c8709a652036b4bd8c3868326edfbc3 Mon Sep 17 00:00:00 2001 From: Torresmorah Date: Wed, 22 Feb 2023 09:02:19 -0600 Subject: [PATCH] perf(webapp): reduce the number of queries performed to get blocks data --- webapp/src/context/state.context.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/webapp/src/context/state.context.js b/webapp/src/context/state.context.js index 8d6f91c3..6cd6f7c4 100644 --- a/webapp/src/context/state.context.js +++ b/webapp/src/context/state.context.js @@ -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' @@ -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`) @@ -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) { @@ -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) } }