Skip to content

Commit 291e32e

Browse files
committed
enhance: cancel the running event when a new same refresh event occurs (#1760)
Signed-off-by: leo <[email protected]>
1 parent d72e26a commit 291e32e

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed

src/ViewModels/Repository.cs

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,17 @@ public void Close()
578578
JsonSerializer.Serialize(stream, _settings, JsonCodeGen.Default.RepositorySettings);
579579
}
580580

581+
if (_cancellationRefreshBranches is { IsCancellationRequested: false })
582+
_cancellationRefreshBranches.Cancel();
583+
if (_cancellationRefreshTags is { IsCancellationRequested: false })
584+
_cancellationRefreshTags.Cancel();
585+
if (_cancellationRefreshWorkingCopyChanges is { IsCancellationRequested: false })
586+
_cancellationRefreshWorkingCopyChanges.Cancel();
587+
if (_cancellationRefreshCommits is { IsCancellationRequested: false })
588+
_cancellationRefreshCommits.Cancel();
589+
if (_cancellationRefreshStashes is { IsCancellationRequested: false })
590+
_cancellationRefreshStashes.Cancel();
591+
581592
_autoFetchTimer.Dispose();
582593
_autoFetchTimer = null;
583594

@@ -1170,6 +1181,12 @@ public bool MayHaveSubmodules()
11701181

11711182
public void RefreshBranches()
11721183
{
1184+
if (_cancellationRefreshBranches is { IsCancellationRequested: false })
1185+
_cancellationRefreshBranches.Cancel();
1186+
1187+
_cancellationRefreshBranches = new CancellationTokenSource();
1188+
var token = _cancellationRefreshBranches.Token;
1189+
11731190
Task.Run(async () =>
11741191
{
11751192
var branches = await new Commands.QueryBranches(FullPath).GetResultAsync().ConfigureAwait(false);
@@ -1178,6 +1195,9 @@ public void RefreshBranches()
11781195

11791196
Dispatcher.UIThread.Invoke(() =>
11801197
{
1198+
if (token.IsCancellationRequested)
1199+
return;
1200+
11811201
Remotes = remotes;
11821202
Branches = branches;
11831203
CurrentBranch = branches.Find(x => x.IsCurrent);
@@ -1198,7 +1218,7 @@ public void RefreshBranches()
11981218
var hasPendingPullOrPush = CurrentBranch?.TrackStatus.IsVisible ?? false;
11991219
GetOwnerPage()?.ChangeDirtyState(Models.DirtyState.HasPendingPullOrPush, !hasPendingPullOrPush);
12001220
});
1201-
});
1221+
}, token);
12021222
}
12031223

12041224
public void RefreshWorktrees()
@@ -1228,19 +1248,34 @@ public void RefreshWorktrees()
12281248

12291249
public void RefreshTags()
12301250
{
1251+
if (_cancellationRefreshTags is { IsCancellationRequested: false })
1252+
_cancellationRefreshTags.Cancel();
1253+
1254+
_cancellationRefreshTags = new CancellationTokenSource();
1255+
var token = _cancellationRefreshTags.Token;
1256+
12311257
Task.Run(async () =>
12321258
{
12331259
var tags = await new Commands.QueryTags(FullPath).GetResultAsync().ConfigureAwait(false);
12341260
Dispatcher.UIThread.Invoke(() =>
12351261
{
1262+
if (token.IsCancellationRequested)
1263+
return;
1264+
12361265
Tags = tags;
12371266
VisibleTags = BuildVisibleTags();
12381267
});
1239-
});
1268+
}, token);
12401269
}
12411270

12421271
public void RefreshCommits()
12431272
{
1273+
if (_cancellationRefreshCommits is { IsCancellationRequested: false })
1274+
_cancellationRefreshCommits.Cancel();
1275+
1276+
_cancellationRefreshCommits = new CancellationTokenSource();
1277+
var token = _cancellationRefreshCommits.Token;
1278+
12441279
Task.Run(async () =>
12451280
{
12461281
await Dispatcher.UIThread.InvokeAsync(() => _histories.IsLoading = true);
@@ -1273,6 +1308,9 @@ public void RefreshCommits()
12731308

12741309
Dispatcher.UIThread.Invoke(() =>
12751310
{
1311+
if (token.IsCancellationRequested)
1312+
return;
1313+
12761314
if (_histories != null)
12771315
{
12781316
_histories.IsLoading = false;
@@ -1287,7 +1325,7 @@ public void RefreshCommits()
12871325

12881326
_navigateToCommitDelayed = string.Empty;
12891327
});
1290-
});
1328+
}, token);
12911329
}
12921330

12931331
public void RefreshSubmodules()
@@ -1352,43 +1390,61 @@ public void RefreshWorkingCopyChanges()
13521390
if (IsBare)
13531391
return;
13541392

