Prevent double initialization of wallet connect client (#10)

This commit is contained in:
shreerang6921 2024-04-24 17:59:55 +05:30 committed by GitHub
parent 3adcdfa39c
commit 1f11e283d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,6 +6,7 @@ import React, {
useState,
useEffect,
useCallback,
useRef,
} from "react";
import { SnackbarProvider, enqueueSnackbar } from "notistack";
import { useNavigate } from "react-router-dom";
@ -46,6 +47,7 @@ export const WalletConnectProvider = ({
}) => {
const [signClient, setSignClient] = useState<SignClient>();
const [session, setSession] = useState<SessionTypes.Struct | null>(null);
const isSignClientInitializing = useRef<boolean>(false);
const navigate = useNavigate()
const disconnect = useCallback(async () => {
@ -85,6 +87,7 @@ export const WalletConnectProvider = ({
);
const createClient = useCallback(async () => {
isSignClientInitializing.current = true;
const signClient = await SignClient.init({
projectId: PROJECT_ID,
metadata: {
@ -98,6 +101,7 @@ export const WalletConnectProvider = ({
setSignClient(signClient);
await subscribeToEvents(signClient);
await checkPersistedState(signClient);
isSignClientInitializing.current = false;
}, [checkPersistedState, subscribeToEvents])
const connect = async () => {
@ -136,7 +140,7 @@ export const WalletConnectProvider = ({
};
useEffect(() => {
if (!signClient) {
if (!signClient && !isSignClientInitializing.current) {
createClient();
}
}, [signClient, createClient]);