From 905a97d0b384f01ec9536f56a87f3bd01254a36f Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 24 May 2025 10:01:26 +0800 Subject: [PATCH 1/2] Test result clear issue --- Flow.Launcher/ViewModel/MainViewModel.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 48652f06358..7859b4e1356 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -194,6 +194,8 @@ public MainViewModel() _ = RegisterClockAndDateUpdateAsync(); } + private int cancelIndex = 0; + private void RegisterViewUpdate() { var resultUpdateChannel = Channel.CreateUnbounded(); @@ -212,6 +214,17 @@ async Task UpdateActionAsync() await Task.Delay(20); while (channelReader.TryRead(out var item)) { + if (item.shouldClearExistingResults) + { + cancelIndex++; + if (cancelIndex > 1) + { + // Assume one task for clearing existing results is cancelled + continue; + } + } + + if (!item.Token.IsCancellationRequested) queue[item.ID] = item; } From d5d660ebe11be25112fba28c6ea6a00a91dd09d3 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Mon, 26 May 2025 10:18:17 +0800 Subject: [PATCH 2/2] Test for empty list --- Flow.Launcher/ViewModel/MainViewModel.cs | 27 +++++++++++---------- Flow.Launcher/ViewModel/ResultsViewModel.cs | 13 ++++++++-- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 7859b4e1356..c8a7e0a3670 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -214,18 +214,7 @@ async Task UpdateActionAsync() await Task.Delay(20); while (channelReader.TryRead(out var item)) { - if (item.shouldClearExistingResults) - { - cancelIndex++; - if (cancelIndex > 1) - { - // Assume one task for clearing existing results is cancelled - continue; - } - } - - - if (!item.Token.IsCancellationRequested) + if (item.shouldClearExistingResults || !item.Token.IsCancellationRequested) queue[item.ID] = item; } @@ -1450,6 +1439,16 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) : _lastQuery = query; _previousIsHomeQuery = currentIsHomeQuery; + // Test: Assume first query task for clearing existing results is cancelled + if (shouldClearExistingResults) + { + cancelIndex++; + if (cancelIndex == 2) + { + currentUpdateSource.Cancel(); + } + } + if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, plugin.Metadata, query, token, reSelect, shouldClearExistingResults))) { @@ -1874,7 +1873,9 @@ public void UpdateResultView(ICollection resultsForUpdates) { if (!resultsForUpdates.Any()) return; + CancellationToken token; + var shouldClearExistingResults = resultsForUpdates.Any(r => r.shouldClearExistingResults); try { @@ -1936,7 +1937,7 @@ public void UpdateResultView(ICollection resultsForUpdates) // it should be the same for all results bool reSelect = resultsForUpdates.First().ReSelectFirstResult; - Results.AddResults(resultsForUpdates, token, reSelect); + Results.AddResults(resultsForUpdates, token, reSelect, shouldClearExistingResults); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "")] diff --git a/Flow.Launcher/ViewModel/ResultsViewModel.cs b/Flow.Launcher/ViewModel/ResultsViewModel.cs index 9cd36e5c480..1c7a504da22 100644 --- a/Flow.Launcher/ViewModel/ResultsViewModel.cs +++ b/Flow.Launcher/ViewModel/ResultsViewModel.cs @@ -185,12 +185,21 @@ public void AddResults(List newRawResults, string resultId) /// /// To avoid deadlock, this method should not called from main thread /// - public void AddResults(ICollection resultsForUpdates, CancellationToken token, bool reselect = true) + public void AddResults(ICollection resultsForUpdates, CancellationToken token, bool reselect = true, bool shouldClearExistingResults = false) { var newResults = NewResults(resultsForUpdates); if (token.IsCancellationRequested) - return; + { + if (shouldClearExistingResults) + { + newResults = new List(); + } + else + { + return; + } + } UpdateResults(newResults, reselect, token); }