Preload prev blocks on page reload
This commit is contained in:
parent
81a2771f38
commit
ba2bfb12ee
|
@ -22,6 +22,8 @@ import { RuntimeContext } from "../../useRuntime";
|
||||||
|
|
||||||
const MAX_BLOCK_HISTORY = 20;
|
const MAX_BLOCK_HISTORY = 20;
|
||||||
|
|
||||||
|
const PREV_BLOCK_COUNT = 15;
|
||||||
|
|
||||||
const options: ChartOptions = {
|
const options: ChartOptions = {
|
||||||
animation: false,
|
animation: false,
|
||||||
plugins: {
|
plugins: {
|
||||||
|
@ -67,6 +69,11 @@ const Blocks: React.FC<BlocksProps> = ({ latestBlock, targetBlockNumber }) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skip blocks before the hard fork during the transition
|
||||||
|
if (blockNumber < targetBlockNumber) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const extBlock = await readBlock(provider, blockNumber.toString());
|
const extBlock = await readBlock(provider, blockNumber.toString());
|
||||||
setNow(Date.now());
|
setNow(Date.now());
|
||||||
setBlock((_blocks) => {
|
setBlock((_blocks) => {
|
||||||
|
@ -85,7 +92,7 @@ const Blocks: React.FC<BlocksProps> = ({ latestBlock, targetBlockNumber }) => {
|
||||||
return newBlocks;
|
return newBlocks;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[provider]
|
[provider, targetBlockNumber]
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -110,6 +117,24 @@ const Blocks: React.FC<BlocksProps> = ({ latestBlock, targetBlockNumber }) => {
|
||||||
};
|
};
|
||||||
}, [blocks]);
|
}, [blocks]);
|
||||||
|
|
||||||
|
// On page reload, pre-populate the last N blocks
|
||||||
|
useEffect(
|
||||||
|
() => {
|
||||||
|
const addPreviousBlocks = async () => {
|
||||||
|
for (
|
||||||
|
let i = latestBlock.number - PREV_BLOCK_COUNT;
|
||||||
|
i < latestBlock.number;
|
||||||
|
i++
|
||||||
|
) {
|
||||||
|
await addBlock(i);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
addPreviousBlocks();
|
||||||
|
},
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="w-full mb-auto">
|
<div className="w-full mb-auto">
|
||||||
<div className="px-9 pt-3 pb-12 divide-y-2">
|
<div className="px-9 pt-3 pb-12 divide-y-2">
|
||||||
|
|
Loading…
Reference in New Issue