From c11ee2f9e78fa626bd7801fed293d990fdfaf682 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 8 Apr 2025 17:20:57 +0800 Subject: [PATCH] Improve code quality & comments & Fix lock issue --- .../Storage/BinaryStorage.cs | 6 ++-- .../Storage/PluginJsonStorage.cs | 1 + Flow.Launcher.Plugin/Interfaces/ISavable.cs | 9 ++++-- Flow.Launcher/PublicAPIInstance.cs | 6 ---- Plugins/Flow.Launcher.Plugin.Program/Main.cs | 29 +++++++------------ 5 files changed, 20 insertions(+), 31 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs b/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs index f218c5d8d..b5de3b50f 100644 --- a/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/BinaryStorage.cs @@ -42,8 +42,7 @@ namespace Flow.Launcher.Infrastructure.Storage public async ValueTask TryLoadAsync(T defaultData) { - if (Data != null) - return Data; + if (Data != null) return Data; if (File.Exists(FilePath)) { @@ -55,8 +54,7 @@ namespace Flow.Launcher.Infrastructure.Storage } await using var stream = new FileStream(FilePath, FileMode.Open); - var d = await DeserializeAsync(stream, defaultData); - Data = d; + Data = await DeserializeAsync(stream, defaultData); } else { diff --git a/Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs b/Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs index b63e8c1ef..e8cbd70fb 100644 --- a/Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs @@ -20,6 +20,7 @@ namespace Flow.Launcher.Infrastructure.Storage public PluginJsonStorage() { + // C# related, add python related below var dataType = typeof(T); AssemblyName = dataType.Assembly.GetName().Name; DirectoryPath = Path.Combine(DataLocation.PluginSettingsDirectory, AssemblyName); diff --git a/Flow.Launcher.Plugin/Interfaces/ISavable.cs b/Flow.Launcher.Plugin/Interfaces/ISavable.cs index cabd26962..0d23c0fc8 100644 --- a/Flow.Launcher.Plugin/Interfaces/ISavable.cs +++ b/Flow.Launcher.Plugin/Interfaces/ISavable.cs @@ -1,18 +1,21 @@ namespace Flow.Launcher.Plugin { /// - /// 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. /// /// /// For storing plugin settings, prefer /// or . + /// For storing plugin caches, prefer /// or . - /// Once called, your settings will be automatically saved by Flow. + /// Once called, those settings and caches will be automatically saved by Flow. /// public interface ISavable : IFeatures { /// - /// Save additional plugin data, such as cache. + /// Save additional plugin data. /// void Save(); } diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 4dd0268d2..a523f90bb 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -229,9 +229,6 @@ namespace Flow.Launcher } } - /// - /// Save plugin settings. - /// public void SavePluginSettings() { foreach (var value in _pluginJsonStorages.Values) @@ -378,9 +375,6 @@ namespace Flow.Launcher } } - /// - /// Save plugin caches. - /// public void SavePluginCaches() { foreach (var value in _pluginBinaryStorages.Values) diff --git a/Plugins/Flow.Launcher.Plugin.Program/Main.cs b/Plugins/Flow.Launcher.Plugin.Program/Main.cs index a5fbc6c70..561044981 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Main.cs @@ -77,10 +77,6 @@ namespace Flow.Launcher.Plugin.Program private static readonly string WindowsAppPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "WindowsApps"); - static Main() - { - } - public async Task> QueryAsync(Query query, CancellationToken token) { var result = await cache.GetOrCreateAsync(query.Search, async entry => @@ -101,15 +97,15 @@ namespace Flow.Launcher.Plugin.Program .ToArray() : null; return _win32s.Cast() - .Concat(_uwps) - .AsParallel() - .WithCancellation(token) - .Where(HideUninstallersFilter) - .Where(p => HideDuplicatedWindowsAppFilter(p, uwpsDirectories)) - .Where(p => p.Enabled) - .Select(p => p.Result(query.Search, Context.API)) - .Where(r => r?.Score > 0) - .ToList(); + .Concat(_uwps) + .AsParallel() + .WithCancellation(token) + .Where(HideUninstallersFilter) + .Where(p => HideDuplicatedWindowsAppFilter(p, uwpsDirectories)) + .Where(p => p.Enabled) + .Select(p => p.Result(query.Search, Context.API)) + .Where(r => r?.Score > 0) + .ToList(); } catch (OperationCanceledException) { @@ -193,7 +189,6 @@ namespace Flow.Launcher.Plugin.Program await Stopwatch.NormalAsync("|Flow.Launcher.Plugin.Program.Main|Preload programs cost", async () => { var pluginCachePath = Context.CurrentPluginMetadata.PluginCacheDirectoryPath; - FilesFolders.ValidateDirectory(pluginCachePath); static void MoveFile(string sourcePath, string destinationPath) @@ -294,10 +289,10 @@ namespace Flow.Launcher.Plugin.Program { _win32s.Add(win32); } - _win32sLock.Release(); ResetCache(); await Context.API.SaveCacheBinaryStorageAsync>(Win32CacheName, Context.CurrentPluginMetadata.PluginCacheDirectoryPath); _settings.LastIndexTime = DateTime.Now; + _win32sLock.Release(); } public static async Task IndexUwpProgramsAsync() @@ -309,10 +304,10 @@ namespace Flow.Launcher.Plugin.Program { _uwps.Add(uwp); } - _uwpsLock.Release(); ResetCache(); await Context.API.SaveCacheBinaryStorageAsync>(UwpCacheName, Context.CurrentPluginMetadata.PluginCacheDirectoryPath); _settings.LastIndexTime = DateTime.Now; + _uwpsLock.Release(); } public static async Task IndexProgramsAsync() @@ -405,7 +400,6 @@ namespace Flow.Launcher.Plugin.Program } else { - // Release the lock if we cannot find the program _uwpsLock.Release(); } @@ -425,7 +419,6 @@ namespace Flow.Launcher.Plugin.Program } else { - // Release the lock if we cannot find the program _win32sLock.Release(); } }