Skip to content

Commit 646dcef

Browse files
committed
Handle Reown wallet modal closure during connection
Added logic to detect when the Reown wallet modal is closed before a connection is established, treating it as a user cancellation. Updated the exception message for connection failures to reflect both timeout and user cancellation scenarios.
1 parent 950fad9 commit 646dcef

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

Assets/Thirdweb/Runtime/Unity/Wallets/Reown/ReownWallet.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ bool tryResumeSession
9090

9191
if (!connected)
9292
{
93-
throw new TimeoutException($"Reown connection timed out after {connectionTimeout.TotalSeconds} seconds.");
93+
throw new Exception("Reown wallet connection either timed out or was cancelled by the user.");
9494
}
9595

9696
ThirdwebDebug.Log("Reown wallet connected.");
@@ -291,7 +291,38 @@ void OnAccountConnected(object sender, AccountConnectedEventArgs args)
291291
}
292292
}
293293

294+
async void OnModalOpenStateChanged(object sender, ModalOpenStateChangedEventArgs args)
295+
{
296+
if (!args.IsOpen && !connectionEstablished && !connectedTcs.Task.IsCompleted)
297+
{
298+
// Modal closes after successful connection, doing this to avoid race condition
299+
const int maxWaitMs = 2000;
300+
const int pollIntervalMs = 100;
301+
var elapsed = 0;
302+
303+
while (elapsed < maxWaitMs && !connectionEstablished && !connectedTcs.Task.IsCompleted)
304+
{
305+
await ThirdwebTask.Delay(pollIntervalMs);
306+
elapsed += pollIntervalMs;
307+
308+
if (AppKit.ConnectorController.IsAccountConnected)
309+
{
310+
// Connection established, AccountConnected event will handle it
311+
return;
312+
}
313+
}
314+
315+
// At this point user likely cancelled
316+
if (!connectionEstablished && !connectedTcs.Task.IsCompleted)
317+
{
318+
ThirdwebDebug.LogWarning("Reown modal closed before connection was established.");
319+
connectedTcs.SetResult(false);
320+
}
321+
}
322+
}
323+
294324
AppKit.AccountConnected += OnAccountConnected;
325+
AppKit.ModalController.OpenStateChanged += OnModalOpenStateChanged;
295326

296327
try
297328
{
@@ -325,6 +356,7 @@ void OnAccountConnected(object sender, AccountConnectedEventArgs args)
325356
finally
326357
{
327358
AppKit.AccountConnected -= OnAccountConnected;
359+
AppKit.ModalController.OpenStateChanged -= OnModalOpenStateChanged;
328360
if (!connectionEstablished)
329361
{
330362
AppKit.CloseModal();

0 commit comments

Comments
 (0)