Skip to content

Commit 2ae0a38

Browse files
committed
SDK: only attempt auto connect once
1 parent 7306818 commit 2ae0a38

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

.changeset/odd-rooms-sleep.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Only attempt autoconnect once

packages/thirdweb/src/wallets/connection/autoConnectCore.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,7 @@ import {
1919
import type { WalletId } from "../wallet-types.js";
2020
import type { AutoConnectProps } from "./types.js";
2121

22-
/**
23-
* @internal
24-
*/
25-
export const autoConnectCore = async ({
26-
storage,
27-
props,
28-
createWalletFn,
29-
manager,
30-
connectOverride,
31-
getInstalledWallets,
32-
setLastAuthProvider,
33-
}: {
22+
type AutoConnectCoreProps = {
3423
storage: AsyncStorage;
3524
props: AutoConnectProps & { wallets: Wallet[] };
3625
createWalletFn: (id: WalletId) => Wallet;
@@ -43,7 +32,39 @@ export const autoConnectCore = async ({
4332
authProvider: AuthArgsType["strategy"],
4433
storage: AsyncStorage,
4534
) => Promise<void>;
46-
}): Promise<boolean> => {
35+
};
36+
37+
let lastAutoConnectionResultPromise: Promise<boolean> | undefined = undefined;
38+
39+
/**
40+
* @internal
41+
*/
42+
export const autoConnectCore = async (props: AutoConnectCoreProps) => {
43+
// if an auto connect was attempted already
44+
if (lastAutoConnectionResultPromise) {
45+
// wait for its resolution
46+
const lastResult = await lastAutoConnectionResultPromise;
47+
// if it was successful, return true
48+
// if not continue with the new auto connect
49+
if (lastResult) {
50+
return true;
51+
}
52+
}
53+
54+
const resultPromise = _autoConnectCore(props);
55+
lastAutoConnectionResultPromise = resultPromise;
56+
return resultPromise;
57+
};
58+
59+
const _autoConnectCore = async ({
60+
storage,
61+
props,
62+
createWalletFn,
63+
manager,
64+
connectOverride,
65+
getInstalledWallets,
66+
setLastAuthProvider,
67+
}: AutoConnectCoreProps): Promise<boolean> => {
4768
const { wallets, onConnect } = props;
4869
const timeout = props.timeout ?? 15000;
4970

0 commit comments

Comments
 (0)