Skip to content

Commit ad254be

Browse files
committed
Add resetCache parameter and improve indexing logic
Introduced an optional `resetCache` parameter to `IndexWin32ProgramsAsync` and `IndexUwpProgramsAsync` to allow finer control over cache resets during indexing. Updated `IndexProgramsAsync` to explicitly pass `true` for cache resets. Modified `DisableProgramAsync` to return a `bool` indicating success and adjusted its logic to prevent unnecessary cache resets when reindexing. Ensured it returns `false` if the program is already disabled. Replaced nullable `PackageCatalog?` with non-nullable `PackageCatalog` in `UWPPackage.cs` to ensure proper initialization. Updated `WatchPackageChangeAsync` and `WatchDirectory` to call indexing methods with `resetCache` set to `true` when changes are detected. Improved cache reset logic across methods by conditionally invoking `ResetCache` based on the `resetCache` parameter, enhancing performance and maintainability.
1 parent 14369d8 commit ad254be

3 files changed

Lines changed: 27 additions & 17 deletions

File tree

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ static void WatchProgramUpdate()
334334
}
335335
}
336336

337-
public static async Task IndexWin32ProgramsAsync()
337+
public static async Task IndexWin32ProgramsAsync(bool resetCache = true)
338338
{
339339
await _win32sLock.WaitAsync();
340340
try
@@ -345,7 +345,10 @@ public static async Task IndexWin32ProgramsAsync()
345345
{
346346
_win32s.Add(win32);
347347
}
348-
ResetCache();
348+
if (resetCache)
349+
{
350+
ResetCache();
351+
}
349352
await Context.API.SaveCacheBinaryStorageAsync<List<Win32>>(Win32CacheName, Context.CurrentPluginMetadata.PluginCacheDirectoryPath);
350353
lock (_lastIndexTimeLock)
351354
{
@@ -362,7 +365,7 @@ public static async Task IndexWin32ProgramsAsync()
362365
}
363366
}
364367

365-
public static async Task IndexUwpProgramsAsync()
368+
public static async Task IndexUwpProgramsAsync(bool resetCache = true)
366369
{
367370
await _uwpsLock.WaitAsync();
368371
try
@@ -373,7 +376,10 @@ public static async Task IndexUwpProgramsAsync()
373376
{
374377
_uwps.Add(uwp);
375378
}
376-
ResetCache();
379+
if (resetCache)
380+
{
381+
ResetCache();
382+
}
377383
await Context.API.SaveCacheBinaryStorageAsync<List<UWPApp>>(UwpCacheName, Context.CurrentPluginMetadata.PluginCacheDirectoryPath);
378384
lock (_lastIndexTimeLock)
379385
{
@@ -394,12 +400,12 @@ public static async Task IndexProgramsAsync()
394400
{
395401
var win32Task = Task.Run(async () =>
396402
{
397-
await Context.API.StopwatchLogInfoAsync(ClassName, "Win32Program index cost", IndexWin32ProgramsAsync);
403+
await Context.API.StopwatchLogInfoAsync(ClassName, "Win32Program index cost", () => IndexWin32ProgramsAsync(true));
398404
});
399405

400406
var uwpTask = Task.Run(async () =>
401407
{
402-
await Context.API.StopwatchLogInfoAsync(ClassName, "UWPProgram index cost", IndexUwpProgramsAsync);
408+
await Context.API.StopwatchLogInfoAsync(ClassName, "UWPProgram index cost", () => IndexUwpProgramsAsync(true));
403409
});
404410

405411
await Task.WhenAll(win32Task, uwpTask).ConfigureAwait(false);
@@ -446,8 +452,10 @@ public List<Result> LoadContextMenus(Result selectedResult)
446452
{
447453
try
448454
{
449-
await DisableProgramAsync(program);
450-
ResetCache();
455+
if (await DisableProgramAsync(program))
456+
{
457+
ResetCache();
458+
}
451459
Context.API.ShowMsg(
452460
Context.API.GetTranslation("flowlauncher_plugin_program_disable_dlgtitle_success"),
453461
Context.API.GetTranslation(
@@ -469,10 +477,10 @@ public List<Result> LoadContextMenus(Result selectedResult)
469477
return menuOptions;
470478
}
471479

472-
private static async Task DisableProgramAsync(IProgram programToDelete)
480+
private static async Task<bool> DisableProgramAsync(IProgram programToDelete)
473481
{
474482
if (_settings.DisabledProgramSources.Any(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier))
475-
return;
483+
return false;
476484

477485
await _uwpsLock.WaitAsync();
478486
try
@@ -483,8 +491,8 @@ private static async Task DisableProgramAsync(IProgram programToDelete)
483491
program.Enabled = false;
484492
_settings.DisabledProgramSources.Add(new ProgramSource(program));
485493
// Reindex UWP programs
486-
_ = Task.Run(IndexUwpProgramsAsync);
487-
return;
494+
_ = Task.Run(() => IndexUwpProgramsAsync(false));
495+
return true;
488496
}
489497
}
490498
finally
@@ -501,14 +509,16 @@ private static async Task DisableProgramAsync(IProgram programToDelete)
501509
program.Enabled = false;
502510
_settings.DisabledProgramSources.Add(new ProgramSource(program));
503511
// Reindex Win32 programs
504-
_ = Task.Run(IndexWin32ProgramsAsync);
505-
return;
512+
_ = Task.Run(() => IndexWin32ProgramsAsync(false));
513+
return true;
506514
}
507515
}
508516
finally
509517
{
510518
_win32sLock.Release();
511519
}
520+
521+
return false;
512522
}
513523

514524
public static void StartProcess(Func<ProcessStartInfo, Process> runProcess, ProcessStartInfo info)

Plugins/Flow.Launcher.Plugin.Program/Programs/UWPPackage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ private static IEnumerable<Package> CurrentUserPackages()
290290
}
291291

292292
private static readonly Channel<byte> PackageChangeChannel = Channel.CreateBounded<byte>(1);
293-
private static PackageCatalog? catalog;
293+
private static PackageCatalog catalog;
294294

295295
public static async Task WatchPackageChangeAsync()
296296
{
@@ -317,7 +317,7 @@ public static async Task WatchPackageChangeAsync()
317317
{
318318
await Task.Delay(3000).ConfigureAwait(false);
319319
PackageChangeChannel.Reader.TryRead(out _);
320-
await Task.Run(Main.IndexUwpProgramsAsync);
320+
await Task.Run(() => Main.IndexUwpProgramsAsync(true));
321321
}
322322
}
323323
}

Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ public static async Task MonitorDirectoryChangeAsync()
796796
{
797797
}
798798

799-
await Task.Run(Main.IndexWin32ProgramsAsync);
799+
await Task.Run(() => Main.IndexWin32ProgramsAsync(true));
800800
}
801801
}
802802

0 commit comments

Comments
 (0)