throttle PluginsManifest.UpdateManifestAsync

avoid repeatedly fetching manifest data while the user is typing a `pm` query
This commit is contained in:
Ioannis G 2023-07-05 16:54:50 +03:00
parent 64f0da456f
commit f03ac76494
No known key found for this signature in database
GPG key ID: EAC0E4E5E36AC49E
3 changed files with 16 additions and 28 deletions

View file

@ -1,4 +1,4 @@
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.Logger;
using System;
using System.Collections.Generic;
using System.Threading;
@ -16,6 +16,9 @@ namespace Flow.Launcher.Core.ExternalPlugins
private static readonly SemaphoreSlim manifestUpdateLock = new(1);
private static DateTime lastFetchedAt = DateTime.MinValue;
private static TimeSpan fetchTimeout = TimeSpan.FromSeconds(10);
public static List<UserPlugin> UserPlugins { get; private set; }
public static async Task UpdateManifestAsync(CancellationToken token = default)
@ -24,9 +27,13 @@ namespace Flow.Launcher.Core.ExternalPlugins
{
await manifestUpdateLock.WaitAsync(token).ConfigureAwait(false);
var results = await mainPluginStore.FetchAsync(token).ConfigureAwait(false);
if (UserPlugins == null || DateTime.Now.Subtract(lastFetchedAt) >= fetchTimeout)
{
var results = await mainPluginStore.FetchAsync(token).ConfigureAwait(false);
UserPlugins = results;
UserPlugins = results;
lastFetchedAt = DateTime.Now;
}
}
catch (Exception e)
{

View file

@ -1,4 +1,5 @@
using Flow.Launcher.Plugin.PluginsManager.ViewModels;
using Flow.Launcher.Core.ExternalPlugins;
using Flow.Launcher.Plugin.PluginsManager.ViewModels;
using Flow.Launcher.Plugin.PluginsManager.Views;
using System.Collections.Generic;
using System.Linq;
@ -34,7 +35,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
contextMenu = new ContextMenu(Context);
pluginManager = new PluginsManager(Context, Settings);
_ = pluginManager.UpdateManifestAsync();
await PluginsManifest.UpdateManifestAsync();
}
public List<Result> LoadContextMenus(Result selectedResult)

View file

@ -1,4 +1,4 @@
using Flow.Launcher.Core.ExternalPlugins;
using Flow.Launcher.Core.ExternalPlugins;
using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Http;
@ -49,26 +49,6 @@ namespace Flow.Launcher.Plugin.PluginsManager
Settings = settings;
}
private Task _downloadManifestTask = Task.CompletedTask;
internal Task UpdateManifestAsync(CancellationToken token = default, bool silent = false)
{
if (_downloadManifestTask.Status == TaskStatus.Running)
{
return _downloadManifestTask;
}
else
{
_downloadManifestTask = PluginsManifest.UpdateManifestAsync(token);
if (!silent)
_downloadManifestTask.ContinueWith(_ =>
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_update_failed_title"),
Context.API.GetTranslation("plugin_pluginsmanager_update_failed_subtitle"), icoPath, false),
TaskContinuationOptions.OnlyOnFaulted);
return _downloadManifestTask;
}
}
internal List<Result> GetDefaultHotKeys()
{
return new List<Result>()
@ -184,7 +164,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
internal async ValueTask<List<Result>> RequestUpdateAsync(string search, CancellationToken token)
{
await UpdateManifestAsync(token);
await PluginsManifest.UpdateManifestAsync(token);
var resultsForUpdate =
from existingPlugin in Context.API.GetAllPlugins()
@ -359,7 +339,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
internal async ValueTask<List<Result>> RequestInstallOrUpdate(string search, CancellationToken token)
{
await UpdateManifestAsync(token);
await PluginsManifest.UpdateManifestAsync(token);
if (Uri.IsWellFormedUriString(search, UriKind.Absolute)
&& search.Split('.').Last() == zip)