From 0c43ea9379d8a487ac626434623996607889530d Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 8 Dec 2024 09:24:17 +0800 Subject: [PATCH 1/4] Fix possible result update issue for plugins with IResultUpdate interface --- Flow.Launcher/ViewModel/MainViewModel.cs | 35 +++++++++++++++--------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index f964be7954d..8a7d5c0d2b1 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1447,26 +1447,35 @@ public void UpdateResultView(ICollection resultsForUpdates) } #endif - foreach (var metaResults in resultsForUpdates) + try { - foreach (var result in metaResults.Results) + foreach (var metaResults in resultsForUpdates) { - if (_topMostRecord.IsTopMost(result)) - { - result.Score = int.MaxValue; - } - else + foreach (var result in metaResults.Results) { - var priorityScore = metaResults.Metadata.Priority * 150; - result.Score += _userSelectedRecord.GetSelectedCount(result) + priorityScore; + if (_topMostRecord.IsTopMost(result)) + { + result.Score = int.MaxValue; + } + else + { + var priorityScore = metaResults.Metadata.Priority * 150; + result.Score += _userSelectedRecord.GetSelectedCount(result) + priorityScore; + } } } - } - // it should be the same for all results - bool reSelect = resultsForUpdates.First().ReSelectFirstResult; + // it should be the same for all results + bool reSelect = resultsForUpdates.First().ReSelectFirstResult; - Results.AddResults(resultsForUpdates, token, reSelect); + Results.AddResults(resultsForUpdates, token, reSelect); + } + catch (InvalidOperationException e) + { + // Plugin with IResultUpdate interface can somtimes throw this exception + // Collection was modified; enumeration operation may not execute + Log.Exception($"{nameof(MainViewModel)}.{nameof(UpdateResultView)}|UpdateResultView failed", e); + } } #endregion From 9a7c08b1cd3477f63200de4b5628217dbbfb8db0 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 8 Dec 2024 10:00:51 +0800 Subject: [PATCH 2/4] Revert fix in MainViewModel & Fix possible issue in WebSearch plugin --- Flow.Launcher/ViewModel/MainViewModel.cs | 35 +++++++------------ .../Flow.Launcher.Plugin.WebSearch/Main.cs | 2 +- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 8a7d5c0d2b1..f964be7954d 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1447,35 +1447,26 @@ public void UpdateResultView(ICollection resultsForUpdates) } #endif - try + foreach (var metaResults in resultsForUpdates) { - foreach (var metaResults in resultsForUpdates) + foreach (var result in metaResults.Results) { - foreach (var result in metaResults.Results) + if (_topMostRecord.IsTopMost(result)) { - if (_topMostRecord.IsTopMost(result)) - { - result.Score = int.MaxValue; - } - else - { - var priorityScore = metaResults.Metadata.Priority * 150; - result.Score += _userSelectedRecord.GetSelectedCount(result) + priorityScore; - } + result.Score = int.MaxValue; + } + else + { + var priorityScore = metaResults.Metadata.Priority * 150; + result.Score += _userSelectedRecord.GetSelectedCount(result) + priorityScore; } } + } - // it should be the same for all results - bool reSelect = resultsForUpdates.First().ReSelectFirstResult; + // it should be the same for all results + bool reSelect = resultsForUpdates.First().ReSelectFirstResult; - Results.AddResults(resultsForUpdates, token, reSelect); - } - catch (InvalidOperationException e) - { - // Plugin with IResultUpdate interface can somtimes throw this exception - // Collection was modified; enumeration operation may not execute - Log.Exception($"{nameof(MainViewModel)}.{nameof(UpdateResultView)}|UpdateResultView failed", e); - } + Results.AddResults(resultsForUpdates, token, reSelect); } #endregion diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs index ce53c7da5c4..1bb96384c1d 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs @@ -85,7 +85,7 @@ public async Task> QueryAsync(Query query, CancellationToken token) ResultsUpdated?.Invoke(this, new ResultUpdatedEventArgs { - Results = results, + Results = results.ToList(), Query = query }); From 30938f1ce5c639bf2a5aa66a04cf0f5d2d996583 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 8 Dec 2024 12:01:46 +0800 Subject: [PATCH 3/4] Revert "Fix possible issue in WebSearch plugin" --- Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs index 1bb96384c1d..ce53c7da5c4 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs @@ -85,7 +85,7 @@ public async Task> QueryAsync(Query query, CancellationToken token) ResultsUpdated?.Invoke(this, new ResultUpdatedEventArgs { - Results = results.ToList(), + Results = results, Query = query }); From 26d97a2f686afb750cfed7ffe451dac465bb0270 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 8 Dec 2024 12:02:04 +0800 Subject: [PATCH 4/4] Fix plugin change the result when updating view model --- Flow.Launcher/ViewModel/MainViewModel.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index f964be7954d..e5364dbb367 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -233,8 +233,11 @@ private void RegisterResultsUpdatedEvent() var token = e.Token == default ? _updateToken : e.Token; - PluginManager.UpdatePluginMetadata(e.Results, pair.Metadata, e.Query); - if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(e.Results, pair.Metadata, e.Query, + // make a copy of results to avoid plugin change the result when updating view model + var resultsCopy = e.Results.ToList(); + + PluginManager.UpdatePluginMetadata(resultsCopy, pair.Metadata, e.Query); + if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, pair.Metadata, e.Query, token))) { Log.Error("MainViewModel", "Unable to add item to Result Update Queue");