Skip to content

Commit 9629a33

Browse files
committed
refactor: use async instead of .Result when it is called by delegates
Signed-off-by: leo <[email protected]>
1 parent 5339ade commit 9629a33

File tree

7 files changed

+67
-44
lines changed

7 files changed

+67
-44
lines changed

src/Models/AvatarManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void Start()
5151
LoadDefaultAvatar("[email protected]", "github.png");
5252
LoadDefaultAvatar("[email protected]", "unreal.png");
5353

54-
Task.Run(() =>
54+
Task.Run(async () =>
5555
{
5656
while (true)
5757
{
@@ -84,7 +84,7 @@ public void Start()
8484
{
8585
using var client = new HttpClient();
8686
client.Timeout = TimeSpan.FromSeconds(2);
87-
var rsp = client.GetAsync(url).Result;
87+
var rsp = await client.GetAsync(url);
8888
if (rsp.IsSuccessStatusCode)
8989
{
9090
using (var stream = rsp.Content.ReadAsStream())

src/ViewModels/EditRemote.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.ComponentModel.DataAnnotations;
22
using System.IO;
33
using System.Threading.Tasks;
4+
using Avalonia.Threading;
45

56
namespace SourceGit.ViewModels
67
{
@@ -53,7 +54,16 @@ public EditRemote(Repository repo, Models.Remote remote)
5354
_useSSH = Models.Remote.IsSSH(remote.URL);
5455

5556
if (_useSSH)
56-
SSHKey = new Commands.Config(repo.FullPath).GetAsync($"remote.{remote.Name}.sshkey").Result;
57+
{
58+
Task.Run(async () =>
59+
{
60+
var sshKey = await new Commands.Config(repo.FullPath)
61+
.GetAsync($"remote.{remote.Name}.sshkey")
62+
.ConfigureAwait(false);
63+
64+
Dispatcher.UIThread.Post(() => SSHKey = sshKey);
65+
});
66+
}
5767
}
5868

5969
public static ValidationResult ValidateRemoteName(string name, ValidationContext ctx)

src/ViewModels/Histories.cs

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.IO;
5+
using System.Threading.Tasks;
56

67
using Avalonia.Controls;
8+
using Avalonia.Threading;
79
using CommunityToolkit.Mvvm.ComponentModel;
810

911
namespace SourceGit.ViewModels
@@ -133,34 +135,20 @@ public Models.BisectState UpdateBisectInfo()
133135
public void NavigateTo(string commitSHA)
134136
{
135137
var commit = _commits.Find(x => x.SHA.StartsWith(commitSHA, StringComparison.Ordinal));
136-
if (commit == null)
137-
{
138-
AutoSelectedCommit = null;
139-
commit = new Commands.QuerySingleCommit(_repo.FullPath, commitSHA).GetResultAsync().Result;
140-
}
141-
else
142-
{
143-
AutoSelectedCommit = commit;
144-
NavigationId = _navigationId + 1;
145-
}
146-
147138
if (commit != null)
148139
{
149-
if (_detailContext is CommitDetail detail)
150-
{
151-
detail.Commit = commit;
152-
}
153-
else
154-
{
155-
var commitDetail = new CommitDetail(_repo, true);
156-
commitDetail.Commit = commit;
157-
DetailContext = commitDetail;
158-
}
140+
NavigateTo(commit);
141+
return;
159142
}
160-
else
143+
144+
Task.Run(async () =>
161145
{
162-
DetailContext = null;
163-
}
146+
var c = await new Commands.QuerySingleCommit(_repo.FullPath, commitSHA)
147+
.GetResultAsync()
148+
.ConfigureAwait(false);
149+
150+
Dispatcher.UIThread.Post(() => NavigateTo(c));
151+
});
164152
}
165153

166154
public void Select(IList commits)
@@ -297,6 +285,31 @@ public void CheckoutBranchByCommit(Models.Commit commit)
297285
}
298286
}
299287

288+
private void NavigateTo(Models.Commit commit)
289+
{
290+
AutoSelectedCommit = commit;
291+
292+
if (commit == null)
293+
{
294+
DetailContext = null;
295+
}
296+
else
297+
{
298+
NavigationId = _navigationId + 1;
299+
300+
if (_detailContext is CommitDetail detail)
301+
{
302+
detail.Commit = commit;
303+
}
304+
else
305+
{
306+
var commitDetail = new CommitDetail(_repo, true);
307+
commitDetail.Commit = commit;
308+
DetailContext = commitDetail;
309+
}
310+
}
311+
}
312+
300313
private Repository _repo = null;
301314
private bool _isLoading = true;
302315
private List<Models.Commit> _commits = new List<Models.Commit>();

src/ViewModels/Reword.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public string Message
1818
set => SetProperty(ref _message, value, true);
1919
}
2020

21-
public Reword(Repository repo, Models.Commit head)
21+
public Reword(Repository repo, Models.Commit head, string oldMessage)
2222
{
2323
_repo = repo;
24-
_oldMessage = new Commands.QueryCommitFullMessage(_repo.FullPath, head.SHA).GetResultAsync().Result;
24+
_oldMessage = oldMessage;
2525
_message = _oldMessage;
2626
Head = head;
2727
}

src/ViewModels/Squash.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public string Message
1717
set => SetProperty(ref _message, value, true);
1818
}
1919

20-
public Squash(Repository repo, Models.Commit target, string shaToGetPreferMessage)
20+
public Squash(Repository repo, Models.Commit target, string message)
2121
{
2222
_repo = repo;
23-
_message = new Commands.QueryCommitFullMessage(_repo.FullPath, shaToGetPreferMessage).GetResultAsync().Result;
23+
_message = message;
2424
Target = target;
2525
}
2626

src/Views/CommitMessagePresenter.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ private void ProcessHoverCommitLink(Models.InlineElement link)
241241
{
242242
var sha = link.Link;
243243

244-
// If we have already queried this SHA, just use it.
245244
if (_inlineCommits.TryGetValue(sha, out var exist))
246245
{
247246
ToolTip.SetTip(this, exist);
@@ -251,22 +250,18 @@ private void ProcessHoverCommitLink(Models.InlineElement link)
251250
var parentView = this.FindAncestorOfType<CommitBaseInfo>();
252251
if (parentView is { DataContext: ViewModels.CommitDetail detail })
253252
{
254-
// Record the SHA of current viewing commit in the CommitDetail panel to determine if it is changed after
255-
// asynchronous queries.
256253
var lastDetailCommit = detail.Commit.SHA;
257-
Task.Run(() =>
254+
Task.Run(async () =>
258255
{
259-
var c = detail.GetCommitAsync(sha).Result;
260-
Dispatcher.UIThread.Invoke(() =>
256+
var c = await detail.GetCommitAsync(sha);
257+
258+
Dispatcher.UIThread.Post(() =>
261259
{
262-
// Make sure the DataContext of CommitBaseInfo is not changed.
263260
var currentParent = this.FindAncestorOfType<CommitBaseInfo>();
264261
if (currentParent is { DataContext: ViewModels.CommitDetail currentDetail } &&
265262
currentDetail.Commit.SHA == lastDetailCommit)
266263
{
267264
_inlineCommits.TryAdd(sha, c);
268-
269-
// Make sure user still hovers the target SHA.
270265
if (_lastHover == link && c != null)
271266
ToolTip.SetTip(this, c);
272267
}

src/Views/Histories.axaml.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,14 @@ private ContextMenu CreateContextMenuForSingleCommit(ViewModels.Repository repo,
526526
var reword = new MenuItem();
527527
reword.Header = App.Text("CommitCM.Reword");
528528
reword.Icon = App.CreateMenuIcon("Icons.Edit");
529-
reword.Click += (_, e) =>
529+
reword.Click += async (_, e) =>
530530
{
531531
if (repo.CanCreatePopup())
532-
repo.ShowPopup(new ViewModels.Reword(repo, commit));
532+
{
533+
var message = await new Commands.QueryCommitFullMessage(repo.FullPath, commit.SHA).GetResultAsync();
534+
repo.ShowPopup(new ViewModels.Reword(repo, commit, message));
535+
}
536+
533537
e.Handled = true;
534538
};
535539
menu.Items.Add(reword);
@@ -538,13 +542,14 @@ private ContextMenu CreateContextMenuForSingleCommit(ViewModels.Repository repo,
538542
squash.Header = App.Text("CommitCM.Squash");
539543
squash.Icon = App.CreateMenuIcon("Icons.SquashIntoParent");
540544
squash.IsEnabled = commit.Parents.Count == 1;
541-
squash.Click += (_, e) =>
545+
squash.Click += async (_, e) =>
542546
{
543547
if (commit.Parents.Count == 1)
544548
{
549+
var message = await new Commands.QueryCommitFullMessage(repo.FullPath, commit.SHA).GetResultAsync();
545550
var parent = vm.Commits.Find(x => x.SHA.Equals(commit.Parents[0]));
546551
if (parent != null && repo.CanCreatePopup())
547-
repo.ShowPopup(new ViewModels.Squash(repo, parent, commit.SHA));
552+
repo.ShowPopup(new ViewModels.Squash(repo, parent, message));
548553
}
549554

550555
e.Handled = true;

0 commit comments

Comments
 (0)