1- using System . Collections . Generic ;
1+ using System . Collections . Concurrent ;
2+ using System . Collections . Generic ;
23using System . Text . Json . Serialization ;
34using Flow . Launcher . Plugin ;
45
56namespace 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 || ! records . ContainsKey ( result . OriginQuery . RawQuery ) )
15+ if ( records . IsEmpty || result . OriginQuery == null ||
16+ ! records . TryGetValue ( result . OriginQuery . RawQuery , out var value ) )
1617 {
1718 return false ;
1819 }
1920
2021 // 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 ) ;
2223 }
2324
2425 internal void Remove ( Result result )
2526 {
26- records . Remove ( result . OriginQuery . RawQuery ) ;
27+ records . Remove ( result . OriginQuery . RawQuery , out _ ) ;
2728 }
2829
2930 internal void AddOrUpdate ( Result result )
@@ -34,17 +35,15 @@ internal void AddOrUpdate(Result result)
3435 Title = result . Title ,
3536 SubTitle = result . SubTitle
3637 } ;
37- records [ result . OriginQuery . RawQuery ] = record ;
38-
38+ records . AddOrUpdate ( result . OriginQuery . RawQuery , record , ( key , oldValue ) => record ) ;
3939 }
4040
4141 public void Load ( Dictionary < string , Record > dictionary )
4242 {
43- records = dictionary ;
43+ records = new ConcurrentDictionary < string , Record > ( dictionary ) ;
4444 }
4545 }
4646
47-
4847 public class Record
4948 {
5049 public string Title { get ; set ; }
0 commit comments