Skip to content

Commit f8fa9ba

Browse files
committed
Add lock & Improve code quality
1 parent dc346fd commit f8fa9ba

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
3737
private string _queryTextBeforeLeaveResults;
3838
private string _ignoredQueryText; // Used to ignore query text change when switching between context menu and query results
3939

40+
private readonly object _shouldClearExistingResultsLock = new();
41+
private bool _shouldClearExistingResults;
42+
4043
private readonly FlowLauncherJsonStorage<History> _historyItemsStorage;
4144
private readonly FlowLauncherJsonStorage<UserSelectedRecord> _userSelectedRecordStorage;
4245
private readonly FlowLauncherJsonStorageTopMostRecord _topMostRecord;
@@ -1079,8 +1082,6 @@ private bool QueryResultsPreviewed()
10791082

10801083
#region Query
10811084

1082-
internal bool ShouldClearExistingResults { get; set; }
1083-
10841085
public void QueryResults()
10851086
{
10861087
_ = QueryResultsAsync(false);
@@ -1439,7 +1440,10 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
14391440
if (shouldClearExistingResults)
14401441
{
14411442
// Setup the flag to clear existing results so that ResultsViewModel.NewResults will handle in the next update
1442-
ShouldClearExistingResults = true;
1443+
lock (_shouldClearExistingResultsLock)
1444+
{
1445+
_shouldClearExistingResults = true;
1446+
}
14431447
}
14441448
_lastQuery = query;
14451449
_previousIsHomeQuery = currentIsHomeQuery;
@@ -1467,7 +1471,10 @@ void QueryHistoryTask(CancellationToken token)
14671471
if (shouldClearExistingResults)
14681472
{
14691473
// Setup the flag to clear existing results so that ResultsViewModel.NewResults will handle in the next update
1470-
ShouldClearExistingResults = true;
1474+
lock (_shouldClearExistingResultsLock)
1475+
{
1476+
_shouldClearExistingResults = true;
1477+
}
14711478
}
14721479
_lastQuery = query;
14731480
_previousIsHomeQuery = currentIsHomeQuery;
@@ -1708,6 +1715,20 @@ internal bool ResultsSelected(ResultsViewModel results)
17081715
return selected;
17091716
}
17101717

1718+
internal bool CheckShouldClearExistingResultsAndReset()
1719+
{
1720+
lock (_shouldClearExistingResultsLock)
1721+
{
1722+
if (_shouldClearExistingResults)
1723+
{
1724+
_shouldClearExistingResults = false;
1725+
return true;
1726+
}
1727+
1728+
return false;
1729+
}
1730+
}
1731+
17111732
#endregion
17121733

17131734
#region Hotkey

Flow.Launcher/ViewModel/ResultsViewModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,8 @@ private List<ResultViewModel> NewResults(ICollection<ResultsForUpdate> resultsFo
246246
var newResults = resultsForUpdates.SelectMany(u => u.Results, (u, r) => new ResultViewModel(r, _settings));
247247

248248
// If mainVM has flag to clear existing results, handle it here
249-
if (_mainVM != null && _mainVM.ShouldClearExistingResults)
249+
if (_mainVM != null && _mainVM.CheckShouldClearExistingResultsAndReset())
250250
{
251-
_mainVM.ShouldClearExistingResults = false;
252251
App.API.LogDebug("NewResults", $"Existing results are cleared for query");
253252
return newResults.OrderByDescending(rv => rv.Result.Score).ToList();
254253
}

0 commit comments

Comments
 (0)