From d0289b49b669140b4e365817b0a452fe2f7e98ad Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Mon, 22 May 2023 23:13:29 +0800 Subject: [PATCH 1/9] Refactor file watcher logic * Remove LastAccessed filter to avoid unwanted recursive RegisterBookmarkFile() calls * Check if watcher exists --- .../Flow.Launcher.Plugin.BrowserBookmark/Main.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index d9a719272b5..c5a55bb5f41 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -106,17 +106,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; @@ -131,7 +133,7 @@ internal static void RegisterBookmarkFile(string path) }; watcher.EnableRaisingEvents = true; - + Watchers.Add(watcher); } From cbf73fcc0ff795da342f072d789481ab45970e3f Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Mon, 22 May 2023 23:28:47 +0800 Subject: [PATCH 2/9] Don't load bookmark files if plugin disabled --- Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index c5a55bb5f41..306276258d4 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -29,6 +29,13 @@ public void Init(PluginInitContext context) _settings = context.API.LoadSettingJsonStorage(); + if (context.CurrentPluginMetadata.Disabled) + { + // Don't load and monitor files if disabled + // Note: It doesn't start loading or monitoring if enabled later + return; + } + cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings); _ = MonitorRefreshQueue(); From 0dc01a14694ba07ad7e09453dd08629c5c490c04 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Mon, 22 May 2023 23:38:23 +0800 Subject: [PATCH 3/9] update --- .github/actions/spelling/expect.txt | 1 + 1 file changed, 1 insertion(+) 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 From 4591fa76d97328a61c30ad371b8e1f21197376a7 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Tue, 23 May 2023 19:37:38 +0800 Subject: [PATCH 4/9] Create file watchers when reloading bookmarks --- .../Main.cs | 46 ++++++++++++++----- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index 306276258d4..f31d206253b 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -12,6 +12,7 @@ using System.IO; using System.Threading.Channels; using System.Threading.Tasks; +using System.Threading; namespace Flow.Launcher.Plugin.BrowserBookmark { @@ -22,23 +23,27 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex 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 @@ 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); + await Task.Delay(5000); if (reader.TryRead(out _)) { - ReloadData(); + ReloadAllBookmarks(); } } + fileMonitorSemaphore.Release(); } private static readonly List Watchers = new(); @@ -117,6 +130,10 @@ internal static void RegisterBookmarkFile(string path) { return; } + if (context.CurrentPluginMetadata.Disabled) + { + return; + } if (Watchers.Any(x => x.Path.Equals(directory, StringComparison.OrdinalIgnoreCase))) { return; @@ -152,8 +169,8 @@ public void ReloadData() public static void ReloadAllBookmarks() { cachedBookmarks.Clear(); - - cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings); + DisposeFileWatchers(); + LoadBookmarksIfEnabled(); } public string GetTranslatedPluginTitle() @@ -207,12 +224,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(); } } } From 6d64abd21a3b536d889c61d85d2c5384388834ed Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Thu, 15 Jun 2023 21:30:30 +0800 Subject: [PATCH 5/9] Load bookmarks in Query() if not initialized --- Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index f31d206253b..7d214f5c617 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -24,6 +24,8 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex private static Settings _settings; + private static bool initialized = false; + public void Init(PluginInitContext context) { Main.context = context; @@ -31,6 +33,8 @@ public void Init(PluginInitContext context) _settings = context.API.LoadSettingJsonStorage(); LoadBookmarksIfEnabled(); + + initialized = true; } private static void LoadBookmarksIfEnabled() @@ -48,6 +52,12 @@ private static void LoadBookmarksIfEnabled() public List Query(Query query) { + if (!initialized) + { + LoadBookmarksIfEnabled(); + initialized = true; + } + string param = query.Search.TrimStart(); // Should top results be returned? (true if no search parameters have been passed) From e46df28fdd4e4d78945aa306ae10e6f9d6b19915 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sat, 17 Jun 2023 00:42:21 +0800 Subject: [PATCH 6/9] Move initialized flag --- Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index 7d214f5c617..b1d3adf8ecd 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -33,8 +33,6 @@ public void Init(PluginInitContext context) _settings = context.API.LoadSettingJsonStorage(); LoadBookmarksIfEnabled(); - - initialized = true; } private static void LoadBookmarksIfEnabled() @@ -42,12 +40,12 @@ private static void LoadBookmarksIfEnabled() if (context.CurrentPluginMetadata.Disabled) { // 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); _ = MonitorRefreshQueueAsync(); + initialized = true; } public List Query(Query query) @@ -55,7 +53,6 @@ public List Query(Query query) if (!initialized) { LoadBookmarksIfEnabled(); - initialized = true; } string param = query.Search.TrimStart(); @@ -140,10 +137,6 @@ internal static void RegisterBookmarkFile(string path) { return; } - if (context.CurrentPluginMetadata.Disabled) - { - return; - } if (Watchers.Any(x => x.Path.Equals(directory, StringComparison.OrdinalIgnoreCase))) { return; From 9b5341c2b71b2c87778cee3feac6406af2b860a7 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sun, 18 Jun 2023 10:55:55 +0800 Subject: [PATCH 7/9] Preserve file watchers when monitored files change --- Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index b1d3adf8ecd..0c07a84769f 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -122,7 +122,7 @@ private static async Task MonitorRefreshQueueAsync() await Task.Delay(5000); if (reader.TryRead(out _)) { - ReloadAllBookmarks(); + ReloadAllBookmarks(false); } } fileMonitorSemaphore.Release(); @@ -169,10 +169,11 @@ public void ReloadData() ReloadAllBookmarks(); } - public static void ReloadAllBookmarks() + public static void ReloadAllBookmarks(bool disposeFileWatchers = true) { cachedBookmarks.Clear(); - DisposeFileWatchers(); + if (disposeFileWatchers) + DisposeFileWatchers(); LoadBookmarksIfEnabled(); } From e0e86d039dc51f4a492dfc534cc597be2f9792f2 Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sat, 24 Jun 2023 10:54:53 +0800 Subject: [PATCH 8/9] Remove delay --- Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index 0c07a84769f..2a94714fa42 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -119,7 +119,6 @@ private static async Task MonitorRefreshQueueAsync() var reader = refreshQueue.Reader; while (await reader.WaitToReadAsync()) { - await Task.Delay(5000); if (reader.TryRead(out _)) { ReloadAllBookmarks(false); From 9656e5cf4bbb87c939526c0382726fca95206f87 Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Thu, 29 Jun 2023 08:48:25 +0800 Subject: [PATCH 9/9] Clarify initialized condition Co-authored-by: Jeremy Wu --- Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index 53dc6b5283c..a13d6c929fe 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -48,6 +48,7 @@ private static void LoadBookmarksIfEnabled() public List Query(Query query) { + // For when the plugin being previously disabled and is now renabled if (!initialized) { LoadBookmarksIfEnabled();