Fix prefetch

This commit is contained in:
Willian Mitsuda 2022-02-01 19:14:05 -03:00
parent 97f6ae89fe
commit f09c49dfed
2 changed files with 47 additions and 36 deletions

View File

@ -1,4 +1,4 @@
import React, { useContext } from "react"; import React, { useContext, useEffect } from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faChevronLeft } from "@fortawesome/free-solid-svg-icons/faChevronLeft"; import { faChevronLeft } from "@fortawesome/free-solid-svg-icons/faChevronLeft";
import { faChevronRight } from "@fortawesome/free-solid-svg-icons/faChevronRight"; import { faChevronRight } from "@fortawesome/free-solid-svg-icons/faChevronRight";
@ -22,25 +22,22 @@ const NavNonce: React.FC<NavNonceProps> = ({ sender, nonce }) => {
// Prefetch // Prefetch
const swrConfig = useSWRConfig(); const swrConfig = useSWRConfig();
const prefetch = () => { useEffect(() => {
if (provider && sender && nonce !== undefined) { if (!provider || !sender || nonce === undefined || count === undefined) {
return;
}
prefetchTransactionBySenderAndNonce(swrConfig, provider, sender, nonce - 1);
prefetchTransactionBySenderAndNonce(swrConfig, provider, sender, nonce + 1);
if (count > 0) {
prefetchTransactionBySenderAndNonce( prefetchTransactionBySenderAndNonce(
swrConfig, swrConfig,
provider, provider,
sender, sender,
nonce - 2 count - 1
);
prefetchTransactionBySenderAndNonce(
swrConfig,
provider,
sender,
nonce + 2
); );
} }
}; }, [swrConfig, provider, sender, nonce, count]);
// Always prefetch
prefetch();
return ( return (
<div className="pl-2 self-center flex space-x-1"> <div className="pl-2 self-center flex space-x-1">

View File

@ -530,16 +530,18 @@ export const useTransactionCount = (
}; };
type TransactionBySenderAndNonceKey = { type TransactionBySenderAndNonceKey = {
provider: JsonRpcProvider; network: number;
sender: ChecksummedAddress; sender: ChecksummedAddress;
nonce: number; nonce: number;
}; };
export const getTransactionBySenderAndNonceFetcher = async ({ const getTransactionBySenderAndNonceFetcher =
provider, (provider: JsonRpcProvider) =>
async ({
network,
sender, sender,
nonce, nonce,
}: TransactionBySenderAndNonceKey): Promise<string | null | undefined> => { }: TransactionBySenderAndNonceKey): Promise<string | null | undefined> => {
if (nonce < 0) { if (nonce < 0) {
return undefined; return undefined;
} }
@ -551,18 +553,26 @@ export const getTransactionBySenderAndNonceFetcher = async ({
// Empty or success // Empty or success
return result; return result;
}; };
export const prefetchTransactionBySenderAndNonce = ( export const prefetchTransactionBySenderAndNonce = (
{ cache, mutate }: ReturnType<typeof useSWRConfig>, { mutate }: ReturnType<typeof useSWRConfig>,
provider: JsonRpcProvider, provider: JsonRpcProvider,
sender: ChecksummedAddress, sender: ChecksummedAddress,
nonce: number nonce: number
) => { ) => {
const key: TransactionBySenderAndNonceKey = { provider, sender, nonce }; const key: TransactionBySenderAndNonceKey = {
if (cache.get(key)) { network: provider.network.chainId,
mutate(key, getTransactionBySenderAndNonceFetcher(key)); sender,
nonce,
};
mutate(key, (curr: any) => {
if (curr) {
return curr;
} }
return getTransactionBySenderAndNonceFetcher(provider)(key);
});
// }
}; };
export const useTransactionBySenderAndNonce = ( export const useTransactionBySenderAndNonce = (
@ -576,9 +586,13 @@ export const useTransactionBySenderAndNonce = (
TransactionBySenderAndNonceKey | null TransactionBySenderAndNonceKey | null
>( >(
provider && sender && nonce !== undefined provider && sender && nonce !== undefined
? { provider, sender, nonce } ? {
network: provider.network.chainId,
sender,
nonce,
}
: null, : null,
getTransactionBySenderAndNonceFetcher getTransactionBySenderAndNonceFetcher(provider!)
); );
if (error) { if (error) {