Prevent double initialization of wallet connect client (#10)
This commit is contained in:
parent
3adcdfa39c
commit
1f11e283d7
@ -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]);
|
||||||
|
Loading…
Reference in New Issue
Block a user