Introduce concurrent directionary for topmost record

This commit is contained in:
Jack251970 2024-11-27 20:56:14 +08:00
parent 0720779c6b
commit b95bbb3e6a

View file

@ -1,18 +1,18 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Flow.Launcher.Plugin;
namespace Flow.Launcher.Storage
{
// todo this class is not thread safe.... but used from multiple threads.
public class TopMostRecord
{
[JsonInclude]
public Dictionary<string, Record> records { get; private set; } = new Dictionary<string, Record>();
public ConcurrentDictionary<string, Record> records { get; private set; } = new ConcurrentDictionary<string, Record>();
internal bool IsTopMost(Result result)
{
if (records.Count == 0 || (result.OriginQuery != null && !records.ContainsKey(result.OriginQuery.RawQuery)))
if (records.IsEmpty || (result.OriginQuery != null && !records.ContainsKey(result.OriginQuery.RawQuery)))
{
return false;
}
@ -23,7 +23,7 @@ namespace Flow.Launcher.Storage
internal void Remove(Result result)
{
records.Remove(result.OriginQuery.RawQuery);
records.Remove(result.OriginQuery.RawQuery, out _);
}
internal void AddOrUpdate(Result result)
@ -34,17 +34,15 @@ namespace Flow.Launcher.Storage
Title = result.Title,
SubTitle = result.SubTitle
};
records[result.OriginQuery.RawQuery] = record;
records.AddOrUpdate(result.OriginQuery.RawQuery, record, (key, oldValue) => record);
}
public void Load(Dictionary<string, Record> dictionary)
{
records = dictionary;
records = new ConcurrentDictionary<string, Record>(dictionary);
}
}
public class Record
{
public string Title { get; set; }