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