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 || ! records . ContainsKey ( result . OriginQuery . RawQuery ) )
15
+ if ( records . IsEmpty || result . OriginQuery == null ||
16
+ ! records . TryGetValue ( result . OriginQuery . RawQuery , out var value ) )
16
17
{
17
18
return false ;
18
19
}
19
20
20
21
// since this dictionary should be very small (or empty) going over it should be pretty fast.
21
- return records [ result . OriginQuery . RawQuery ] . Equals ( result ) ;
22
+ return value . Equals ( result ) ;
22
23
}
23
24
24
25
internal void Remove ( Result result )
25
26
{
26
- records . Remove ( result . OriginQuery . RawQuery ) ;
27
+ records . Remove ( result . OriginQuery . RawQuery , out _ ) ;
27
28
}
28
29
29
30
internal void AddOrUpdate ( Result result )
@@ -34,17 +35,15 @@ internal void AddOrUpdate(Result result)
34
35
Title = result . Title ,
35
36
SubTitle = result . SubTitle
36
37
} ;
37
- records [ result . OriginQuery . RawQuery ] = record ;
38
-
38
+ records . AddOrUpdate ( result . OriginQuery . RawQuery , record , ( key , oldValue ) => record ) ;
39
39
}
40
40
41
41
public void Load ( Dictionary < string , Record > dictionary )
42
42
{
43
- records = dictionary ;
43
+ records = new ConcurrentDictionary < string , Record > ( dictionary ) ;
44
44
}
45
45
}
46
46
47
-
48
47
public class Record
49
48
{
50
49
public string Title { get ; set ; }
0 commit comments