Skip to content

Commit 668439a

Browse files
committed
fix: cancel prev searching request before starting a new one (#2197)
Signed-off-by: leo <longshuang@msn.cn>
1 parent a04a64d commit 668439a

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/ViewModels/SearchCommitContext.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using Avalonia.Threading;
56
using CommunityToolkit.Mvvm.ComponentModel;
@@ -105,6 +106,12 @@ public void StartSearch()
105106

106107
IsQuerying = true;
107108

109+
if (_cancellation is { IsCancellationRequested: false })
110+
_cancellation.Cancel();
111+
112+
_cancellation = new();
113+
var token = _cancellation.Token;
114+
108115
Task.Run(async () =>
109116
{
110117
var result = new List<Models.Commit>();
@@ -158,17 +165,23 @@ public void StartSearch()
158165

159166
Dispatcher.UIThread.Post(() =>
160167
{
161-
IsQuerying = false;
168+
if (token.IsCancellationRequested)
169+
return;
162170

171+
IsQuerying = false;
163172
if (_repo.IsSearchingCommits)
164173
Results = result;
165174
});
166-
});
175+
}, token);
167176
}
168177

169178
public void EndSearch()
170179
{
180+
if (_cancellation is { IsCancellationRequested: false })
181+
_cancellation.Cancel();
182+
171183
_worktreeFiles = null;
184+
IsQuerying = false;
172185
Suggestions = null;
173186
Results = null;
174187
GC.Collect();
@@ -228,6 +241,7 @@ private void UpdateSuggestions()
228241
}
229242

230243
private Repository _repo = null;
244+
private CancellationTokenSource _cancellation = null;
231245
private int _method = (int)Models.CommitSearchMethod.ByMessage;
232246
private string _filter = string.Empty;
233247
private bool _onlySearchCurrentBranch = false;

0 commit comments

Comments
 (0)