mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Improve cancellation, locking, and logging mechanisms
Enhanced cancellation handling by adding `token.IsCancellationRequested` checks to improve responsiveness. Refactored locking mechanisms for `_win32sLock` and `_uwpsLock` using `try-finally` blocks to ensure proper acquisition and release, improving thread safety and preventing deadlocks. Reorganized Win32 and UWP program querying logic for better modularity and readability. Replaced shared collection access with local variables to improve clarity and maintain thread safety. Simplified empty result handling by directly returning `emptyResults` when canceled. Removed redundant debug log statements to reduce verbosity and updated remaining logs for clarity. Suppressed unused result warnings by replacing direct calls to `EverythingApiDllImport.Everything_GetMajorVersion()` with null-coalescing assignments.
This commit is contained in:
parent
db0c86d50c
commit
3d8fd1d352
2 changed files with 31 additions and 21 deletions
|
|
@ -52,7 +52,10 @@ namespace Flow.Launcher.Plugin.Explorer.Search.Everything
|
|||
|
||||
try
|
||||
{
|
||||
EverythingApiDllImport.Everything_GetMajorVersion();
|
||||
if (token.IsCancellationRequested)
|
||||
return false;
|
||||
|
||||
_ = EverythingApiDllImport.Everything_GetMajorVersion();
|
||||
var result = EverythingApiDllImport.Everything_GetLastError() != StateCode.IPCError;
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,17 +80,37 @@ namespace Flow.Launcher.Plugin.Program
|
|||
|
||||
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
|
||||
{
|
||||
Context.API.LogDebug(ClassName, $"Query received: {query.Search}");
|
||||
var result = await cache.GetOrCreateAsync(query.Search, async entry =>
|
||||
{
|
||||
Context.API.LogDebug(ClassName, $"Cache miss for query: {query.Search}");
|
||||
var resultList = await Task.Run(async () =>
|
||||
{
|
||||
Context.API.LogDebug(ClassName, "Acquiring locks for querying win32 programs");
|
||||
Context.API.LogDebug(ClassName, "Preparing win32 programs");
|
||||
List<Win32> win32s;
|
||||
await _win32sLock.WaitAsync();
|
||||
Context.API.LogDebug(ClassName, "Acquiring locks for querying uwp programs");
|
||||
try
|
||||
{
|
||||
win32s = [.. _win32s];
|
||||
if (token.IsCancellationRequested) return emptyResults;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_win32sLock.Release();
|
||||
}
|
||||
|
||||
Context.API.LogDebug(ClassName, "Preparing UWP programs");
|
||||
List<UWPApp> uwps;
|
||||
await _uwpsLock.WaitAsync();
|
||||
Context.API.LogDebug(ClassName, "Locks acquired for querying programs");
|
||||
try
|
||||
{
|
||||
uwps = [.. _uwps];
|
||||
if (token.IsCancellationRequested) return emptyResults;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_uwpsLock.Release();
|
||||
}
|
||||
|
||||
Context.API.LogDebug(ClassName, "Start hanlding programs");
|
||||
try
|
||||
{
|
||||
// Collect all UWP Windows app directories
|
||||
|
|
@ -100,10 +120,9 @@ namespace Flow.Launcher.Plugin.Program
|
|||
.Select(uwp => uwp.Location.TrimEnd('\\')) // Remove trailing slash
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray() : null;
|
||||
Context.API.LogDebug(ClassName, "Start filtering and selecting programs");
|
||||
|
||||
return _win32s.Cast<IProgram>()
|
||||
.Concat(_uwps)
|
||||
return win32s.Cast<IProgram>()
|
||||
.Concat(uwps)
|
||||
.AsParallel()
|
||||
.WithCancellation(token)
|
||||
.Where(HideUninstallersFilter)
|
||||
|
|
@ -115,22 +134,14 @@ namespace Flow.Launcher.Plugin.Program
|
|||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
Context.API.LogDebug(ClassName, "Query operation was canceled");
|
||||
return emptyResults;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_uwpsLock.Release();
|
||||
_win32sLock.Release();
|
||||
}
|
||||
}, token);
|
||||
|
||||
resultList = resultList.Count != 0 ? resultList : emptyResults;
|
||||
|
||||
Context.API.LogDebug(ClassName, $"Query completed with {resultList.Count} results");
|
||||
entry.SetSize(resultList.Count);
|
||||
entry.SetSlidingExpiration(TimeSpan.FromHours(8));
|
||||
Context.API.LogDebug(ClassName, $"Caching results for query: {query.Search} with {resultList.Count} items");
|
||||
|
||||
return resultList;
|
||||
});
|
||||
|
|
@ -319,7 +330,6 @@ namespace Flow.Launcher.Plugin.Program
|
|||
try
|
||||
{
|
||||
var win32S = Win32.All(_settings);
|
||||
Context.API.LogDebug(ClassName, "Get all Win32 programs");
|
||||
_win32s.Clear();
|
||||
foreach (var win32 in win32S)
|
||||
{
|
||||
|
|
@ -339,7 +349,6 @@ namespace Flow.Launcher.Plugin.Program
|
|||
{
|
||||
_win32sLock.Release();
|
||||
}
|
||||
Context.API.LogDebug(ClassName, "End indexing Win32 programs");
|
||||
}
|
||||
|
||||
public static async Task IndexUwpProgramsAsync()
|
||||
|
|
@ -350,7 +359,6 @@ namespace Flow.Launcher.Plugin.Program
|
|||
try
|
||||
{
|
||||
var uwps = UWPPackage.All(_settings);
|
||||
Context.API.LogDebug(ClassName, "Get all Uwp programs");
|
||||
_uwps.Clear();
|
||||
foreach (var uwp in uwps)
|
||||
{
|
||||
|
|
@ -370,7 +378,6 @@ namespace Flow.Launcher.Plugin.Program
|
|||
{
|
||||
_uwpsLock.Release();
|
||||
}
|
||||
Context.API.LogDebug(ClassName, "End indexing Uwp programs");
|
||||
}
|
||||
|
||||
public static async Task IndexProgramsAsync()
|
||||
|
|
|
|||
Loading…
Reference in a new issue