这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/ChainSafe.Gaming.MetaMask.Unity/MetaMaskController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class MetaMaskController : MonoBehaviour
{
private bool isInitialized;

private TaskCompletionSource<string> connectionTcs;

/// Use this instead of <see cref="Debug"/>.
private ILogWriter logger;

Expand Down Expand Up @@ -56,7 +58,14 @@ public void Initialize(ILogWriter logWriter)
/// <returns>Connected account address.</returns>
public Task<string> Connect()
{
var taskCompletionSource = new TaskCompletionSource<string>();
if (connectionTcs != null && !connectionTcs.Task.IsCompleted)
{
connectionTcs.SetException(new Web3Exception("Metamask connection not completed."));

return connectionTcs.Task;
}

connectionTcs = new TaskCompletionSource<string>();

// Unsubscribe in case we're already subscribed from a previous login.
OnAccountConnected -= Connected;
Expand All @@ -68,9 +77,9 @@ void Connected(string address)
{
ConnectedAddress = address;

if (!taskCompletionSource.TrySetResult(ConnectedAddress))
if (!connectionTcs.TrySetResult(ConnectedAddress))
{
taskCompletionSource.SetException(new Web3Exception("Error setting connected account address."));
connectionTcs.SetException(new Web3Exception("Error setting connected account address."));
}
else
{
Expand All @@ -85,15 +94,15 @@ void Connected(string address)
}
else
{
logger.LogError("Metamask is not available, please install it first.");
connectionTcs.SetException(new Web3Exception("Metamask is not available, please install it first."));

// Unsubscribe to event.
OnAccountConnected -= Connected;

return null;
}

return taskCompletionSource.Task;
return connectionTcs.Task;
}

/// <summary>
Expand Down Expand Up @@ -194,7 +203,14 @@ public void ChainSelected(string chainId)
// Callback for displaying an error if operation fails.
private void DisplayError(string message)
{
logger.LogError(message);
if (connectionTcs != null && !connectionTcs.Task.IsCompleted)
{
connectionTcs.SetException(new Web3Exception(message));
}
else
{
logger.LogError(message);
}
}
}
}