Skip to content

Commit

Permalink
Over-fetching hook (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
grod220 authored Dec 13, 2024
1 parent 925873c commit 843a6bb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/pages/trade/api/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const useBook = () => {
},
});

useRefetchOnNewBlock(query);
useRefetchOnNewBlock('routeBook', query);

return query;
};
2 changes: 1 addition & 1 deletion src/pages/trade/api/candles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const useCandles = (durationWindow: DurationWindow) => {
},
});

useRefetchOnNewBlock(query);
useRefetchOnNewBlock('candles', query);

return query;
};
2 changes: 1 addition & 1 deletion src/pages/trade/model/useSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const useSummary = (window: DurationWindow) => {
},
});

useRefetchOnNewBlock(query);
useRefetchOnNewBlock('summary', query);

return query;
};
15 changes: 12 additions & 3 deletions src/shared/api/compact-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,23 @@ const startBlockHeightStream = async (transport: Transport, signal: AbortSignal)
}
};

export const useRefetchOnNewBlock = ({ refetch }: UseQueryResult) => {
const lastRefetchedBlockHeights = new Map<string, number>();

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'];
Expand Down

0 comments on commit 843a6bb

Please sign in to comment.