Skip to content

Commit 4591fa7

Browse files
Create file watchers when reloading bookmarks
1 parent 0dc01a1 commit 4591fa7

File tree

1 file changed

+35
-11
lines changed
  • Plugins/Flow.Launcher.Plugin.BrowserBookmark

1 file changed

+35
-11
lines changed

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.IO;
1313
using System.Threading.Channels;
1414
using System.Threading.Tasks;
15+
using System.Threading;
1516

1617
namespace Flow.Launcher.Plugin.BrowserBookmark
1718
{
@@ -22,23 +23,27 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex
2223
private static List<Bookmark> cachedBookmarks = new List<Bookmark>();
2324

2425
private static Settings _settings;
25-
26+
2627
public void Init(PluginInitContext context)
2728
{
2829
Main.context = context;
29-
30+
3031
_settings = context.API.LoadSettingJsonStorage<Settings>();
3132

33+
LoadBookmarksIfEnabled();
34+
}
35+
36+
private static void LoadBookmarksIfEnabled()
37+
{
3238
if (context.CurrentPluginMetadata.Disabled)
3339
{
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
3642
return;
3743
}
3844

3945
cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings);
40-
41-
_ = MonitorRefreshQueue();
46+
_ = MonitorRefreshQueueAsync();
4247
}
4348

4449
public List<Result> Query(Query query)
@@ -95,17 +100,25 @@ public List<Result> Query(Query query)
95100

96101
private static Channel<byte> refreshQueue = Channel.CreateBounded<byte>(1);
97102

98-
private async Task MonitorRefreshQueue()
103+
private static SemaphoreSlim fileMonitorSemaphore = new(1, 1);
104+
105+
private static async Task MonitorRefreshQueueAsync()
99106
{
107+
if (fileMonitorSemaphore.CurrentCount < 1)
108+
{
109+
return;
110+
}
111+
await fileMonitorSemaphore.WaitAsync();
100112
var reader = refreshQueue.Reader;
101113
while (await reader.WaitToReadAsync())
102114
{
103-
await Task.Delay(2000);
115+
await Task.Delay(5000);
104116
if (reader.TryRead(out _))
105117
{
106-
ReloadData();
118+
ReloadAllBookmarks();
107119
}
108120
}
121+
fileMonitorSemaphore.Release();
109122
}
110123

111124
private static readonly List<FileSystemWatcher> Watchers = new();
@@ -117,6 +130,10 @@ internal static void RegisterBookmarkFile(string path)
117130
{
118131
return;
119132
}
133+
if (context.CurrentPluginMetadata.Disabled)
134+
{
135+
return;
136+
}
120137
if (Watchers.Any(x => x.Path.Equals(directory, StringComparison.OrdinalIgnoreCase)))
121138
{
122139
return;
@@ -152,8 +169,8 @@ public void ReloadData()
152169
public static void ReloadAllBookmarks()
153170
{
154171
cachedBookmarks.Clear();
155-
156-
cachedBookmarks = BookmarkLoader.LoadAllBookmarks(_settings);
172+
DisposeFileWatchers();
173+
LoadBookmarksIfEnabled();
157174
}
158175

159176
public string GetTranslatedPluginTitle()
@@ -207,12 +224,19 @@ internal class BookmarkAttributes
207224
{
208225
internal string Url { get; set; }
209226
}
227+
210228
public void Dispose()
229+
{
230+
DisposeFileWatchers();
231+
}
232+
233+
private static void DisposeFileWatchers()
211234
{
212235
foreach (var watcher in Watchers)
213236
{
214237
watcher.Dispose();
215238
}
239+
Watchers.Clear();
216240
}
217241
}
218242
}

0 commit comments

Comments
 (0)