Skip to content

Commit 9665619

Browse files
authored
Merge pull request #3257 from Jack251970/result_copy
Fix Issue that Plugin Cannot Cache Records
2 parents 70396bc + b469228 commit 9665619

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public static async Task<List<Result>> QueryForPluginAsync(PluginPair pair, Quer
281281
return results;
282282
}
283283

284-
public static void UpdatePluginMetadata(List<Result> results, PluginMetadata metadata, Query query)
284+
public static void UpdatePluginMetadata(IReadOnlyList<Result> results, PluginMetadata metadata, Query query)
285285
{
286286
foreach (var r in results)
287287
{

Flow.Launcher.Plugin/Result.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,9 @@ public ValueTask<bool> ExecuteAsync(ActionContext context)
266266
/// The key to identify the record. This is used when FL checks whether the result is the topmost record. Or FL calculates the hashcode of the result for user selected records.
267267
/// This can be useful when your plugin will change the Title or SubTitle of the result dynamically.
268268
/// If the plugin does not specific this, FL just uses Title and SubTitle to identify this result.
269+
/// Note: Because old data does not have this key, we should use null as the default value for consistency.
269270
/// </summary>
270-
public string RecordKey { get; set; } = string.Empty;
271+
public string RecordKey { get; set; } = null;
271272

272273
/// <summary>
273274
/// Info of the preview section of a <see cref="Result"/>

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ private void RegisterResultsUpdatedEvent()
231231

232232
var token = e.Token == default ? _updateToken : e.Token;
233233

234-
// make a copy of results to avoid plugin change the result when updating view model
235-
var resultsCopy = e.Results.ToList();
234+
// make a clone to avoid possible issue that plugin will also change the list and items when updating view model
235+
var resultsCopy = DeepCloneResults(e.Results, token);
236236

237237
PluginManager.UpdatePluginMetadata(resultsCopy, pair.Metadata, e.Query);
238238
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, pair.Metadata, e.Query,
@@ -414,6 +414,22 @@ private async Task OpenResultAsync(string index)
414414
}
415415
}
416416

417+
private static IReadOnlyList<Result> DeepCloneResults(IReadOnlyList<Result> results, CancellationToken token = default)
418+
{
419+
var resultsCopy = new List<Result>();
420+
foreach (var result in results.ToList())
421+
{
422+
if (token.IsCancellationRequested)
423+
{
424+
break;
425+
}
426+
427+
var resultCopy = result.Clone();
428+
resultsCopy.Add(resultCopy);
429+
}
430+
return resultsCopy;
431+
}
432+
417433
#endregion
418434

419435
#region BasicCommands
@@ -1180,9 +1196,18 @@ async Task QueryTask(PluginPair plugin, bool reSelect = true)
11801196

11811197
currentCancellationToken.ThrowIfCancellationRequested();
11821198

1183-
results ??= _emptyResult;
1199+
IReadOnlyList<Result> resultsCopy;
1200+
if (results == null)
1201+
{
1202+
resultsCopy = _emptyResult;
1203+
}
1204+
else
1205+
{
1206+
// make a copy of results to avoid possible issue that FL changes some properties of the records, like score, etc.
1207+
resultsCopy = DeepCloneResults(results);
1208+
}
11841209

1185-
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, plugin.Metadata, query,
1210+
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, plugin.Metadata, query,
11861211
currentCancellationToken, reSelect)))
11871212
{
11881213
Log.Error("MainViewModel", "Unable to add item to Result Update Queue");
@@ -1471,12 +1496,31 @@ public void UpdateResultView(ICollection<ResultsForUpdate> resultsForUpdates)
14711496
{
14721497
result.Score = Result.MaxScore;
14731498
}
1474-
else if (result.Score != Result.MaxScore)
1499+
else
14751500
{
14761501
var priorityScore = metaResults.Metadata.Priority * 150;
1477-
result.Score += result.AddSelectedCount ?
1478-
_userSelectedRecord.GetSelectedCount(result) + priorityScore :
1479-
priorityScore;
1502+
if (result.AddSelectedCount)
1503+
{
1504+
if ((long)result.Score + _userSelectedRecord.GetSelectedCount(result) + priorityScore > Result.MaxScore)
1505+
{
1506+
result.Score = Result.MaxScore;
1507+
}
1508+
else
1509+
{
1510+
result.Score += _userSelectedRecord.GetSelectedCount(result) + priorityScore;
1511+
}
1512+
}
1513+
else
1514+
{
1515+
if ((long)result.Score + priorityScore > Result.MaxScore)
1516+
{
1517+
result.Score = Result.MaxScore;
1518+
}
1519+
else
1520+
{
1521+
result.Score += priorityScore;
1522+
}
1523+
}
14801524
}
14811525
}
14821526
}

0 commit comments

Comments
 (0)