Improve code quality & comments & Fix lock issue

This commit is contained in:
Jack251970 2025-04-08 17:20:57 +08:00
parent 734c5bb67d
commit c11ee2f9e7
5 changed files with 20 additions and 31 deletions

View file

@ -42,8 +42,7 @@ namespace Flow.Launcher.Infrastructure.Storage
public async ValueTask<T> TryLoadAsync(T defaultData) public async ValueTask<T> TryLoadAsync(T defaultData)
{ {
if (Data != null) if (Data != null) return Data;
return Data;
if (File.Exists(FilePath)) if (File.Exists(FilePath))
{ {
@ -55,8 +54,7 @@ namespace Flow.Launcher.Infrastructure.Storage
} }
await using var stream = new FileStream(FilePath, FileMode.Open); await using var stream = new FileStream(FilePath, FileMode.Open);
var d = await DeserializeAsync(stream, defaultData); Data = await DeserializeAsync(stream, defaultData);
Data = d;
} }
else else
{ {

View file

@ -20,6 +20,7 @@ namespace Flow.Launcher.Infrastructure.Storage
public PluginJsonStorage() public PluginJsonStorage()
{ {
// C# related, add python related below
var dataType = typeof(T); var dataType = typeof(T);
AssemblyName = dataType.Assembly.GetName().Name; AssemblyName = dataType.Assembly.GetName().Name;
DirectoryPath = Path.Combine(DataLocation.PluginSettingsDirectory, AssemblyName); DirectoryPath = Path.Combine(DataLocation.PluginSettingsDirectory, AssemblyName);

View file

@ -1,18 +1,21 @@
namespace Flow.Launcher.Plugin namespace Flow.Launcher.Plugin
{ {
/// <summary> /// <summary>
/// Inherit this interface if additional data e.g. cache needs to be saved. /// Inherit this interface if additional data.
/// If you need to save data which is not a setting or cache,
/// please implement this interface.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// For storing plugin settings, prefer <see cref="IPublicAPI.LoadSettingJsonStorage{T}"/> /// For storing plugin settings, prefer <see cref="IPublicAPI.LoadSettingJsonStorage{T}"/>
/// or <see cref="IPublicAPI.SaveSettingJsonStorage{T}"/>. /// or <see cref="IPublicAPI.SaveSettingJsonStorage{T}"/>.
/// For storing plugin caches, prefer <see cref="IPublicAPI.LoadCacheBinaryStorageAsync{T}"/>
/// or <see cref="IPublicAPI.SaveCacheBinaryStorageAsync{T}(string, string)"/>. /// or <see cref="IPublicAPI.SaveCacheBinaryStorageAsync{T}(string, string)"/>.
/// Once called, your settings will be automatically saved by Flow. /// Once called, those settings and caches will be automatically saved by Flow.
/// </remarks> /// </remarks>
public interface ISavable : IFeatures public interface ISavable : IFeatures
{ {
/// <summary> /// <summary>
/// Save additional plugin data, such as cache. /// Save additional plugin data.
/// </summary> /// </summary>
void Save(); void Save();
} }

View file

@ -229,9 +229,6 @@ namespace Flow.Launcher
} }
} }
/// <summary>
/// Save plugin settings.
/// </summary>
public void SavePluginSettings() public void SavePluginSettings()
{ {
foreach (var value in _pluginJsonStorages.Values) foreach (var value in _pluginJsonStorages.Values)
@ -378,9 +375,6 @@ namespace Flow.Launcher
} }
} }
/// <summary>
/// Save plugin caches.
/// </summary>
public void SavePluginCaches() public void SavePluginCaches()
{ {
foreach (var value in _pluginBinaryStorages.Values) foreach (var value in _pluginBinaryStorages.Values)

View file

@ -77,10 +77,6 @@ namespace Flow.Launcher.Plugin.Program
private static readonly string WindowsAppPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "WindowsApps"); private static readonly string WindowsAppPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "WindowsApps");
static Main()
{
}
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token) public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
{ {
var result = await cache.GetOrCreateAsync(query.Search, async entry => var result = await cache.GetOrCreateAsync(query.Search, async entry =>
@ -101,15 +97,15 @@ namespace Flow.Launcher.Plugin.Program
.ToArray() : null; .ToArray() : null;
return _win32s.Cast<IProgram>() return _win32s.Cast<IProgram>()
.Concat(_uwps) .Concat(_uwps)
.AsParallel() .AsParallel()
.WithCancellation(token) .WithCancellation(token)
.Where(HideUninstallersFilter) .Where(HideUninstallersFilter)
.Where(p => HideDuplicatedWindowsAppFilter(p, uwpsDirectories)) .Where(p => HideDuplicatedWindowsAppFilter(p, uwpsDirectories))
.Where(p => p.Enabled) .Where(p => p.Enabled)
.Select(p => p.Result(query.Search, Context.API)) .Select(p => p.Result(query.Search, Context.API))
.Where(r => r?.Score > 0) .Where(r => r?.Score > 0)
.ToList(); .ToList();
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
@ -193,7 +189,6 @@ namespace Flow.Launcher.Plugin.Program
await Stopwatch.NormalAsync("|Flow.Launcher.Plugin.Program.Main|Preload programs cost", async () => await Stopwatch.NormalAsync("|Flow.Launcher.Plugin.Program.Main|Preload programs cost", async () =>
{ {
var pluginCachePath = Context.CurrentPluginMetadata.PluginCacheDirectoryPath; var pluginCachePath = Context.CurrentPluginMetadata.PluginCacheDirectoryPath;
FilesFolders.ValidateDirectory(pluginCachePath); FilesFolders.ValidateDirectory(pluginCachePath);
static void MoveFile(string sourcePath, string destinationPath) static void MoveFile(string sourcePath, string destinationPath)
@ -294,10 +289,10 @@ namespace Flow.Launcher.Plugin.Program
{ {
_win32s.Add(win32); _win32s.Add(win32);
} }
_win32sLock.Release();
ResetCache(); ResetCache();
await Context.API.SaveCacheBinaryStorageAsync<List<Win32>>(Win32CacheName, Context.CurrentPluginMetadata.PluginCacheDirectoryPath); await Context.API.SaveCacheBinaryStorageAsync<List<Win32>>(Win32CacheName, Context.CurrentPluginMetadata.PluginCacheDirectoryPath);
_settings.LastIndexTime = DateTime.Now; _settings.LastIndexTime = DateTime.Now;
_win32sLock.Release();
} }
public static async Task IndexUwpProgramsAsync() public static async Task IndexUwpProgramsAsync()
@ -309,10 +304,10 @@ namespace Flow.Launcher.Plugin.Program
{ {
_uwps.Add(uwp); _uwps.Add(uwp);
} }
_uwpsLock.Release();
ResetCache(); ResetCache();
await Context.API.SaveCacheBinaryStorageAsync<List<UWPApp>>(UwpCacheName, Context.CurrentPluginMetadata.PluginCacheDirectoryPath); await Context.API.SaveCacheBinaryStorageAsync<List<UWPApp>>(UwpCacheName, Context.CurrentPluginMetadata.PluginCacheDirectoryPath);
_settings.LastIndexTime = DateTime.Now; _settings.LastIndexTime = DateTime.Now;
_uwpsLock.Release();
} }
public static async Task IndexProgramsAsync() public static async Task IndexProgramsAsync()
@ -405,7 +400,6 @@ namespace Flow.Launcher.Plugin.Program
} }
else else
{ {
// Release the lock if we cannot find the program
_uwpsLock.Release(); _uwpsLock.Release();
} }
@ -425,7 +419,6 @@ namespace Flow.Launcher.Plugin.Program
} }
else else
{ {
// Release the lock if we cannot find the program
_win32sLock.Release(); _win32sLock.Release();
} }
} }