diff --git a/src/pages/trade/api/book.ts b/src/pages/trade/api/book.ts index 6d7bb902..65635f65 100644 --- a/src/pages/trade/api/book.ts +++ b/src/pages/trade/api/book.ts @@ -25,7 +25,7 @@ export const useBook = () => { }, }); - useRefetchOnNewBlock(query); + useRefetchOnNewBlock('routeBook', query); return query; }; diff --git a/src/pages/trade/api/candles.tsx b/src/pages/trade/api/candles.tsx index 6ee76ade..87c0a9d5 100644 --- a/src/pages/trade/api/candles.tsx +++ b/src/pages/trade/api/candles.tsx @@ -27,7 +27,7 @@ export const useCandles = (durationWindow: DurationWindow) => { }, }); - useRefetchOnNewBlock(query); + useRefetchOnNewBlock('candles', query); return query; }; diff --git a/src/pages/trade/model/useSummary.ts b/src/pages/trade/model/useSummary.ts index dea9ce6a..618f2e18 100644 --- a/src/pages/trade/model/useSummary.ts +++ b/src/pages/trade/model/useSummary.ts @@ -20,7 +20,7 @@ export const useSummary = (window: DurationWindow) => { }, }); - useRefetchOnNewBlock(query); + useRefetchOnNewBlock('summary', query); return query; }; diff --git a/src/shared/api/compact-block.ts b/src/shared/api/compact-block.ts index da6d60ac..a355e43c 100644 --- a/src/shared/api/compact-block.ts +++ b/src/shared/api/compact-block.ts @@ -40,14 +40,23 @@ const startBlockHeightStream = async (transport: Transport, signal: AbortSignal) } }; -export const useRefetchOnNewBlock = ({ refetch }: UseQueryResult) => { +const lastRefetchedBlockHeights = new Map(); + +export const useRefetchOnNewBlock = (queryKey: string, { refetch }: UseQueryResult) => { const { data: blockHeight } = useLatestBlockHeight(); + const queryKeyString = JSON.stringify(queryKey); useEffect(() => { - if (blockHeight) { + if (!blockHeight) { + return; + } + + const lastHeight = lastRefetchedBlockHeights.get(queryKeyString) ?? -1; + if (blockHeight > lastHeight) { + lastRefetchedBlockHeights.set(queryKeyString, blockHeight); void refetch(); } - }, [blockHeight, refetch]); + }, [blockHeight, refetch, queryKeyString]); }; export const LATEST_HEIGHT_QUERY_KEY = ['latestBlockHeight'];