10
10
using System . IO ;
11
11
using System . Threading . Channels ;
12
12
using System . Threading . Tasks ;
13
+ using System . Threading ;
13
14
14
15
namespace Flow . Launcher . Plugin . BrowserBookmark
15
16
{
@@ -20,20 +21,39 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex
20
21
private static List < Bookmark > cachedBookmarks = new List < Bookmark > ( ) ;
21
22
22
23
private static Settings _settings ;
23
-
24
+
25
+ private static bool initialized = false ;
26
+
24
27
public void Init ( PluginInitContext context )
25
28
{
26
29
Main . context = context ;
27
-
30
+
28
31
_settings = context . API . LoadSettingJsonStorage < Settings > ( ) ;
29
32
30
- cachedBookmarks = BookmarkLoader . LoadAllBookmarks ( _settings ) ;
33
+ LoadBookmarksIfEnabled ( ) ;
34
+ }
31
35
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 ;
33
47
}
34
48
35
49
public List < Result > Query ( Query query )
36
50
{
51
+ // For when the plugin being previously disabled and is now renabled
52
+ if ( ! initialized )
53
+ {
54
+ LoadBookmarksIfEnabled ( ) ;
55
+ }
56
+
37
57
string param = query . Search . TrimStart ( ) ;
38
58
39
59
// Should top results be returned? (true if no search parameters have been passed)
@@ -86,35 +106,44 @@ public List<Result> Query(Query query)
86
106
87
107
private static Channel < byte > refreshQueue = Channel . CreateBounded < byte > ( 1 ) ;
88
108
89
- private async Task MonitorRefreshQueue ( )
109
+ private static SemaphoreSlim fileMonitorSemaphore = new ( 1 , 1 ) ;
110
+
111
+ private static async Task MonitorRefreshQueueAsync ( )
90
112
{
113
+ if ( fileMonitorSemaphore . CurrentCount < 1 )
114
+ {
115
+ return ;
116
+ }
117
+ await fileMonitorSemaphore . WaitAsync ( ) ;
91
118
var reader = refreshQueue . Reader ;
92
119
while ( await reader . WaitToReadAsync ( ) )
93
120
{
94
- await Task . Delay ( 2000 ) ;
95
121
if ( reader . TryRead ( out _ ) )
96
122
{
97
- ReloadData ( ) ;
123
+ ReloadAllBookmarks ( false ) ;
98
124
}
99
125
}
126
+ fileMonitorSemaphore . Release ( ) ;
100
127
}
101
128
102
129
private static readonly List < FileSystemWatcher > Watchers = new ( ) ;
103
130
104
131
internal static void RegisterBookmarkFile ( string path )
105
132
{
106
133
var directory = Path . GetDirectoryName ( path ) ;
107
- if ( ! Directory . Exists ( directory ) )
134
+ if ( ! Directory . Exists ( directory ) || ! File . Exists ( path ) )
135
+ {
108
136
return ;
109
- var watcher = new FileSystemWatcher ( directory ! ) ;
110
- if ( File . Exists ( path ) )
137
+ }
138
+ if ( Watchers . Any ( x => x . Path . Equals ( directory , StringComparison . OrdinalIgnoreCase ) ) )
111
139
{
112
- var fileName = Path . GetFileName ( path ) ;
113
- watcher . Filter = fileName ;
140
+ return ;
114
141
}
142
+
143
+ var watcher = new FileSystemWatcher ( directory ! ) ;
144
+ watcher . Filter = Path . GetFileName ( path ) ;
115
145
116
146
watcher . NotifyFilter = NotifyFilters . FileName |
117
- NotifyFilters . LastAccess |
118
147
NotifyFilters . LastWrite |
119
148
NotifyFilters . Size ;
120
149
@@ -129,7 +158,7 @@ internal static void RegisterBookmarkFile(string path)
129
158
} ;
130
159
131
160
watcher . EnableRaisingEvents = true ;
132
-
161
+
133
162
Watchers . Add ( watcher ) ;
134
163
}
135
164
@@ -138,11 +167,12 @@ public void ReloadData()
138
167
ReloadAllBookmarks ( ) ;
139
168
}
140
169
141
- public static void ReloadAllBookmarks ( )
170
+ public static void ReloadAllBookmarks ( bool disposeFileWatchers = true )
142
171
{
143
172
cachedBookmarks . Clear ( ) ;
144
-
145
- cachedBookmarks = BookmarkLoader . LoadAllBookmarks ( _settings ) ;
173
+ if ( disposeFileWatchers )
174
+ DisposeFileWatchers ( ) ;
175
+ LoadBookmarksIfEnabled ( ) ;
146
176
}
147
177
148
178
public string GetTranslatedPluginTitle ( )
@@ -196,12 +226,19 @@ internal class BookmarkAttributes
196
226
{
197
227
internal string Url { get ; set ; }
198
228
}
229
+
199
230
public void Dispose ( )
231
+ {
232
+ DisposeFileWatchers ( ) ;
233
+ }
234
+
235
+ private static void DisposeFileWatchers ( )
200
236
{
201
237
foreach ( var watcher in Watchers )
202
238
{
203
239
watcher . Dispose ( ) ;
204
240
}
241
+ Watchers . Clear ( ) ;
205
242
}
206
243
}
207
244
}
0 commit comments