diff --git a/.github/actions/spelling/expect.txt b/.github/actions/spelling/expect.txt index 78ae8476b..3e2f24c72 100644 --- a/.github/actions/spelling/expect.txt +++ b/.github/actions/spelling/expect.txt @@ -78,6 +78,7 @@ WCA_ACCENT_POLICY HGlobal dopusrt firefox +Firefox msedge svgc ime diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index 6f2b24f02..a13d6c929 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -10,6 +10,7 @@ using Flow.Launcher.Plugin.BrowserBookmark.Views; using System.IO; using System.Threading.Channels; using System.Threading.Tasks; +using System.Threading; namespace Flow.Launcher.Plugin.BrowserBookmark { @@ -20,20 +21,39 @@ namespace Flow.Launcher.Plugin.BrowserBookmark private static List cachedBookmarks = new List(); private static Settings _settings; - + + private static bool initialized = false; + public void Init(PluginInitContext context) { Main.context = context; - + _settings = context.API.LoadSettingJsonStorage(); - cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings); + LoadBookmarksIfEnabled(); + } - _ = MonitorRefreshQueue(); + private static void LoadBookmarksIfEnabled() + { + if (context.CurrentPluginMetadata.Disabled) + { + // Don't load or monitor files if disabled + return; + } + + cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings); + _ = MonitorRefreshQueueAsync(); + initialized = true; } public List Query(Query query) { + // For when the plugin being previously disabled and is now renabled + if (!initialized) + { + LoadBookmarksIfEnabled(); + } + string param = query.Search.TrimStart(); // Should top results be returned? (true if no search parameters have been passed) @@ -86,17 +106,24 @@ namespace Flow.Launcher.Plugin.BrowserBookmark private static Channel refreshQueue = Channel.CreateBounded(1); - private async Task MonitorRefreshQueue() + private static SemaphoreSlim fileMonitorSemaphore = new(1, 1); + + private static async Task MonitorRefreshQueueAsync() { + if (fileMonitorSemaphore.CurrentCount < 1) + { + return; + } + await fileMonitorSemaphore.WaitAsync(); var reader = refreshQueue.Reader; while (await reader.WaitToReadAsync()) { - await Task.Delay(2000); if (reader.TryRead(out _)) { - ReloadData(); + ReloadAllBookmarks(false); } } + fileMonitorSemaphore.Release(); } private static readonly List Watchers = new(); @@ -104,17 +131,19 @@ namespace Flow.Launcher.Plugin.BrowserBookmark internal static void RegisterBookmarkFile(string path) { var directory = Path.GetDirectoryName(path); - if (!Directory.Exists(directory)) - return; - var watcher = new FileSystemWatcher(directory!); - if (File.Exists(path)) + if (!Directory.Exists(directory) || !File.Exists(path)) { - var fileName = Path.GetFileName(path); - watcher.Filter = fileName; + return; } + if (Watchers.Any(x => x.Path.Equals(directory, StringComparison.OrdinalIgnoreCase))) + { + return; + } + + var watcher = new FileSystemWatcher(directory!); + watcher.Filter = Path.GetFileName(path); watcher.NotifyFilter = NotifyFilters.FileName | - NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Size; @@ -129,7 +158,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark }; watcher.EnableRaisingEvents = true; - + Watchers.Add(watcher); } @@ -138,11 +167,12 @@ namespace Flow.Launcher.Plugin.BrowserBookmark ReloadAllBookmarks(); } - public static void ReloadAllBookmarks() + public static void ReloadAllBookmarks(bool disposeFileWatchers = true) { cachedBookmarks.Clear(); - - cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings); + if (disposeFileWatchers) + DisposeFileWatchers(); + LoadBookmarksIfEnabled(); } public string GetTranslatedPluginTitle() @@ -196,12 +226,19 @@ namespace Flow.Launcher.Plugin.BrowserBookmark { internal string Url { get; set; } } + public void Dispose() + { + DisposeFileWatchers(); + } + + private static void DisposeFileWatchers() { foreach (var watcher in Watchers) { watcher.Dispose(); } + Watchers.Clear(); } } }