Skip to content

Commit b95bbb3

Browse files
committed
Introduce concurrent directionary for topmost record
1 parent 0720779 commit b95bbb3

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

Flow.Launcher/Storage/TopMostRecord.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Concurrent;
2+
using System.Collections.Generic;
23
using System.Text.Json.Serialization;
34
using Flow.Launcher.Plugin;
45

56
namespace Flow.Launcher.Storage
67
{
7-
// todo this class is not thread safe.... but used from multiple threads.
88
public class TopMostRecord
99
{
1010
[JsonInclude]
11-
public Dictionary<string, Record> records { get; private set; } = new Dictionary<string, Record>();
11+
public ConcurrentDictionary<string, Record> records { get; private set; } = new ConcurrentDictionary<string, Record>();
1212

1313
internal bool IsTopMost(Result result)
1414
{
15-
if (records.Count == 0 || (result.OriginQuery != null && !records.ContainsKey(result.OriginQuery.RawQuery)))
15+
if (records.IsEmpty || (result.OriginQuery != null && !records.ContainsKey(result.OriginQuery.RawQuery)))
1616
{
1717
return false;
1818
}
@@ -23,7 +23,7 @@ internal bool IsTopMost(Result result)
2323

2424
internal void Remove(Result result)
2525
{
26-
records.Remove(result.OriginQuery.RawQuery);
26+
records.Remove(result.OriginQuery.RawQuery, out _);
2727
}
2828

2929
internal void AddOrUpdate(Result result)
@@ -34,17 +34,15 @@ internal void AddOrUpdate(Result result)
3434
Title = result.Title,
3535
SubTitle = result.SubTitle
3636
};
37-
records[result.OriginQuery.RawQuery] = record;
38-
37+
records.AddOrUpdate(result.OriginQuery.RawQuery, record, (key, oldValue) => record);
3938
}
4039

4140
public void Load(Dictionary<string, Record> dictionary)
4241
{
43-
records = dictionary;
42+
records = new ConcurrentDictionary<string, Record>(dictionary);
4443
}
4544
}
4645

47-
4846
public class Record
4947
{
5048
public string Title { get; set; }

0 commit comments

Comments
 (0)