fix: cancel prev searching request before starting a new one (#2197)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2026-03-17 17:06:30 +08:00
parent a04a64d05d
commit 668439a17d

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
@@ -105,6 +106,12 @@ namespace SourceGit.ViewModels
IsQuerying = true;
if (_cancellation is { IsCancellationRequested: false })
_cancellation.Cancel();
_cancellation = new();
var token = _cancellation.Token;
Task.Run(async () =>
{
var result = new List<Models.Commit>();
@@ -158,17 +165,23 @@ namespace SourceGit.ViewModels
Dispatcher.UIThread.Post(() =>
{
IsQuerying = false;
if (token.IsCancellationRequested)
return;
IsQuerying = false;
if (_repo.IsSearchingCommits)
Results = result;
});
});
}, token);
}
public void EndSearch()
{
if (_cancellation is { IsCancellationRequested: false })
_cancellation.Cancel();
_worktreeFiles = null;
IsQuerying = false;
Suggestions = null;
Results = null;
GC.Collect();
@@ -228,6 +241,7 @@ namespace SourceGit.ViewModels
}
private Repository _repo = null;
private CancellationTokenSource _cancellation = null;
private int _method = (int)Models.CommitSearchMethod.ByMessage;
private string _filter = string.Empty;
private bool _onlySearchCurrentBranch = false;