Skip to content

Commit f03ac76

Browse files
committed
throttle PluginsManifest.UpdateManifestAsync
avoid repeatedly fetching manifest data while the user is typing a `pm` query
1 parent 64f0da4 commit f03ac76

File tree

3 files changed

+16
-28
lines changed

3 files changed

+16
-28
lines changed

Flow.Launcher.Core/ExternalPlugins/PluginsManifest.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Flow.Launcher.Infrastructure.Logger;
1+
using Flow.Launcher.Infrastructure.Logger;
22
using System;
33
using System.Collections.Generic;
44
using System.Threading;
@@ -16,6 +16,9 @@ public static class PluginsManifest
1616

1717
private static readonly SemaphoreSlim manifestUpdateLock = new(1);
1818

19+
private static DateTime lastFetchedAt = DateTime.MinValue;
20+
private static TimeSpan fetchTimeout = TimeSpan.FromSeconds(10);
21+
1922
public static List<UserPlugin> UserPlugins { get; private set; }
2023

2124
public static async Task UpdateManifestAsync(CancellationToken token = default)
@@ -24,9 +27,13 @@ public static async Task UpdateManifestAsync(CancellationToken token = default)
2427
{
2528
await manifestUpdateLock.WaitAsync(token).ConfigureAwait(false);
2629

27-
var results = await mainPluginStore.FetchAsync(token).ConfigureAwait(false);
30+
if (UserPlugins == null || DateTime.Now.Subtract(lastFetchedAt) >= fetchTimeout)
31+
{
32+
var results = await mainPluginStore.FetchAsync(token).ConfigureAwait(false);
2833

29-
UserPlugins = results;
34+
UserPlugins = results;
35+
lastFetchedAt = DateTime.Now;
36+
}
3037
}
3138
catch (Exception e)
3239
{

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Flow.Launcher.Plugin.PluginsManager.ViewModels;
1+
using Flow.Launcher.Core.ExternalPlugins;
2+
using Flow.Launcher.Plugin.PluginsManager.ViewModels;
23
using Flow.Launcher.Plugin.PluginsManager.Views;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -34,7 +35,7 @@ public async Task InitAsync(PluginInitContext context)
3435
contextMenu = new ContextMenu(Context);
3536
pluginManager = new PluginsManager(Context, Settings);
3637

37-
_ = pluginManager.UpdateManifestAsync();
38+
await PluginsManifest.UpdateManifestAsync();
3839
}
3940

4041
public List<Result> LoadContextMenus(Result selectedResult)

Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Flow.Launcher.Core.ExternalPlugins;
1+
using Flow.Launcher.Core.ExternalPlugins;
22
using Flow.Launcher.Core.Plugin;
33
using Flow.Launcher.Infrastructure;
44
using Flow.Launcher.Infrastructure.Http;
@@ -49,26 +49,6 @@ internal PluginsManager(PluginInitContext context, Settings settings)
4949
Settings = settings;
5050
}
5151

52-
private Task _downloadManifestTask = Task.CompletedTask;
53-
54-
internal Task UpdateManifestAsync(CancellationToken token = default, bool silent = false)
55-
{
56-
if (_downloadManifestTask.Status == TaskStatus.Running)
57-
{
58-
return _downloadManifestTask;
59-
}
60-
else
61-
{
62-
_downloadManifestTask = PluginsManifest.UpdateManifestAsync(token);
63-
if (!silent)
64-
_downloadManifestTask.ContinueWith(_ =>
65-
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_update_failed_title"),
66-
Context.API.GetTranslation("plugin_pluginsmanager_update_failed_subtitle"), icoPath, false),
67-
TaskContinuationOptions.OnlyOnFaulted);
68-
return _downloadManifestTask;
69-
}
70-
}
71-
7252
internal List<Result> GetDefaultHotKeys()
7353
{
7454
return new List<Result>()
@@ -184,7 +164,7 @@ internal async Task InstallOrUpdateAsync(UserPlugin plugin)
184164

185165
internal async ValueTask<List<Result>> RequestUpdateAsync(string search, CancellationToken token)
186166
{
187-
await UpdateManifestAsync(token);
167+
await PluginsManifest.UpdateManifestAsync(token);
188168

189169
var resultsForUpdate =
190170
from existingPlugin in Context.API.GetAllPlugins()
@@ -359,7 +339,7 @@ private bool InstallSourceKnown(string url)
359339

360340
internal async ValueTask<List<Result>> RequestInstallOrUpdate(string search, CancellationToken token)
361341
{
362-
await UpdateManifestAsync(token);
342+
await PluginsManifest.UpdateManifestAsync(token);
363343

364344
if (Uri.IsWellFormedUriString(search, UriKind.Absolute)
365345
&& search.Split('.').Last() == zip)

0 commit comments

Comments
 (0)