@@ -44,17 +44,26 @@ export function getMultichainClient<T extends RpcApi = DefaultRpcApi>({
4444 transport,
4545} : { transport : Transport } ) : MultichainApiClient < T > {
4646 let initializationPromise : Promise < void > | undefined = undefined ;
47+ let connectionPromise : Promise < void > | undefined = undefined ;
48+
49+ async function ensureConnected ( ) {
50+ if ( transport . isConnected ( ) ) {
51+ return ;
52+ }
53+
54+ if ( ! connectionPromise ) {
55+ connectionPromise = transport . connect ( ) ;
56+ }
57+ await connectionPromise ;
58+ }
4759
4860 async function ensureInitialized ( ) {
4961 if ( initializationPromise ) {
5062 return await initializationPromise ;
5163 }
5264
5365 initializationPromise = ( async ( ) => {
54- // Ensure connected first
55- if ( ! transport . isConnected ( ) ) {
56- await transport . connect ( ) ;
57- }
66+ await ensureConnected ( ) ;
5867
5968 // Use withRetry to handle the case where the Multichain API requests don't resolve on page load (cf. https://github.com/MetaMask/metamask-mobile/issues/16550)
6069 await withRetry ( ( ) => transport . request ( { method : 'wallet_getSession' } ) ) ;
@@ -64,7 +73,7 @@ export function getMultichainClient<T extends RpcApi = DefaultRpcApi>({
6473 }
6574
6675 // Try to connect to the transport on client creation to reduce latency when first used
67- void ensureInitialized ( ) ;
76+ void ensureConnected ( ) ;
6877
6978 return {
7079 createSession : async ( params : CreateSessionParams < T > ) : Promise < SessionData > => {
@@ -78,6 +87,7 @@ export function getMultichainClient<T extends RpcApi = DefaultRpcApi>({
7887 revokeSession : async ( ) => {
7988 await ensureInitialized ( ) ;
8089 initializationPromise = undefined ;
90+ connectionPromise = undefined ;
8191 await request ( { transport, method : 'wallet_revokeSession' } ) ;
8292 await transport . disconnect ( ) ;
8393 } ,
0 commit comments