diff --git a/Flow.Launcher/Storage/TopMostRecord.cs b/Flow.Launcher/Storage/TopMostRecord.cs index a088353f5..069b670cb 100644 --- a/Flow.Launcher/Storage/TopMostRecord.cs +++ b/Flow.Launcher/Storage/TopMostRecord.cs @@ -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 records { get; private set; } = new Dictionary(); + public ConcurrentDictionary records { get; private set; } = new ConcurrentDictionary(); 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 dictionary) { - records = dictionary; + records = new ConcurrentDictionary(dictionary); } } - public class Record { public string Title { get; set; }