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

View File

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