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] Refactor file watcher logic * Remove LastAccessed filter to avoid unwanted recursive RegisterBookmarkFile() calls * Check if watcher exists --- .../Main.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index d9a719272..c5a55bb5f 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -106,17 +106,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; @@ -131,7 +133,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark }; watcher.EnableRaisingEvents = true; - + Watchers.Add(watcher); }