Skip to content

Commit b33b9c0

Browse files
authored
Merge branch 'dev' into quickswitch
2 parents 8d9b152 + 2d2f7de commit b33b9c0

1 file changed

Lines changed: 23 additions & 17 deletions

File tree

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
3737
private Query _lastQuery;
3838
private bool _lastIsHomeQuery;
3939
private string _queryTextBeforeLeaveResults;
40-
private string _ignoredQueryText = null;
40+
private string _ignoredQueryText; // Used to ignore query text change when switching between context menu and query results
4141

4242
private readonly FlowLauncherJsonStorage<History> _historyItemsStorage;
4343
private readonly FlowLauncherJsonStorage<UserSelectedRecord> _userSelectedRecordStorage;
@@ -48,6 +48,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
4848
private readonly TopMostRecord _topMostRecord;
4949

5050
private CancellationTokenSource _updateSource; // Used to cancel old query flows
51+
private CancellationToken _updateToken; // Used to avoid ObjectDisposedException of _updateSource.Token
5152

5253
private ChannelWriter<ResultsForUpdate> _resultsUpdateChannelWriter;
5354
private Task _resultsViewUpdateTask;
@@ -70,6 +71,7 @@ public MainViewModel()
7071
_queryTextBeforeLeaveResults = "";
7172
_queryText = "";
7273
_lastQuery = new Query();
74+
_ignoredQueryText = null; // null as invalid value
7375

7476
Settings = Ioc.Default.GetRequiredService<Settings>();
7577
Settings.PropertyChanged += (_, args) =>
@@ -252,7 +254,7 @@ public void RegisterResultsUpdatedEvent()
252254
return;
253255
}
254256

255-
var token = e.Token == default ? _updateSource.Token : e.Token;
257+
var token = e.Token == default ? _updateToken : e.Token;
256258

257259
IReadOnlyList<Result> resultsCopy;
258260
if (e.Results == null)
@@ -1328,15 +1330,20 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
13281330
return;
13291331
}
13301332

1331-
_updateSource = new CancellationTokenSource();
1333+
_updateSource?.Dispose();
1334+
1335+
var currentUpdateSource = new CancellationTokenSource();
1336+
_updateSource = currentUpdateSource;
1337+
var currentCancellationToken = _updateSource.Token;
1338+
_updateToken = currentCancellationToken;
13321339

13331340
ProgressBarVisibility = Visibility.Hidden;
13341341
_isQueryRunning = true;
13351342

13361343
// Switch to ThreadPool thread
13371344
await TaskScheduler.Default;
13381345

1339-
if (_updateSource.Token.IsCancellationRequested) return;
1346+
if (currentCancellationToken.IsCancellationRequested) return;
13401347

13411348
// Update the query's IsReQuery property to true if this is a re-query
13421349
query.IsReQuery = isReQuery;
@@ -1385,20 +1392,19 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
13851392
{
13861393
// Wait 15 millisecond for query change in global query
13871394
// if query changes, return so that it won't be calculated
1388-
await Task.Delay(15, _updateSource.Token);
1389-
if (_updateSource.Token.IsCancellationRequested)
1390-
return;
1395+
await Task.Delay(15, currentCancellationToken);
1396+
if (currentCancellationToken.IsCancellationRequested) return;
13911397
}*/
13921398

1393-
_ = Task.Delay(200, _updateSource.Token).ContinueWith(_ =>
1399+
_ = Task.Delay(200, currentCancellationToken).ContinueWith(_ =>
13941400
{
13951401
// start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet
13961402
if (_isQueryRunning)
13971403
{
13981404
ProgressBarVisibility = Visibility.Visible;
13991405
}
14001406
},
1401-
_updateSource.Token,
1407+
currentCancellationToken,
14021408
TaskContinuationOptions.NotOnCanceled,
14031409
TaskScheduler.Default);
14041410

@@ -1409,21 +1415,21 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
14091415
{
14101416
tasks = plugins.Select(plugin => plugin.Metadata.HomeDisabled switch
14111417
{
1412-
false => QueryTaskAsync(plugin, _updateSource.Token),
1418+
false => QueryTaskAsync(plugin, currentCancellationToken),
14131419
true => Task.CompletedTask
14141420
}).ToArray();
14151421

14161422
// Query history results for home page firstly so it will be put on top of the results
14171423
if (Settings.ShowHistoryResultsForHomePage)
14181424
{
1419-
QueryHistoryTask();
1425+
QueryHistoryTask(currentCancellationToken);
14201426
}
14211427
}
14221428
else
14231429
{
14241430
tasks = plugins.Select(plugin => plugin.Metadata.Disabled switch
14251431
{
1426-
false => QueryTaskAsync(plugin, _updateSource.Token),
1432+
false => QueryTaskAsync(plugin, currentCancellationToken),
14271433
true => Task.CompletedTask
14281434
}).ToArray();
14291435
}
@@ -1438,13 +1444,13 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
14381444
// nothing to do here
14391445
}
14401446

1441-
if (_updateSource.Token.IsCancellationRequested) return;
1447+
if (currentCancellationToken.IsCancellationRequested) return;
14421448

14431449
// this should happen once after all queries are done so progress bar should continue
14441450
// until the end of all querying
14451451
_isQueryRunning = false;
14461452

1447-
if (!_updateSource.Token.IsCancellationRequested)
1453+
if (!currentCancellationToken.IsCancellationRequested)
14481454
{
14491455
// update to hidden if this is still the current query
14501456
ProgressBarVisibility = Visibility.Hidden;
@@ -1542,19 +1548,19 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
15421548
}
15431549
}
15441550

1545-
void QueryHistoryTask()
1551+
void QueryHistoryTask(CancellationToken token)
15461552
{
15471553
// Select last history results and revert its order to make sure last history results are on top
15481554
var historyItems = _history.Items.TakeLast(Settings.MaxHistoryResultsToShowForHomePage).Reverse();
15491555

15501556
var results = GetHistoryItems(historyItems);
15511557

1552-
if (_updateSource.Token.IsCancellationRequested) return;
1558+
if (token.IsCancellationRequested) return;
15531559

15541560
App.API.LogDebug(ClassName, $"Update results for history");
15551561

15561562
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, _historyMetadata, query,
1557-
_updateSource.Token)))
1563+
token)))
15581564
{
15591565
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
15601566
}

0 commit comments

Comments
 (0)