From 297643c3e52d9800154390e40a5518e64cb86d9e Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 6 May 2025 19:35:40 +0800 Subject: [PATCH] Revert all changes as master branch --- Flow.Launcher/ViewModel/MainViewModel.cs | 35 +++++++++++++----------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index aab2e2d03..c0b74dc68 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1266,9 +1266,12 @@ namespace Flow.Launcher.ViewModel var isHomeQuery = query.RawQuery == string.Empty; - _updateSource?.Dispose(); // Dispose old update source to fix possible cancellation issue - _updateSource = new CancellationTokenSource(); - _updateToken = _updateSource.Token; + _updateSource?.Dispose(); + + var currentUpdateSource = new CancellationTokenSource(); + _updateSource = currentUpdateSource; + var currentCancellationToken = _updateSource.Token; + _updateToken = currentCancellationToken; ProgressBarVisibility = Visibility.Hidden; _isQueryRunning = true; @@ -1276,7 +1279,7 @@ namespace Flow.Launcher.ViewModel // Switch to ThreadPool thread await TaskScheduler.Default; - if (_updateToken.IsCancellationRequested) return; + if (currentCancellationToken.IsCancellationRequested) return; // Update the query's IsReQuery property to true if this is a re-query query.IsReQuery = isReQuery; @@ -1325,11 +1328,11 @@ namespace Flow.Launcher.ViewModel { // Wait 15 millisecond for query change in global query // if query changes, return so that it won't be calculated - await Task.Delay(15, _updateToken); - if (_updateToken.IsCancellationRequested) return; + await Task.Delay(15, currentCancellationToken); + if (currentCancellationToken.IsCancellationRequested) return; }*/ - _ = Task.Delay(200, _updateToken).ContinueWith(_ => + _ = Task.Delay(200, currentCancellationToken).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) @@ -1337,7 +1340,7 @@ namespace Flow.Launcher.ViewModel ProgressBarVisibility = Visibility.Visible; } }, - _updateToken, + currentCancellationToken, TaskContinuationOptions.NotOnCanceled, TaskScheduler.Default); @@ -1348,21 +1351,21 @@ namespace Flow.Launcher.ViewModel { tasks = plugins.Select(plugin => plugin.Metadata.HomeDisabled switch { - false => QueryTaskAsync(plugin, _updateToken), + false => QueryTaskAsync(plugin, currentCancellationToken), true => Task.CompletedTask }).ToArray(); // Query history results for home page firstly so it will be put on top of the results if (Settings.ShowHistoryResultsForHomePage) { - QueryHistoryTask(); + QueryHistoryTask(currentCancellationToken); } } else { tasks = plugins.Select(plugin => plugin.Metadata.Disabled switch { - false => QueryTaskAsync(plugin, _updateToken), + false => QueryTaskAsync(plugin, currentCancellationToken), true => Task.CompletedTask }).ToArray(); } @@ -1377,13 +1380,13 @@ namespace Flow.Launcher.ViewModel // nothing to do here } - if (_updateToken.IsCancellationRequested) return; + if (currentCancellationToken.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 (!_updateToken.IsCancellationRequested) + if (!currentCancellationToken.IsCancellationRequested) { // update to hidden if this is still the current query ProgressBarVisibility = Visibility.Hidden; @@ -1443,19 +1446,19 @@ namespace Flow.Launcher.ViewModel } } - void QueryHistoryTask() + void QueryHistoryTask(CancellationToken token) { // Select last history results and revert its order to make sure last history results are on top var historyItems = _history.Items.TakeLast(Settings.MaxHistoryResultsToShowForHomePage).Reverse(); var results = GetHistoryItems(historyItems); - if (_updateToken.IsCancellationRequested) return; + if (token.IsCancellationRequested) return; App.API.LogDebug(ClassName, $"Update results for history"); if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, _historyMetadata, query, - _updateToken))) + token))) { App.API.LogError(ClassName, "Unable to add item to Result Update Queue"); }