Skip to content

Commit 25afcba

Browse files
authored
refactor: auto dispatch notifications to UI thread in Launcher (#1489)
1 parent 19d8224 commit 25afcba

12 files changed

+27
-52
lines changed

src/Commands/Command.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
using System.Text.RegularExpressions;
66
using System.Threading;
77

8-
using Avalonia.Threading;
9-
108
namespace SourceGit.Commands
119
{
1210
public partial class Command
@@ -68,7 +66,7 @@ public bool Exec()
6866
catch (Exception e)
6967
{
7068
if (RaiseError)
71-
Dispatcher.UIThread.Post(() => App.RaiseException(Context, e.Message));
69+
App.RaiseException(Context, e.Message);
7270

7371
Log?.AppendLine(string.Empty);
7472
return false;
@@ -96,7 +94,7 @@ public bool Exec()
9694
{
9795
var errMsg = string.Join("\n", errs).Trim();
9896
if (!string.IsNullOrEmpty(errMsg))
99-
Dispatcher.UIThread.Post(() => App.RaiseException(Context, errMsg));
97+
App.RaiseException(Context, errMsg);
10098
}
10199

102100
return false;

src/Commands/Discard.cs

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

5-
using Avalonia.Threading;
6-
75
namespace SourceGit.Commands
86
{
97
public static class Discard
@@ -36,10 +34,7 @@ public static void All(string repo, bool includeIgnored, Models.ICommandLog log)
3634
}
3735
catch (Exception e)
3836
{
39-
Dispatcher.UIThread.Invoke(() =>
40-
{
41-
App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}");
42-
});
37+
App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}");
4338
}
4439

4540
new Reset(repo, "HEAD", "--hard") { Log = log }.Exec();
@@ -78,10 +73,7 @@ public static void Changes(string repo, List<Models.Change> changes, Models.ICom
7873
}
7974
catch (Exception e)
8075
{
81-
Dispatcher.UIThread.Invoke(() =>
82-
{
83-
App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}");
84-
});
76+
App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}");
8577
}
8678

8779
if (restores.Count > 0)

src/Commands/GenerateCommitMessage.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using System.Text;
44
using System.Threading;
55

6-
using Avalonia.Threading;
7-
86
namespace SourceGit.Commands
97
{
108
/// <summary>
@@ -86,7 +84,7 @@ public void Exec()
8684
}
8785
catch (Exception e)
8886
{
89-
Dispatcher.UIThread.Post(() => App.RaiseException(_repo, $"Failed to generate commit message: {e}"));
87+
App.RaiseException(_repo, $"Failed to generate commit message: {e}");
9088
}
9189
}
9290

src/Commands/GitFlow.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Text;
2-
using Avalonia.Threading;
32

