diff --git a/.github/actions/spelling/expect.txt b/.github/actions/spelling/expect.txt index 78ae8476bc6..3e2f24c722f 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 6f2b24f02d1..a13d6c929fe 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -10,6 +10,7 @@ using System.IO; using System.Threading.Channels; using System.Threading.Tasks; +using System.Threading; namespace Flow.Launcher.Plugin.BrowserBookmark { @@ -20,20 +21,39 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex 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 @@ public List Query(Query query) 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 @@ private async Task MonitorRefreshQueue() internal static void RegisterBookmarkFile(string path) { var directory = Path.GetDirectoryName(path); - if (!Directory.Exists(directory)) + if (!Directory.Exists(directory) || !File.Exists(path)) + { return; - var watcher = new FileSystemWatcher(directory!); - if (File.Exists(path)) + } + if (Watchers.Any(x => x.Path.Equals(directory, StringComparison.OrdinalIgnoreCase))) { - var fileName = Path.GetFileName(path); - watcher.Filter = fileName; + 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 @@ internal static void RegisterBookmarkFile(string path) }; watcher.EnableRaisingEvents = true; - + Watchers.Add(watcher); } @@ -138,11 +167,12 @@ public void ReloadData() 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 @@ internal class BookmarkAttributes { internal string Url { get; set; } } + public void Dispose() + { + DisposeFileWatchers(); + } + + private static void DisposeFileWatchers() { foreach (var watcher in Watchers) { watcher.Dispose(); } + Watchers.Clear(); } } }