Skip to content

Commit 33fb0dd

Browse files
committed
refactor PluginsManifest to use conditional requests
1 parent de97cd4 commit 33fb0dd

File tree

3 files changed

+29
-51
lines changed

3 files changed

+29
-51
lines changed

Flow.Launcher.Core/ExternalPlugins/PluginsManifest.cs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using Flow.Launcher.Infrastructure.Http;
21
using Flow.Launcher.Infrastructure.Logger;
32
using System;
43
using System.Collections.Generic;
4+
using System.Net;
5+
using System.Net.Http;
56
using System.Text.Json;
67
using System.Threading;
78
using System.Threading.Tasks;
@@ -10,43 +11,45 @@ namespace Flow.Launcher.Core.ExternalPlugins
1011
{
1112
public static class PluginsManifest
1213
{
13-
static PluginsManifest()
14-
{
15-
UpdateTask = UpdateManifestAsync();
16-
}
17-
18-
public static List<UserPlugin> UserPlugins { get; private set; } = new List<UserPlugin>();
14+
private const string manifestFileUrl = "https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/plugin_api_v2/plugins.json";
1915

20-
public static Task UpdateTask { get; private set; }
16+
private static HttpClient httpClient = new HttpClient();
2117

2218
private static readonly SemaphoreSlim manifestUpdateLock = new(1);
2319

24-
public static Task UpdateManifestAsync()
25-
{
26-
if (manifestUpdateLock.CurrentCount == 0)
27-
{
28-
return UpdateTask;
29-
}
20+
private static string latestEtag = "";
3021

31-
return UpdateTask = DownloadManifestAsync();
32-
}
22+
public static List<UserPlugin> UserPlugins { get; private set; } = new List<UserPlugin>();
3323

34-
private static async Task DownloadManifestAsync()
24+
public static async Task UpdateManifestAsync()
3525
{
3626
try
3727
{
3828
await manifestUpdateLock.WaitAsync().ConfigureAwait(false);
3929

40-
await using var jsonStream = await Http.GetStreamAsync("https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/plugin_api_v2/plugins.json")
41-
.ConfigureAwait(false);
30+
var request = new HttpRequestMessage(HttpMethod.Get, manifestFileUrl);
31+
request.Headers.Add("If-None-Match", latestEtag);
32+
33+
var response = await httpClient.SendAsync(request).ConfigureAwait(false);
34+
35+
if (response.StatusCode == HttpStatusCode.OK)
36+
{
37+
Log.Info($"|PluginsManifest.{nameof(UpdateManifestAsync)}|Fetched plugins from manifest repo");
4238

43-
UserPlugins = await JsonSerializer.DeserializeAsync<List<UserPlugin>>(jsonStream).ConfigureAwait(false);
39+
var json = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
40+
41+
UserPlugins = await JsonSerializer.DeserializeAsync<List<UserPlugin>>(json).ConfigureAwait(false);
42+
43+
latestEtag = response.Headers.ETag.Tag;
44+
}
45+
else if (response.StatusCode != HttpStatusCode.NotModified)
46+
{
47+
Log.Warn($"|PluginsManifest.{nameof(UpdateManifestAsync)}|Http response for manifest file was {response.StatusCode}");
48+
}
4449
}
4550
catch (Exception e)
4651
{
47-
Log.Exception("|PluginManagement.GetManifest|Encountered error trying to download plugins manifest", e);
48-
49-
UserPlugins = new List<UserPlugin>();
52+
Log.Exception($"|PluginsManifest.{nameof(UpdateManifestAsync)}|Http request failed", e);
5053
}
5154
finally
5255
{

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public class Main : ISettingProvider, IAsyncPlugin, IContextMenu, IPluginI18n, I
2424

2525
internal PluginsManager pluginManager;
2626

27-
private DateTime lastUpdateTime = DateTime.MinValue;
28-
2927
public Control CreateSettingPanel()
3028
{
3129
return new PluginsManagerSettings(viewModel);
@@ -38,12 +36,6 @@ public Task InitAsync(PluginInitContext context)
3836
viewModel = new SettingsViewModel(context, Settings);
3937
contextMenu = new ContextMenu(Context);
4038
pluginManager = new PluginsManager(Context, Settings);
41-
_manifestUpdateTask = pluginManager
42-
.UpdateManifestAsync(true)
43-
.ContinueWith(_ =>
44-
{
45-
lastUpdateTime = DateTime.Now;
46-
}, TaskContinuationOptions.OnlyOnRanToCompletion);
4739

4840
return Task.CompletedTask;
4941
}
@@ -53,23 +45,13 @@ public List<Result> LoadContextMenus(Result selectedResult)
5345
return contextMenu.LoadContextMenus(selectedResult);
5446
}
5547

56-
private Task _manifestUpdateTask = Task.CompletedTask;
57-
5848
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
5949
{
6050
var search = query.Search;
6151

6252
if (string.IsNullOrWhiteSpace(search))
6353
return pluginManager.GetDefaultHotKeys();
6454

65-
if ((DateTime.Now - lastUpdateTime).TotalHours > 12 && _manifestUpdateTask.IsCompleted) // 12 hours
66-
{
67-
_manifestUpdateTask = pluginManager.UpdateManifestAsync().ContinueWith(t =>
68-
{
69-
lastUpdateTime = DateTime.Now;
70-
}, TaskContinuationOptions.OnlyOnRanToCompletion);
71-
}
72-
7355
return search switch
7456
{
7557
//search could be url, no need ToLower() when passed in
@@ -100,7 +82,6 @@ public string GetTranslatedPluginDescription()
10082
public async Task ReloadDataAsync()
10183
{
10284
await pluginManager.UpdateManifestAsync();
103-
lastUpdateTime = DateTime.Now;
10485
}
10586
}
10687
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ internal Task UpdateManifestAsync(bool silent = false)
5959
}
6060
else
6161
{
62-
_downloadManifestTask = PluginsManifest.UpdateTask;
62+
_downloadManifestTask = PluginsManifest.UpdateManifestAsync();
6363
if (!silent)
6464
_downloadManifestTask.ContinueWith(_ =>
6565
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_update_failed_title"),
@@ -181,10 +181,7 @@ internal async Task InstallOrUpdate(UserPlugin plugin)
181181

182182
internal async ValueTask<List<Result>> RequestUpdate(string search, CancellationToken token)
183183
{
184-
if (!PluginsManifest.UserPlugins.Any())
185-
{
186-
await UpdateManifestAsync();
187-
}
184+
await UpdateManifestAsync();
188185

189186
token.ThrowIfCancellationRequested();
190187

@@ -371,10 +368,7 @@ private bool InstallSourceKnown(string url)
371368

372369
internal async ValueTask<List<Result>> RequestInstallOrUpdate(string searchName, CancellationToken token)
373370
{
374-
if (!PluginsManifest.UserPlugins.Any())
375-
{
376-
await UpdateManifestAsync();
377-
}
371+
await UpdateManifestAsync();
378372

379373
token.ThrowIfCancellationRequested();
380374

0 commit comments

Comments
 (0)