1393+
if (_cancellationRefreshWorkingCopyChanges is { IsCancellationRequested: false })
1394+
_cancellationRefreshWorkingCopyChanges.Cancel();
1395+
1396+
_cancellationRefreshWorkingCopyChanges = new CancellationTokenSource();
1397+
var token = _cancellationRefreshWorkingCopyChanges.Token;
1398+
13551399
Task.Run(async () =>
13561400
{
13571401
var changes = await new Commands.QueryLocalChanges(FullPath, _settings.IncludeUntrackedInLocalChanges)
13581402
.GetResultAsync()
13591403
.ConfigureAwait(false);
13601404

1361-
if (_workingCopy == null)
1405+
if (_workingCopy == null || token.IsCancellationRequested)
13621406
return;
13631407

13641408
changes.Sort((l, r) => Models.NumericSort.Compare(l.Path, r.Path));
1365-
_workingCopy.SetData(changes);
1409+
_workingCopy.SetData(changes, token);
13661410

13671411
Dispatcher.UIThread.Invoke(() =>
13681412
{
1413+
if (token.IsCancellationRequested)
1414+
return;
1415+
13691416
LocalChangesCount = changes.Count;
13701417
OnPropertyChanged(nameof(InProgressContext));
13711418
GetOwnerPage()?.ChangeDirtyState(Models.DirtyState.HasLocalChanges, changes.Count == 0);
13721419
});
1373-
});
1420+
}, token);
13741421
}
13751422

13761423
public void RefreshStashes()
13771424
{
13781425
if (IsBare)
13791426
return;
13801427

1428+
if (_cancellationRefreshStashes is { IsCancellationRequested: false })
1429+
_cancellationRefreshStashes.Cancel();
1430+
1431+
_cancellationRefreshStashes = new CancellationTokenSource();
1432+
var token = _cancellationRefreshStashes.Token;
1433+
13811434
Task.Run(async () =>
13821435
{
13831436
var stashes = await new Commands.QueryStashes(FullPath).GetResultAsync().ConfigureAwait(false);
13841437
Dispatcher.UIThread.Invoke(() =>
13851438
{
1439+
if (token.IsCancellationRequested)
1440+
return;
1441+
13861442
if (_stashesPage != null)
13871443
_stashesPage.Stashes = stashes;
13881444

13891445
StashesCount = stashes.Count;
13901446
});
1391-
});
1447+
}, token);
13921448
}
13931449

13941450
public void ToggleHistoryShowFlag(Models.HistoryShowFlags flag)
@@ -2021,6 +2077,7 @@ private void FetchInBackground(object sender)
20212077
private object _visibleTags = null;
20222078
private List<Models.Submodule> _submodules = [];
20232079
private object _visibleSubmodules = null;
2080+
private string _navigateToCommitDelayed = string.Empty;
20242081

20252082
private bool _isAutoFetching = false;
20262083
private Timer _autoFetchTimer = null;
@@ -2029,6 +2086,10 @@ private void FetchInBackground(object sender)
20292086
private Models.BisectState _bisectState = Models.BisectState.None;
20302087
private bool _isBisectCommandRunning = false;
20312088

2032-
private string _navigateToCommitDelayed = string.Empty;
2089+
private CancellationTokenSource _cancellationRefreshBranches = null;
2090+
private CancellationTokenSource _cancellationRefreshTags = null;
2091+
private CancellationTokenSource _cancellationRefreshWorkingCopyChanges = null;
2092+
private CancellationTokenSource _cancellationRefreshCommits = null;
2093+
private CancellationTokenSource _cancellationRefreshStashes = null;
20332094
}
20342095
}

src/ViewModels/WorkingCopy.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Threading;
45
using System.Threading.Tasks;
56

67
using Avalonia.Threading;
@@ -246,15 +247,17 @@ public void Dispose()
246247
_commitMessage = string.Empty;
247248
}
248249

249-
public void SetData(List<Models.Change> changes)
250+
public void SetData(List<Models.Change> changes, CancellationToken cancellationToken)
250251
{
251252
if (!IsChanged(_cached, changes))
252253
{
253254
// Just force refresh selected changes.
254255
Dispatcher.UIThread.Invoke(() =>
255256
{
256-
HasUnsolvedConflicts = _cached.Find(x => x.IsConflicted) != null;
257+
if (cancellationToken.IsCancellationRequested)
258+
return;
257259

260+
HasUnsolvedConflicts = _cached.Find(x => x.IsConflicted) != null;
258261
UpdateDetail();
259262
UpdateInProgressState();
260263
});
@@ -308,6 +311,9 @@ public void SetData(List<Models.Change> changes)
308311

309312
Dispatcher.UIThread.Invoke(() =>
310313
{
314+
if (cancellationToken.IsCancellationRequested)
315+
return;
316+
311317
_isLoadingData = true;
312318
HasUnsolvedConflicts = hasConflict;
313319
VisibleUnstaged = visibleUnstaged;

0 commit comments

Comments
 (0)