Skip to content

Fix result missing + not updating issues #3513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 7, 2025
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions Flow.Launcher/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
private Query _lastQuery;
private bool _lastIsHomeQuery;
private string _queryTextBeforeLeaveResults;
private string _ignoredQueryText = null;
private string _ignoredQueryText; // Used to ignore query text change when switching between context menu and query results

private readonly FlowLauncherJsonStorage<History> _historyItemsStorage;
private readonly FlowLauncherJsonStorage<UserSelectedRecord> _userSelectedRecordStorage;
Expand All @@ -46,6 +46,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
private readonly TopMostRecord _topMostRecord;

private CancellationTokenSource _updateSource; // Used to cancel old query flows
private CancellationToken _updateToken; // Used to avoid ObjectDisposedException of _updateSource.Token

private ChannelWriter<ResultsForUpdate> _resultsUpdateChannelWriter;
private Task _resultsViewUpdateTask;
Expand All @@ -67,6 +68,9 @@ public MainViewModel()
_queryTextBeforeLeaveResults = "";
_queryText = "";
_lastQuery = new Query();
_ignoredQueryText = null; // null as invalid value
_updateSource = new CancellationTokenSource();
_updateToken = _updateSource.Token;

Settings = Ioc.Default.GetRequiredService<Settings>();
Settings.PropertyChanged += (_, args) =>
Expand Down Expand Up @@ -248,7 +252,7 @@ public void RegisterResultsUpdatedEvent()
return;
}

var token = e.Token == default ? _updateSource.Token : e.Token;
var token = e.Token == default ? _updateToken : e.Token;

// make a clone to avoid possible issue that plugin will also change the list and items when updating view model
var resultsCopy = DeepCloneResults(e.Results, token);
Expand Down Expand Up @@ -1264,15 +1268,17 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b

var isHomeQuery = query.RawQuery == string.Empty;

_updateSource = new CancellationTokenSource();
var oldSource = Interlocked.Exchange(ref _updateSource, new CancellationTokenSource());
_updateToken = _updateSource.Token;
oldSource?.Dispose(); // Dispose old update source to fix possible cancellation issue

ProgressBarVisibility = Visibility.Hidden;
_isQueryRunning = true;

// Switch to ThreadPool thread
await TaskScheduler.Default;

if (_updateSource.Token.IsCancellationRequested) return;
if (_updateToken.IsCancellationRequested) return;

// Update the query's IsReQuery property to true if this is a re-query
query.IsReQuery = isReQuery;
Expand Down Expand Up @@ -1321,20 +1327,19 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
{
// Wait 15 millisecond for query change in global query
// if query changes, return so that it won't be calculated
await Task.Delay(15, _updateSource.Token);
if (_updateSource.Token.IsCancellationRequested)
return;
await Task.Delay(15, _updateToken);
if (_updateToken.IsCancellationRequested) return;
}*/

_ = Task.Delay(200, _updateSource.Token).ContinueWith(_ =>
_ = Task.Delay(200, _updateToken).ContinueWith(_ =>
{
// start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet
if (_isQueryRunning)
{
ProgressBarVisibility = Visibility.Visible;
}
},
_updateSource.Token,
_updateToken,
TaskContinuationOptions.NotOnCanceled,
TaskScheduler.Default);

Expand All @@ -1345,7 +1350,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
{
tasks = plugins.Select(plugin => plugin.Metadata.HomeDisabled switch
{
false => QueryTaskAsync(plugin, _updateSource.Token),
false => QueryTaskAsync(plugin, _updateToken),
true => Task.CompletedTask
}).ToArray();

Expand All @@ -1359,7 +1364,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
{
tasks = plugins.Select(plugin => plugin.Metadata.Disabled switch
{
false => QueryTaskAsync(plugin, _updateSource.Token),
false => QueryTaskAsync(plugin, _updateToken),
true => Task.CompletedTask
}).ToArray();
}
Expand All @@ -1374,13 +1379,13 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
// nothing to do here
}

if (_updateSource.Token.IsCancellationRequested) return;
if (_updateToken.IsCancellationRequested) return;

// this should happen once after all queries are done so progress bar should continue
// until the end of all querying
_isQueryRunning = false;

if (!_updateSource.Token.IsCancellationRequested)
if (!_updateToken.IsCancellationRequested)
{
// update to hidden if this is still the current query
ProgressBarVisibility = Visibility.Hidden;
Expand Down Expand Up @@ -1447,12 +1452,12 @@ void QueryHistoryTask()

var results = GetHistoryItems(historyItems);

if (_updateSource.Token.IsCancellationRequested) return;
if (_updateToken.IsCancellationRequested) return;

App.API.LogDebug(ClassName, $"Update results for history");

if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, _historyMetadata, query,
_updateSource.Token)))
_updateToken)))
{
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
}
Expand Down
Loading