Skip to content

Commit 87e7ff5

Browse files
committed
Remove unneeded logic and add a background reindex at startup
1 parent 823fa38 commit 87e7ff5

File tree

1 file changed

+32
-49
lines changed
  • Plugins/Flow.Launcher.Plugin.Program

1 file changed

+32
-49
lines changed

Plugins/Flow.Launcher.Plugin.Program/Main.cs

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I
2424
internal static UWP.Application[] _uwps { get; set; }
2525
internal static Settings _settings { get; set; }
2626

27-
private static bool IsStartupIndexProgramsRequired => _settings.LastIndexTime.AddDays(3) < DateTime.Today;
2827

2928
internal static PluginInitContext Context { get; private set; }
3029

@@ -51,29 +50,25 @@ public void Save()
5150

5251
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
5352
{
54-
55-
if (IsStartupIndexProgramsRequired)
56-
_ = IndexPrograms();
57-
5853
var result = await cache.GetOrCreateAsync(query.Search, async entry =>
59-
{
60-
var resultList = await Task.Run(() =>
61-
_win32s.Cast<IProgram>()
62-
.Concat(_uwps)
63-
.AsParallel()
64-
.WithCancellation(token)
65-
.Where(p => p.Enabled)
66-
.Select(p => p.Result(query.Search, Context.API))
67-
.Where(r => r?.Score > 0)
68-
.ToList());
69-
70-
resultList = resultList.Any() ? resultList : emptyResults;
71-
72-
entry.SetSize(resultList.Count);
73-
entry.SetSlidingExpiration(TimeSpan.FromHours(8));
74-
75-
return resultList;
76-
});
54+
{
55+
var resultList = await Task.Run(() =>
56+
_win32s.Cast<IProgram>()
57+
.Concat(_uwps)
58+
.AsParallel()
59+
.WithCancellation(token)
60+
.Where(p => p.Enabled)
61+
.Select(p => p.Result(query.Search, Context.API))
62+
.Where(r => r?.Score > 0)
63+
.ToList());
64+
65+
resultList = resultList.Any() ? resultList : emptyResults;
66+
67+
entry.SetSize(resultList.Count);
68+
entry.SetSlidingExpiration(TimeSpan.FromHours(8));
69+
70+
return resultList;
71+
});
7772

7873
return result;
7974
}
@@ -84,49 +79,35 @@ public async Task InitAsync(PluginInitContext context)
8479

8580
_settings = context.API.LoadSettingJsonStorage<Settings>();
8681

87-
await Task.Yield();
88-
8982
Stopwatch.Normal("|Flow.Launcher.Plugin.Program.Main|Preload programs cost", () =>
9083
{
9184
_win32Storage = new BinaryStorage<Win32[]>("Win32");
92-
_win32s = _win32Storage.TryLoad(new Win32[] { });
85+
_win32s = _win32Storage.TryLoad(new Win32[]
86+
{
87+
});
9388
_uwpStorage = new BinaryStorage<UWP.Application[]>("UWP");
94-
_uwps = _uwpStorage.TryLoad(new UWP.Application[] { });
89+
_uwps = _uwpStorage.TryLoad(new UWP.Application[]
90+
{
91+
});
9592
});
9693
Log.Info($"|Flow.Launcher.Plugin.Program.Main|Number of preload win32 programs <{_win32s.Length}>");
9794
Log.Info($"|Flow.Launcher.Plugin.Program.Main|Number of preload uwps <{_uwps.Length}>");
9895

99-
100-
bool indexedWinApps = false;
101-
bool indexedUWPApps = false;
96+
bool cacheEmpty = !_win32s.Any() && !_uwps.Any();
10297

10398
var a = Task.Run(() =>
10499
{
105-
if (IsStartupIndexProgramsRequired || !_win32s.Any())
106-
{
107-
Stopwatch.Normal("|Flow.Launcher.Plugin.Program.Main|Win32Program index cost", IndexWin32Programs);
108-
indexedWinApps = true;
109-
}
100+
Stopwatch.Normal("|Flow.Launcher.Plugin.Program.Main|Win32Program index cost", IndexWin32Programs);
110101
});
111102

112103
var b = Task.Run(() =>
113104
{
114-
if (IsStartupIndexProgramsRequired || !_uwps.Any())
115-
{
116-
Stopwatch.Normal("|Flow.Launcher.Plugin.Program.Main|Win32Program index cost", IndexUwpPrograms);
117-
indexedUWPApps = true;
118-
}
105+
Stopwatch.Normal("|Flow.Launcher.Plugin.Program.Main|Win32Program index cost", IndexUwpPrograms);
119106
});
120107

121-
var indexTask = Task.WhenAll(a, b).ContinueWith(t =>
122-
{
123-
if (indexedWinApps && indexedUWPApps)
124-
_settings.LastIndexTime = DateTime.Today;
125-
});
108+
if (cacheEmpty)
109+
await Task.WhenAll(a, b);
126110

127-
if (!(_win32s.Any() && _uwps.Any()))
128-
await indexTask;
129-
130111
Win32.WatchProgramUpdate(_settings);
131112
UWP.WatchPackageChange();
132113
}
@@ -141,7 +122,9 @@ public static void IndexUwpPrograms()
141122
{
142123
var windows10 = new Version(10, 0);
143124
var support = Environment.OSVersion.Version.Major >= windows10.Major;
144-
var applications = support ? UWP.All() : new UWP.Application[] { };
125+
var applications = support ? UWP.All() : new UWP.Application[]
126+
{
127+
};
145128
_uwps = applications;
146129
}
147130

0 commit comments

Comments
 (0)