Skip to content

Commit 5430756

Browse files
authored
Merge pull request #293 from taooceros/SelectedCountOptimize
fix TopMostRecord and UserSelectedRecord
2 parents 02211f2 + 6655a8f commit 5430756

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

Flow.Launcher.Infrastructure/Storage/JsonStorage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Flow.Launcher.Infrastructure.Storage
99
/// <summary>
1010
/// Serialize object using json format.
1111
/// </summary>
12-
public class JsonStrorage<T>
12+
public class JsonStrorage<T> where T : new()
1313
{
1414
private readonly JsonSerializerOptions _serializerSettings;
1515
private T _data;
@@ -76,7 +76,7 @@ private void LoadDefault()
7676
BackupOriginFile();
7777
}
7878

79-
_data = JsonSerializer.Deserialize<T>("{}", _serializerSettings);
79+
_data = new T();
8080
Save();
8181
}
8282

Flow.Launcher/Storage/TopMostRecord.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using System.Text.Json;
4+
using System.Text.Json.Serialization;
45
using Flow.Launcher.Plugin;
56

67
namespace Flow.Launcher.Storage
78
{
89
// todo this class is not thread safe.... but used from multiple threads.
910
public class TopMostRecord
1011
{
11-
private Dictionary<string, Record> records = new Dictionary<string, Record>();
12+
/// <summary>
13+
/// You should not directly access this field
14+
/// <para>
15+
/// It is public due to System.Text.Json limitation in version 3.1
16+
/// </para>
17+
/// </summary>
18+
/// TODO: Set it to private
19+
public Dictionary<string, Record> records { get; set; } = new Dictionary<string, Record>();
1220

1321
internal bool IsTopMost(Result result)
1422
{
15-
if (records.Count == 0)
23+
if (records.Count == 0 || !records.ContainsKey(result.OriginQuery.RawQuery))
1624
{
1725
return false;
1826
}
1927

20-
// since this dictionary should be very small (or empty) going over it should be pretty fast.
21-
return records.Any(o => o.Value.Title == result.Title
22-
&& o.Value.SubTitle == result.SubTitle
23-
&& o.Value.PluginID == result.PluginID
24-
&& o.Key == result.OriginQuery.RawQuery);
28+
// since this dictionary should be very small (or empty) going over it should be pretty fast.
29+
return records[result.OriginQuery.RawQuery].Equals(result);
2530
}
2631

2732
internal void Remove(Result result)
@@ -53,5 +58,12 @@ public class Record
5358
public string Title { get; set; }
5459
public string SubTitle { get; set; }
5560
public string PluginID { get; set; }
61+
62+
public bool Equals(Result r)
63+
{
64+
return Title == r.Title
65+
&& SubTitle == r.SubTitle
66+
&& PluginID == r.PluginID;
67+
}
5668
}
5769
}

Flow.Launcher/Storage/UserSelectedRecord.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
using System.Collections.Generic;
2+
using System.Text.Json.Serialization;
23
using Flow.Launcher.Infrastructure.Storage;
34
using Flow.Launcher.Plugin;
45

56
namespace Flow.Launcher.Storage
67
{
78
public class UserSelectedRecord
89
{
9-
private Dictionary<string, int> records = new Dictionary<string, int>();
10+
/// <summary>
11+
/// You should not directly access this field
12+
/// <para>
13+
/// It is public due to System.Text.Json limitation in version 3.1
14+
/// </para>
15+
/// </summary>
16+
/// TODO: Set it to private
17+
[JsonPropertyName("records")]
18+
public Dictionary<string, int> records { get; set; }
19+
20+
public UserSelectedRecord()
21+
{
22+
records = new Dictionary<string, int>();
23+
}
1024

1125
public void Add(Result result)
1226
{
1327
var key = result.ToString();
14-
if (records.TryGetValue(key, out int value))
28+
if (records.ContainsKey(key))
1529
{
16-
records[key] = value + 1;
30+
records[key]++;
1731
}
1832
else
1933
{

0 commit comments

Comments
 (0)