fix potential duplicate item due to IResultUpdate event

This commit is contained in:
张弘韬 2021-01-14 19:54:37 +08:00
parent c39dfab6e5
commit 3d69998bc3

View file

@ -94,7 +94,7 @@ namespace Flow.Launcher.ViewModel
{ {
foreach (var pair in PluginManager.GetPluginsForInterface<IResultUpdated>()) foreach (var pair in PluginManager.GetPluginsForInterface<IResultUpdated>())
{ {
var plugin = (IResultUpdated)pair.Plugin; var plugin = (IResultUpdated) pair.Plugin;
plugin.ResultsUpdated += (s, e) => plugin.ResultsUpdated += (s, e) =>
{ {
PluginManager.UpdatePluginMetadata(e.Results, pair.Metadata, e.Query); PluginManager.UpdatePluginMetadata(e.Results, pair.Metadata, e.Query);
@ -113,11 +113,18 @@ namespace Flow.Launcher.ViewModel
async Task updateAction() async Task updateAction()
{ {
var queue = new Dictionary<string, ResultsForUpdate>();
while (await _resultsUpdateQueue.OutputAvailableAsync()) while (await _resultsUpdateQueue.OutputAvailableAsync())
{ {
queue.Clear();
await Task.Delay(20); await Task.Delay(20);
_resultsUpdateQueue.TryReceiveAll(out var queue); while (_resultsUpdateQueue.TryReceive(out var item))
UpdateResultView(queue.Where(r => !r.Token.IsCancellationRequested)); {
if (!item.Token.IsCancellationRequested)
queue[item.ID] = item;
}
UpdateResultView(queue.Values);
} }
} }
@ -381,7 +388,7 @@ namespace Flow.Launcher.ViewModel
Title = string.Format(title, h.Query), Title = string.Format(title, h.Query),
SubTitle = string.Format(time, h.ExecutedDateTime), SubTitle = string.Format(time, h.ExecutedDateTime),
IcoPath = "Images\\history.png", IcoPath = "Images\\history.png",
OriginQuery = new Query { RawQuery = h.Query }, OriginQuery = new Query {RawQuery = h.Query},
Action = _ => Action = _ =>
{ {
SelectedResults = Results; SelectedResults = Results;
@ -438,70 +445,70 @@ namespace Flow.Launcher.ViewModel
var plugins = PluginManager.ValidPluginsForQuery(query); var plugins = PluginManager.ValidPluginsForQuery(query);
Task.Run(async () => Task.Run(async () =>
{
if (query.ActionKeyword == Plugin.Query.GlobalPluginWildcardSign)
{ {
// Wait 45 millisecond for query change in global query if (query.ActionKeyword == Plugin.Query.GlobalPluginWildcardSign)
// if query changes, return so that it won't be calculated {
await Task.Delay(45, currentCancellationToken); // Wait 45 millisecond for query change in global query
// if query changes, return so that it won't be calculated
await Task.Delay(45, currentCancellationToken);
if (currentCancellationToken.IsCancellationRequested)
return;
}
_ = 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 (!currentCancellationToken.IsCancellationRequested && _isQueryRunning)
{
ProgressBarVisibility = Visibility.Visible;
}
}, currentCancellationToken);
// so looping will stop once it was cancelled
var parallelOptions = new ParallelOptions {CancellationToken = currentCancellationToken};
try
{
Parallel.ForEach(plugins, parallelOptions, plugin =>
{
if (!plugin.Metadata.Disabled)
{
try
{
var results = PluginManager.QueryForPlugin(plugin, query);
_resultsUpdateQueue.Post(new ResultsForUpdate(results, plugin.Metadata,
query,
currentCancellationToken));
}
catch (Exception e)
{
Log.Exception("MainViewModel",
$"Exception when querying the plugin {plugin.Metadata.Name}", e,
"QueryResults");
}
}
});
}
catch (OperationCanceledException)
{
return;
// nothing to do here
}
if (currentCancellationToken.IsCancellationRequested) if (currentCancellationToken.IsCancellationRequested)
return; return;
}
_ = Task.Delay(200, currentCancellationToken).ContinueWith(_ => // this should happen once after all queries are done so progress bar should continue
{ // until the end of all querying
// start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet _isQueryRunning = false;
if (!currentCancellationToken.IsCancellationRequested && _isQueryRunning) if (!currentCancellationToken.IsCancellationRequested)
{ {
ProgressBarVisibility = Visibility.Visible; // update to hidden if this is still the current query
ProgressBarVisibility = Visibility.Hidden;
} }
}, currentCancellationToken); }, currentCancellationToken)
.ContinueWith(t => Log.Exception("MainViewModel", "Error when querying plugins",
// so looping will stop once it was cancelled t.Exception?.InnerException, "QueryResults")
var parallelOptions = new ParallelOptions { CancellationToken = currentCancellationToken }; , TaskContinuationOptions.OnlyOnFaulted);
try
{
Parallel.ForEach(plugins, parallelOptions, plugin =>
{
if (!plugin.Metadata.Disabled)
{
try
{
var results = PluginManager.QueryForPlugin(plugin, query);
_resultsUpdateQueue.Post(new ResultsForUpdate(results, plugin.Metadata,
query,
currentCancellationToken));
}
catch (Exception e)
{
Log.Exception("MainViewModel",
$"Exception when querying the plugin {plugin.Metadata.Name}", e,
"QueryResults");
}
}
});
}
catch (OperationCanceledException)
{
return;
// nothing to do here
}
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 (!currentCancellationToken.IsCancellationRequested)
{
// update to hidden if this is still the current query
ProgressBarVisibility = Visibility.Hidden;
}
}, currentCancellationToken)
.ContinueWith(t => Log.Exception("MainViewModel", "Error when querying plugins", t.Exception?.InnerException, "QueryResults")
, TaskContinuationOptions.OnlyOnFaulted);
} }