Skip to content

Commit b999fd0

Browse files
committed
Check should clear existing results after token checked
1 parent 1206baa commit b999fd0

File tree

3 files changed

+19
-68
lines changed

3 files changed

+19
-68
lines changed

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 15 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.ComponentModel;
45
using System.Globalization;
@@ -37,9 +38,6 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
3738
private string _queryTextBeforeLeaveResults;
3839
private string _ignoredQueryText; // Used to ignore query text change when switching between context menu and query results
3940

40-
private readonly object _shouldClearExistingResultsLock = new();
41-
private bool _shouldClearExistingResults;
42-
4341
private readonly FlowLauncherJsonStorage<History> _historyItemsStorage;
4442
private readonly FlowLauncherJsonStorage<UserSelectedRecord> _userSelectedRecordStorage;
4543
private readonly FlowLauncherJsonStorageTopMostRecord _topMostRecord;
@@ -216,7 +214,17 @@ async Task UpdateActionAsync()
216214
while (channelReader.TryRead(out var item))
217215
{
218216
if (!item.Token.IsCancellationRequested)
217+
{
218+
// Indicate if to clear existing results so to show only ones from plugins with action keywords
219+
var query = item.Query;
220+
var currentIsHomeQuery = item.IsHomeQuery;
221+
var shouldClearExistingResults = ShouldClearExistingResultsForQuery(query, currentIsHomeQuery);
222+
_lastQuery = item.Query;
223+
_previousIsHomeQuery = currentIsHomeQuery;
224+
item.ShouldClearExistingResults = shouldClearExistingResults;
225+
219226
queue[item.ID] = item;
227+
}
220228
}
221229

222230
UpdateResultView(queue.Values);
@@ -270,24 +278,8 @@ public void RegisterResultsUpdatedEvent()
270278

271279
App.API.LogDebug(ClassName, $"Update results for plugin <{pair.Metadata.Name}>");
272280

273-
// Home query does not support IResultUpdated, so this flag is false
274-
var currentIsHomeQuery = false;
275-
276-
// Indicate if to clear existing results so to show only ones from plugins with action keywords
277-
var shouldClearExistingResults = ShouldClearExistingResultsForQuery(e.Query, currentIsHomeQuery);
278-
if (shouldClearExistingResults)
279-
{
280-
// Setup the flag to clear existing results so that ResultsViewModel.NewResults will handle in the next update
281-
lock (_shouldClearExistingResultsLock)
282-
{
283-
_shouldClearExistingResults = true;
284-
}
285-
}
286-
_lastQuery = e.Query;
287-
_previousIsHomeQuery = currentIsHomeQuery;
288-
289281
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, pair.Metadata, e.Query,
290-
token)))
282+
false, token)))
291283
{
292284
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
293285
}
@@ -1453,21 +1445,8 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
14531445

14541446
App.API.LogDebug(ClassName, $"Update results for plugin <{plugin.Metadata.Name}>");
14551447

1456-
// Indicate if to clear existing results so to show only ones from plugins with action keywords
1457-
var shouldClearExistingResults = ShouldClearExistingResultsForQuery(query, currentIsHomeQuery);
1458-
if (shouldClearExistingResults)
1459-
{
1460-
// Setup the flag to clear existing results so that ResultsViewModel.NewResults will handle in the next update
1461-
lock (_shouldClearExistingResultsLock)
1462-
{
1463-
_shouldClearExistingResults = true;
1464-
}
1465-
}
1466-
_lastQuery = query;
1467-
_previousIsHomeQuery = currentIsHomeQuery;
1468-
14691448
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, plugin.Metadata, query,
1470-
token, reSelect)))
1449+
currentIsHomeQuery, token, reSelect)))
14711450
{
14721451
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
14731452
}
@@ -1484,21 +1463,8 @@ void QueryHistoryTask(CancellationToken token)
14841463

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

1487-
// Indicate if to clear existing results so to show only ones from plugins with action keywords
1488-
var shouldClearExistingResults = ShouldClearExistingResultsForQuery(query, currentIsHomeQuery);
1489-
if (shouldClearExistingResults)
1490-
{
1491-
// Setup the flag to clear existing results so that ResultsViewModel.NewResults will handle in the next update
1492-
lock (_shouldClearExistingResultsLock)
1493-
{
1494-
_shouldClearExistingResults = true;
1495-
}
1496-
}
1497-
_lastQuery = query;
1498-
_previousIsHomeQuery = currentIsHomeQuery;
1499-
15001466
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, _historyMetadata, query,
1501-
token, reSelect)))
1467+
currentIsHomeQuery, token, reSelect)))
15021468
{
15031469
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
15041470
}
@@ -1733,20 +1699,6 @@ internal bool ResultsSelected(ResultsViewModel results)
17331699
return selected;
17341700
}
17351701

1736-
internal bool CheckShouldClearExistingResultsAndReset()
1737-
{
1738-
lock (_shouldClearExistingResultsLock)
1739-
{
1740-
if (_shouldClearExistingResults)
1741-
{
1742-
_shouldClearExistingResults = false;
1743-
return true;
1744-
}
1745-
1746-
return false;
1747-
}
1748-
}
1749-
17501702
#endregion
17511703

17521704
#region Hotkey
@@ -1912,6 +1864,7 @@ public void UpdateResultView(ICollection<ResultsForUpdate> resultsForUpdates)
19121864
{
19131865
if (!resultsForUpdates.Any())
19141866
return;
1867+
19151868
CancellationToken token;
19161869

19171870
try

Flow.Launcher/ViewModel/ResultsForUpdate.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ public record struct ResultsForUpdate(
88
IReadOnlyList<Result> Results,
99
PluginMetadata Metadata,
1010
Query Query,
11+
bool IsHomeQuery,
1112
CancellationToken Token,
12-
bool ReSelectFirstResult = true)
13+
bool ReSelectFirstResult = true,
14+
bool ShouldClearExistingResults = false)
1315
{
1416
public string ID { get; } = Metadata.ID;
1517
}

Flow.Launcher/ViewModel/ResultsViewModel.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,6 @@ public void AddResults(List<Result> newRawResults, string resultId)
187187
/// </summary>
188188
public void AddResults(ICollection<ResultsForUpdate> resultsForUpdates, CancellationToken token, bool reselect = true)
189189
{
190-
if (token.IsCancellationRequested)
191-
return;
192-
193190
// Since NewResults may need to clear existing results, do not check token cancellation after this point
194191
var newResults = NewResults(resultsForUpdates);
195192

@@ -245,8 +242,7 @@ private List<ResultViewModel> NewResults(ICollection<ResultsForUpdate> resultsFo
245242

246243
var newResults = resultsForUpdates.SelectMany(u => u.Results, (u, r) => new ResultViewModel(r, _settings));
247244

248-
// If mainVM has flag to clear existing results, handle it here
249-
if (_mainVM != null && _mainVM.CheckShouldClearExistingResultsAndReset())
245+
if (resultsForUpdates.Any(x => x.ShouldClearExistingResults))
250246
{
251247
App.API.LogDebug("NewResults", $"Existing results are cleared for query");
252248
return newResults.OrderByDescending(rv => rv.Result.Score).ToList();

0 commit comments

Comments
 (0)