1010using System . IO ;
1111using System . Threading . Channels ;
1212using System . Threading . Tasks ;
13+ using System . Threading ;
1314
1415namespace Flow . Launcher . Plugin . BrowserBookmark
1516{
@@ -20,20 +21,39 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex
2021 private static List < Bookmark > cachedBookmarks = new List < Bookmark > ( ) ;
2122
2223 private static Settings _settings ;
23-
24+
25+ private static bool initialized = false ;
26+
2427 public void Init ( PluginInitContext context )
2528 {
2629 Main . context = context ;
27-
30+
2831 _settings = context . API . LoadSettingJsonStorage < Settings > ( ) ;
2932
30- cachedBookmarks = BookmarkLoader . LoadAllBookmarks ( _settings ) ;
33+ LoadBookmarksIfEnabled ( ) ;
34+ }
3135
32- _ = MonitorRefreshQueue ( ) ;
36+ private static void LoadBookmarksIfEnabled ( )
37+ {
38+ if ( context . CurrentPluginMetadata . Disabled )
39+ {
40+ // Don't load or monitor files if disabled
41+ return ;
42+ }
43+
44+ cachedBookmarks = BookmarkLoader . LoadAllBookmarks ( _settings ) ;
45+ _ = MonitorRefreshQueueAsync ( ) ;
46+ initialized = true ;
3347 }
3448
3549 public List < Result > Query ( Query query )
3650 {
51+ // For when the plugin being previously disabled and is now renabled
52+ if ( ! initialized )
53+ {
54+ LoadBookmarksIfEnabled ( ) ;
55+ }
56+
3757 string param = query . Search . TrimStart ( ) ;
3858
3959 // Should top results be returned? (true if no search parameters have been passed)
@@ -86,35 +106,44 @@ public List<Result> Query(Query query)
86106
87107 private static Channel < byte > refreshQueue = Channel . CreateBounded < byte > ( 1 ) ;
88108
89- private async Task MonitorRefreshQueue ( )
109+ private static SemaphoreSlim fileMonitorSemaphore = new ( 1 , 1 ) ;
110+
111+ private static async Task MonitorRefreshQueueAsync ( )
90112 {
113+ if ( fileMonitorSemaphore . CurrentCount < 1 )
114+ {
115+ return ;
116+ }
117+ await fileMonitorSemaphore . WaitAsync ( ) ;
91118 var reader = refreshQueue . Reader ;
92119 while ( await reader . WaitToReadAsync ( ) )
93120 {
94- await Task . Delay ( 2000 ) ;
95121 if ( reader . TryRead ( out _ ) )
96122 {
97- ReloadData ( ) ;
123+ ReloadAllBookmarks ( false ) ;
98124 }
99125 }
126+ fileMonitorSemaphore . Release ( ) ;
100127 }
101128
102129 private static readonly List < FileSystemWatcher > Watchers = new ( ) ;
103130
104131 internal static void RegisterBookmarkFile ( string path )
105132 {
106133 var directory = Path . GetDirectoryName ( path ) ;
107- if ( ! Directory . Exists ( directory ) )
134+ if ( ! Directory . Exists ( directory ) || ! File . Exists ( path ) )
135+ {
108136 return ;
109- var watcher = new FileSystemWatcher ( directory ! ) ;
110- if ( File . Exists ( path ) )
137+ }
138+ if ( Watchers . Any ( x => x . Path . Equals ( directory , StringComparison . OrdinalIgnoreCase ) ) )
111139 {
112- var fileName = Path . GetFileName ( path ) ;
113- watcher . Filter = fileName ;
140+ return ;
114141 }
142+
143+ var watcher = new FileSystemWatcher ( directory ! ) ;
144+ watcher . Filter = Path . GetFileName ( path ) ;
115145
116146 watcher . NotifyFilter = NotifyFilters . FileName |
117- NotifyFilters . LastAccess |
118147 NotifyFilters . LastWrite |
119148 NotifyFilters . Size ;
120149
@@ -129,7 +158,7 @@ internal static void RegisterBookmarkFile(string path)
129158 } ;
130159
131160 watcher . EnableRaisingEvents = true ;
132-
161+
133162 Watchers . Add ( watcher ) ;
134163 }
135164
@@ -138,11 +167,12 @@ public void ReloadData()
138167 ReloadAllBookmarks ( ) ;
139168 }
140169
141- public static void ReloadAllBookmarks ( )
170+ public static void ReloadAllBookmarks ( bool disposeFileWatchers = true )
142171 {
143172 cachedBookmarks . Clear ( ) ;
144-
145- cachedBookmarks = BookmarkLoader . LoadAllBookmarks ( _settings ) ;
173+ if ( disposeFileWatchers )
174+ DisposeFileWatchers ( ) ;
175+ LoadBookmarksIfEnabled ( ) ;
146176 }
147177
148178 public string GetTranslatedPluginTitle ( )
@@ -196,12 +226,19 @@ internal class BookmarkAttributes
196226 {
197227 internal string Url { get ; set ; }
198228 }
229+
199230 public void Dispose ( )
231+ {
232+ DisposeFileWatchers ( ) ;
233+ }
234+
235+ private static void DisposeFileWatchers ( )
200236 {
201237 foreach ( var watcher in Watchers )
202238 {
203239 watcher . Dispose ( ) ;
204240 }
241+ Watchers . Clear ( ) ;
205242 }
206243 }
207244}
0 commit comments