12
12
using System . IO ;
13
13
using System . Threading . Channels ;
14
14
using System . Threading . Tasks ;
15
+ using System . Threading ;
15
16
16
17
namespace Flow . Launcher . Plugin . BrowserBookmark
17
18
{
@@ -22,23 +23,27 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex
22
23
private static List < Bookmark > cachedBookmarks = new List < Bookmark > ( ) ;
23
24
24
25
private static Settings _settings ;
25
-
26
+
26
27
public void Init ( PluginInitContext context )
27
28
{
28
29
Main . context = context ;
29
-
30
+
30
31
_settings = context . API . LoadSettingJsonStorage < Settings > ( ) ;
31
32
33
+ LoadBookmarksIfEnabled ( ) ;
34
+ }
35
+
36
+ private static void LoadBookmarksIfEnabled ( )
37
+ {
32
38
if ( context . CurrentPluginMetadata . Disabled )
33
39
{
34
- // Don't load and monitor files if disabled
35
- // Note: It doesn't start loading or monitoring if enabled later
40
+ // Don't load or monitor files if disabled
41
+ // Note: It doesn't start loading or monitoring if enabled later, you need to manually reload data
36
42
return ;
37
43
}
38
44
39
45
cachedBookmarks = BookmarkLoader . LoadAllBookmarks ( _settings ) ;
40
-
41
- _ = MonitorRefreshQueue ( ) ;
46
+ _ = MonitorRefreshQueueAsync ( ) ;
42
47
}
43
48
44
49
public List < Result > Query ( Query query )
@@ -95,17 +100,25 @@ public List<Result> Query(Query query)
95
100
96
101
private static Channel < byte > refreshQueue = Channel . CreateBounded < byte > ( 1 ) ;
97
102
98
- private async Task MonitorRefreshQueue ( )
103
+ private static SemaphoreSlim fileMonitorSemaphore = new ( 1 , 1 ) ;
104
+
105
+ private static async Task MonitorRefreshQueueAsync ( )
99
106
{
107
+ if ( fileMonitorSemaphore . CurrentCount < 1 )
108
+ {
109
+ return ;
110
+ }
111
+ await fileMonitorSemaphore . WaitAsync ( ) ;
100
112
var reader = refreshQueue . Reader ;
101
113
while ( await reader . WaitToReadAsync ( ) )
102
114
{
103
- await Task . Delay ( 2000 ) ;
115
+ await Task . Delay ( 5000 ) ;
104
116
if ( reader . TryRead ( out _ ) )
105
117
{
106
- ReloadData ( ) ;
118
+ ReloadAllBookmarks ( ) ;
107
119
}
108
120
}
121
+ fileMonitorSemaphore . Release ( ) ;
109
122
}
110
123
111
124
private static readonly List < FileSystemWatcher > Watchers = new ( ) ;
@@ -117,6 +130,10 @@ internal static void RegisterBookmarkFile(string path)
117
130
{
118
131
return ;
119
132
}
133
+ if ( context . CurrentPluginMetadata . Disabled )
134
+ {
135
+ return ;
136
+ }
120
137
if ( Watchers . Any ( x => x . Path . Equals ( directory , StringComparison . OrdinalIgnoreCase ) ) )
121
138
{
122
139
return ;
@@ -152,8 +169,8 @@ public void ReloadData()
152
169
public static void ReloadAllBookmarks ( )
153
170
{
154
171
cachedBookmarks . Clear ( ) ;
155
-
156
- cachedBookmarks = BookmarkLoader . LoadAllBookmarks ( _settings ) ;
172
+ DisposeFileWatchers ( ) ;
173
+ LoadBookmarksIfEnabled ( ) ;
157
174
}
158
175
159
176
public string GetTranslatedPluginTitle ( )
@@ -207,12 +224,19 @@ internal class BookmarkAttributes
207
224
{
208
225
internal string Url { get ; set ; }
209
226
}
227
+
210
228
public void Dispose ( )
229
+ {
230
+ DisposeFileWatchers ( ) ;
231
+ }
232
+
233
+ private static void DisposeFileWatchers ( )
211
234
{
212
235
foreach ( var watcher in Watchers )
213
236
{
214
237
watcher . Dispose ( ) ;
215
238
}
239
+ Watchers . Clear ( ) ;
216
240
}
217
241
}
218
242
}
0 commit comments