refactor: use async instead of .Result when it is called by delegates

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-07-23 20:22:05 +08:00
parent 5339adec65
commit 9629a339dd
7 changed files with 67 additions and 44 deletions

View File

@@ -51,7 +51,7 @@ namespace SourceGit.Models
LoadDefaultAvatar("noreply@github.com", "github.png");
LoadDefaultAvatar("unrealbot@epicgames.com", "unreal.png");
Task.Run(() =>
Task.Run(async () =>
{
while (true)
{
@@ -84,7 +84,7 @@ namespace SourceGit.Models
{
using var client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(2);
var rsp = client.GetAsync(url).Result;
var rsp = await client.GetAsync(url);
if (rsp.IsSuccessStatusCode)
{
using (var stream = rsp.Content.ReadAsStream())

View File

@@ -1,6 +1,7 @@
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Threading.Tasks;
using Avalonia.Threading;
namespace SourceGit.ViewModels
{
@@ -53,7 +54,16 @@ namespace SourceGit.ViewModels
_useSSH = Models.Remote.IsSSH(remote.URL);
if (_useSSH)
SSHKey = new Commands.Config(repo.FullPath).GetAsync($"remote.{remote.Name}.sshkey").Result;
{
Task.Run(async () =>
{
var sshKey = await new Commands.Config(repo.FullPath)
.GetAsync($"remote.{remote.Name}.sshkey")
.ConfigureAwait(false);
Dispatcher.UIThread.Post(() => SSHKey = sshKey);
});
}
}
public static ValidationResult ValidateRemoteName(string name, ValidationContext ctx)

View File

@@ -2,8 +2,10 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
@@ -133,34 +135,20 @@ namespace SourceGit.ViewModels
public void NavigateTo(string commitSHA)
{
var commit = _commits.Find(x => x.SHA.StartsWith(commitSHA, StringComparison.Ordinal));
if (commit == null)
{
AutoSelectedCommit = null;
commit = new Commands.QuerySingleCommit(_repo.FullPath, commitSHA).GetResultAsync().Result;
}
else
{
AutoSelectedCommit = commit;
NavigationId = _navigationId + 1;
}
if (commit != null)
{
if (_detailContext is CommitDetail detail)
{
detail.Commit = commit;
}
else
{
var commitDetail = new CommitDetail(_repo, true);
commitDetail.Commit = commit;
DetailContext = commitDetail;
}
NavigateTo(commit);
return;
}
else
Task.Run(async () =>
{
DetailContext = null;
}
var c = await new Commands.QuerySingleCommit(_repo.FullPath, commitSHA)
.GetResultAsync()
.ConfigureAwait(false);
Dispatcher.UIThread.Post(() => NavigateTo(c));
});
}
public void Select(IList commits)
@@ -297,6 +285,31 @@ namespace SourceGit.ViewModels
}
}
private void NavigateTo(Models.Commit commit)
{
AutoSelectedCommit = commit;
if (commit == null)
{
DetailContext = null;
}
else
{
NavigationId = _navigationId + 1;
if (_detailContext is CommitDetail detail)
{
detail.Commit = commit;
}
else
{
var commitDetail = new CommitDetail(_repo, true);
commitDetail.Commit = commit;
DetailContext = commitDetail;
}
}
}
private Repository _repo = null;
private bool _isLoading = true;
private List<Models.Commit> _commits = new List<Models.Commit>();

View File

@@ -18,10 +18,10 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _message, value, true);
}
public Reword(Repository repo, Models.Commit head)
public Reword(Repository repo, Models.Commit head, string oldMessage)
{
_repo = repo;
_oldMessage = new Commands.QueryCommitFullMessage(_repo.FullPath, head.SHA).GetResultAsync().Result;
_oldMessage = oldMessage;
_message = _oldMessage;
Head = head;
}

View File

@@ -17,10 +17,10 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _message, value, true);
}
public Squash(Repository repo, Models.Commit target, string shaToGetPreferMessage)
public Squash(Repository repo, Models.Commit target, string message)
{
_repo = repo;
_message = new Commands.QueryCommitFullMessage(_repo.FullPath, shaToGetPreferMessage).GetResultAsync().Result;
_message = message;
Target = target;
}

View File

@@ -241,7 +241,6 @@ namespace SourceGit.Views
{
var sha = link.Link;
// If we have already queried this SHA, just use it.
if (_inlineCommits.TryGetValue(sha, out var exist))
{
ToolTip.SetTip(this, exist);
@@ -251,22 +250,18 @@ namespace SourceGit.Views
var parentView = this.FindAncestorOfType<CommitBaseInfo>();
if (parentView is { DataContext: ViewModels.CommitDetail detail })
{
// Record the SHA of current viewing commit in the CommitDetail panel to determine if it is changed after
// asynchronous queries.
var lastDetailCommit = detail.Commit.SHA;
Task.Run(() =>
Task.Run(async () =>
{
var c = detail.GetCommitAsync(sha).Result;
Dispatcher.UIThread.Invoke(() =>
var c = await detail.GetCommitAsync(sha);
Dispatcher.UIThread.Post(() =>
{
// Make sure the DataContext of CommitBaseInfo is not changed.
var currentParent = this.FindAncestorOfType<CommitBaseInfo>();
if (currentParent is { DataContext: ViewModels.CommitDetail currentDetail } &&
currentDetail.Commit.SHA == lastDetailCommit)
{
_inlineCommits.TryAdd(sha, c);
// Make sure user still hovers the target SHA.
if (_lastHover == link && c != null)
ToolTip.SetTip(this, c);
}

View File

@@ -526,10 +526,14 @@ namespace SourceGit.Views
var reword = new MenuItem();
reword.Header = App.Text("CommitCM.Reword");
reword.Icon = App.CreateMenuIcon("Icons.Edit");
reword.Click += (_, e) =>
reword.Click += async (_, e) =>
{
if (repo.CanCreatePopup())
repo.ShowPopup(new ViewModels.Reword(repo, commit));
{
var message = await new Commands.QueryCommitFullMessage(repo.FullPath, commit.SHA).GetResultAsync();
repo.ShowPopup(new ViewModels.Reword(repo, commit, message));
}
e.Handled = true;
};
menu.Items.Add(reword);
@@ -538,13 +542,14 @@ namespace SourceGit.Views
squash.Header = App.Text("CommitCM.Squash");
squash.Icon = App.CreateMenuIcon("Icons.SquashIntoParent");
squash.IsEnabled = commit.Parents.Count == 1;
squash.Click += (_, e) =>
squash.Click += async (_, e) =>
{
if (commit.Parents.Count == 1)
{
var message = await new Commands.QueryCommitFullMessage(repo.FullPath, commit.SHA).GetResultAsync();
var parent = vm.Commits.Find(x => x.SHA.Equals(commit.Parents[0]));
if (parent != null && repo.CanCreatePopup())
repo.ShowPopup(new ViewModels.Squash(repo, parent, commit.SHA));
repo.ShowPopup(new ViewModels.Squash(repo, parent, message));
}
e.Handled = true;