diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index 306276258..f31d20625 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -12,6 +12,7 @@ using Flow.Launcher.Plugin.SharedCommands; using System.IO; using System.Threading.Channels; using System.Threading.Tasks; +using System.Threading; namespace Flow.Launcher.Plugin.BrowserBookmark { @@ -22,23 +23,27 @@ namespace Flow.Launcher.Plugin.BrowserBookmark private static List cachedBookmarks = new List(); private static Settings _settings; - + public void Init(PluginInitContext context) { Main.context = context; - + _settings = context.API.LoadSettingJsonStorage(); + LoadBookmarksIfEnabled(); + } + + private static void LoadBookmarksIfEnabled() + { if (context.CurrentPluginMetadata.Disabled) { - // Don't load and monitor files if disabled - // Note: It doesn't start loading or monitoring if enabled later + // Don't load or monitor files if disabled + // Note: It doesn't start loading or monitoring if enabled later, you need to manually reload data return; } cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings); - - _ = MonitorRefreshQueue(); + _ = MonitorRefreshQueueAsync(); } public List Query(Query query) @@ -95,17 +100,25 @@ 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); + await Task.Delay(5000); if (reader.TryRead(out _)) { - ReloadData(); + ReloadAllBookmarks(); } } + fileMonitorSemaphore.Release(); } private static readonly List Watchers = new(); @@ -117,6 +130,10 @@ namespace Flow.Launcher.Plugin.BrowserBookmark { return; } + if (context.CurrentPluginMetadata.Disabled) + { + return; + } if (Watchers.Any(x => x.Path.Equals(directory, StringComparison.OrdinalIgnoreCase))) { return; @@ -152,8 +169,8 @@ namespace Flow.Launcher.Plugin.BrowserBookmark public static void ReloadAllBookmarks() { cachedBookmarks.Clear(); - - cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings); + DisposeFileWatchers(); + LoadBookmarksIfEnabled(); } public string GetTranslatedPluginTitle() @@ -207,12 +224,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(); } } }