43
namespace SourceGit.Commands
54
{
@@ -43,7 +42,7 @@ public static bool Start(string repo, Models.GitFlowBranchType type, string name
4342
start.Args = $"flow hotfix start {name}";
4443
break;
4544
default:
46-
Dispatcher.UIThread.Invoke(() => App.RaiseException(repo, "Bad git-flow branch type!!!"));
45+
App.RaiseException(repo, "Bad git-flow branch type!!!");
4746
return false;
4847
}
4948

@@ -68,7 +67,7 @@ public static bool Finish(string repo, Models.GitFlowBranchType type, string nam
6867
builder.Append("hotfix");
6968
break;
7069
default:
71-
Dispatcher.UIThread.Invoke(() => App.RaiseException(repo, "Bad git-flow branch type!!!"));
70+
App.RaiseException(repo, "Bad git-flow branch type!!!");
7271
return false;
7372
}
7473

src/Commands/MergeTool.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System.IO;
22

3-
using Avalonia.Threading;
4-
53
namespace SourceGit.Commands
64
{
75
public static class MergeTool
@@ -24,14 +22,14 @@ public static bool OpenForMerge(string repo, int toolType, string toolPath, stri
2422

2523
if (!File.Exists(toolPath))
2624
{
27-
Dispatcher.UIThread.Post(() => App.RaiseException(repo, $"Can NOT find external merge tool in '{toolPath}'!"));
25+
App.RaiseException(repo, $"Can NOT find external merge tool in '{toolPath}'!");
2826
return false;
2927
}
3028

3129
var supported = Models.ExternalMerger.Supported.Find(x => x.Type == toolType);
3230
if (supported == null)
3331
{
34-
Dispatcher.UIThread.Post(() => App.RaiseException(repo, "Invalid merge tool in preference setting!"));
32+
App.RaiseException(repo, "Invalid merge tool in preference setting!");
3533
return false;
3634
}
3735

@@ -54,14 +52,14 @@ public static bool OpenForDiff(string repo, int toolType, string toolPath, Model
5452

5553
if (!File.Exists(toolPath))
5654
{
57-
Dispatcher.UIThread.Invoke(() => App.RaiseException(repo, $"Can NOT find external diff tool in '{toolPath}'!"));
55+
App.RaiseException(repo, $"Can NOT find external diff tool in '{toolPath}'!");
5856
return false;
5957
}
6058

6159
var supported = Models.ExternalMerger.Supported.Find(x => x.Type == toolType);
6260
if (supported == null)
6361
{
64-
Dispatcher.UIThread.Post(() => App.RaiseException(repo, "Invalid merge tool in preference setting!"));
62+
App.RaiseException(repo, "Invalid merge tool in preference setting!");
6563
return false;
6664
}
6765

src/Commands/QueryLocalChanges.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using System.Collections.Generic;
33
using System.Text.RegularExpressions;
44

5-
using Avalonia.Threading;
6-
75
namespace SourceGit.Commands
86
{
97
public partial class QueryLocalChanges : Command
@@ -25,7 +23,7 @@ public QueryLocalChanges(string repo, bool includeUntracked = true)
2523
var rs = ReadToEnd();
2624
if (!rs.IsSuccess)
2725
{
28-
Dispatcher.UIThread.Post(() => App.RaiseException(Context, rs.StdErr));
26+
App.RaiseException(Context, rs.StdErr);
2927
return outs;
3028
}
3129

src/Commands/SaveChangesAsPatch.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using System.Diagnostics;
44
using System.IO;
55

6-
using Avalonia.Threading;
7-
86
namespace SourceGit.Commands
97
{
108
public static class SaveChangesAsPatch
@@ -74,10 +72,7 @@ private static bool ProcessSingleChange(string repo, Models.DiffOption opt, File
7472
}
7573
catch (Exception e)
7674
{
77-
Dispatcher.UIThread.Invoke(() =>
78-
{
79-
App.RaiseException(repo, "Save change to patch failed: " + e.Message);
80-
});
75+
App.RaiseException(repo, "Save change to patch failed: " + e.Message);
8176
return false;
8277
}
8378
}

src/Commands/SaveRevisionFile.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using System.Diagnostics;
33
using System.IO;
44

5-
using Avalonia.Threading;
6-
75
namespace SourceGit.Commands
86
{
97
public static class SaveRevisionFile
@@ -53,10 +51,7 @@ private static void ExecCmd(string repo, string args, string outputFile, Stream
5351
}
5452
catch (Exception e)
5553
{
56-
Dispatcher.UIThread.Invoke(() =>
57-
{
58-
App.RaiseException(repo, "Save file failed: " + e.Message);
59-
});
54+
App.RaiseException(repo, "Save file failed: " + e.Message);
6055
}
6156
}
6257
}

src/Commands/UnstageChangesForAmend.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using System.Diagnostics;
44
using System.Text;
55

6-
using Avalonia.Threading;
7-
86
namespace SourceGit.Commands
97
{
108
public class UnstageChangesForAmend
@@ -75,16 +73,13 @@ public bool Exec()
7573
proc.Close();
7674

7775
if (!rs)
78-
Dispatcher.UIThread.Invoke(() => App.RaiseException(_repo, err));
76+
App.RaiseException(_repo, err);
7977

8078
return rs;
8179
}
8280
catch (Exception e)
8381
{
84-
Dispatcher.UIThread.Invoke(() =>
85-
{
86-
App.RaiseException(_repo, "Failed to unstage changes: " + e.Message);
87-
});
82+
App.RaiseException(_repo, "Failed to unstage changes: " + e.Message);
8883
return false;
8984
}
9085
}

src/ViewModels/ExecuteCustomAction.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ private void Run(string args)
237237
}
238238
catch (Exception e)
239239
{
240-
CallUIThread(() => App.RaiseException(_repo.FullPath, e.Message));
240+
App.RaiseException(_repo.FullPath, e.Message);
241241
}
242242
}
243243

@@ -284,12 +284,12 @@ private void RunAndWait(string args, Models.ICommandLog log)
284284
{
285285
var errMsg = builder.ToString().Trim();
286286
if (!string.IsNullOrEmpty(errMsg))
287-
CallUIThread(() => App.RaiseException(_repo.FullPath, errMsg));
287+
App.RaiseException(_repo.FullPath, errMsg);
288288
}
289289
}
290290
catch (Exception e)
291291
{
292-
CallUIThread(() => App.RaiseException(_repo.FullPath, e.Message));
292+
App.RaiseException(_repo.FullPath, e.Message);
293293
}
294294

295295
proc.Close();

0 commit comments

Comments
 (0)