Skip to content

Commit ece9b96

Browse files
authored
Merge branch 'dev' into improve_update
2 parents 51714d5 + 2d2f7de commit ece9b96

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
3636
private Query _progressQuery; // Used for QueryResultAsync
3737
private Query _updateQuery; // Used for ResultsUpdated
3838
private string _queryTextBeforeLeaveResults;
39-
private string _ignoredQueryText = null;
39+
private string _ignoredQueryText; // Used to ignore query text change when switching between context menu and query results
4040

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

4949
private CancellationTokenSource _updateSource; // Used to cancel old query flows
50+
private CancellationToken _updateToken; // Used to avoid ObjectDisposedException of _updateSource.Token
5051

5152
private ChannelWriter<ResultsForUpdate> _resultsUpdateChannelWriter;
5253
private Task _resultsViewUpdateTask;
@@ -68,6 +69,7 @@ public MainViewModel()
6869
_queryTextBeforeLeaveResults = "";
6970
_queryText = "";
7071
_lastQuery = new Query();
72+
_ignoredQueryText = null; // null as invalid value
7173

7274
Settings = Ioc.Default.GetRequiredService<Settings>();
7375
Settings.PropertyChanged += (_, args) =>
@@ -249,7 +251,7 @@ public void RegisterResultsUpdatedEvent()
249251
return;
250252
}
251253

252-
var token = e.Token == default ? _updateSource.Token : e.Token;
254+
var token = e.Token == default ? _updateToken : e.Token;
253255

254256
// make a clone to avoid possible issue that plugin will also change the list and items when updating view model
255257
var resultsCopy = DeepCloneResults(e.Results, token);
@@ -1275,7 +1277,12 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
12751277

12761278
var isHomeQuery = query.RawQuery == string.Empty;
12771279

1278-
_updateSource = new CancellationTokenSource();
1280+
_updateSource?.Dispose();
1281+
1282+
var currentUpdateSource = new CancellationTokenSource();
1283+
_updateSource = currentUpdateSource;
1284+
var currentCancellationToken = _updateSource.Token;
1285+
_updateToken = currentCancellationToken;
12791286

12801287
ProgressBarVisibility = Visibility.Hidden;
12811288

@@ -1334,19 +1341,19 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
13341341
{
13351342
// Wait 15 millisecond for query change in global query
13361343
// if query changes, return so that it won't be calculated
1337-
await Task.Delay(15, _updateSource.Token);
1338-
if (_updateSource.Token.IsCancellationRequested) return;
1344+
await Task.Delay(15, currentCancellationToken);
1345+
if (currentCancellationToken.IsCancellationRequested) return;
13391346
}*/
13401347

1341-
_ = Task.Delay(200, _updateSource.Token).ContinueWith(_ =>
1348+
_ = Task.Delay(200, currentCancellationToken).ContinueWith(_ =>
13421349
{
13431350
// start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet
13441351
if (_progressQuery != null && _progressQuery == query)
13451352
{
13461353
ProgressBarVisibility = Visibility.Visible;
13471354
}
13481355
},
1349-
_updateSource.Token,
1356+
currentCancellationToken,
13501357
TaskContinuationOptions.NotOnCanceled,
13511358
TaskScheduler.Default);
13521359

@@ -1357,21 +1364,21 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
13571364
{
13581365
tasks = plugins.Select(plugin => plugin.Metadata.HomeDisabled switch
13591366
{
1360-
false => QueryTaskAsync(plugin, true, _updateSource.Token),
1367+
false => QueryTaskAsync(plugin, true, currentCancellationToken),
13611368
true => Task.CompletedTask
13621369
}).ToArray();
13631370

13641371
// Query history results for home page firstly so it will be put on top of the results
13651372
if (Settings.ShowHistoryResultsForHomePage)
13661373
{
1367-
QueryHistoryTask();
1374+
QueryHistoryTask(currentCancellationToken);
13681375
}
13691376
}
13701377
else
13711378
{
13721379
tasks = plugins.Select(plugin => plugin.Metadata.Disabled switch
13731380
{
1374-
false => QueryTaskAsync(plugin, false, _updateSource.Token),
1381+
false => QueryTaskAsync(plugin, false, currentCancellationToken),
13751382
true => Task.CompletedTask
13761383
}).ToArray();
13771384
}
@@ -1386,13 +1393,13 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
13861393
// nothing to do here
13871394
}
13881395

1389-
if (_updateSource.Token.IsCancellationRequested) return;
1396+
if (currentCancellationToken.IsCancellationRequested) return;
13901397

13911398
// this should happen once after all queries are done so progress bar should continue
13921399
// until the end of all querying
13931400
_progressQuery = null;
13941401

1395-
if (!_updateSource.Token.IsCancellationRequested)
1402+
if (!currentCancellationToken.IsCancellationRequested)
13961403
{
13971404
// update to hidden if this is still the current query
13981405
ProgressBarVisibility = Visibility.Hidden;
@@ -1458,19 +1465,19 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
14581465
}
14591466
}
14601467

1461-
void QueryHistoryTask()
1468+
void QueryHistoryTask(CancellationToken token)
14621469
{
14631470
// Select last history results and revert its order to make sure last history results are on top
14641471
var historyItems = _history.Items.TakeLast(Settings.MaxHistoryResultsToShowForHomePage).Reverse();
14651472

14661473
var results = GetHistoryItems(historyItems);
14671474

1468-
if (_updateSource.Token.IsCancellationRequested) return;
1475+
if (token.IsCancellationRequested) return;
14691476

14701477
App.API.LogDebug(ClassName, $"Update results for history");
14711478

14721479
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, _historyMetadata, query,
1473-
_updateSource.Token)))
1480+
token)))
14741481
{
14751482
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
14761483
}

0 commit comments

Comments
 (0)