1
- using System . Collections . Generic ;
1
+ using System . Collections . Concurrent ;
2
+ using System . Collections . Generic ;
2
3
using System . Text . Json . Serialization ;
3
4
using Flow . Launcher . Plugin ;
4
5
5
6
namespace Flow . Launcher . Storage
6
7
{
7
- // todo this class is not thread safe.... but used from multiple threads.
8
8
public class TopMostRecord
9
9
{
10
10
[ 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 > ( ) ;
12
12
13
13
internal bool IsTopMost ( Result result )
14
14
{
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 ) ) )
16
16
{
17
17
return false ;
18
18
}
@@ -23,7 +23,7 @@ internal bool IsTopMost(Result result)
23
23
24
24
internal void Remove ( Result result )
25
25
{
26
- records . Remove ( result . OriginQuery . RawQuery ) ;
26
+ records . Remove ( result . OriginQuery . RawQuery , out _ ) ;
27
27
}
28
28
29
29
internal void AddOrUpdate ( Result result )
@@ -34,17 +34,15 @@ internal void AddOrUpdate(Result result)
34
34
Title = result . Title ,
35
35
SubTitle = result . SubTitle
36
36
} ;
37
- records [ result . OriginQuery . RawQuery ] = record ;
38
-
37
+ records . AddOrUpdate ( result . OriginQuery . RawQuery , record , ( key , oldValue ) => record ) ;
39
38
}
40
39
41
40
public void Load ( Dictionary < string , Record > dictionary )
42
41
{
43
- records = dictionary ;
42
+ records = new ConcurrentDictionary < string , Record > ( dictionary ) ;
44
43
}
45
44
}
46
45
47
-
48
46
public class Record
49
47
{
50
48
public string Title { get ; set ; }
0 commit comments