Merge pull request #314 from taooceros/FixUnexpectedErrorNotice

Optimize manifest downloading behavior
This commit is contained in:
Jeremy Wu 2021-02-15 21:20:47 +11:00 committed by GitHub
commit 55b0a91cdc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 30 deletions

View file

@ -37,20 +37,10 @@ namespace Flow.Launcher.Plugin.PluginsManager
Settings = viewModel.Settings; Settings = viewModel.Settings;
contextMenu = new ContextMenu(Context); contextMenu = new ContextMenu(Context);
pluginManager = new PluginsManager(Context, Settings); pluginManager = new PluginsManager(Context, Settings);
var updateManifestTask = pluginManager.UpdateManifest(); _ = pluginManager.UpdateManifest().ContinueWith(_ =>
_ = updateManifestTask.ContinueWith(t => {
{ lastUpdateTime = DateTime.Now;
if (t.IsCompletedSuccessfully) }, TaskContinuationOptions.OnlyOnRanToCompletion);
{
lastUpdateTime = DateTime.Now;
}
else
{
context.API.ShowMsg("Plugin Manifest Download Fail.",
"Please check if you can connect to github.com. " +
"This error means you may not be able to Install and Update Plugin.", pluginManager.icoPath, false);
}
});
return Task.CompletedTask; return Task.CompletedTask;
} }
@ -69,15 +59,17 @@ namespace Flow.Launcher.Plugin.PluginsManager
if ((DateTime.Now - lastUpdateTime).TotalHours > 12) // 12 hours if ((DateTime.Now - lastUpdateTime).TotalHours > 12) // 12 hours
{ {
await pluginManager.UpdateManifest(); _ = pluginManager.UpdateManifest().ContinueWith(t =>
lastUpdateTime = DateTime.Now; {
lastUpdateTime = DateTime.Now;
}, TaskContinuationOptions.OnlyOnRanToCompletion);
} }
return search switch return search switch
{ {
var s when s.StartsWith(Settings.HotKeyInstall) => await pluginManager.RequestInstallOrUpdate(s, token), var s when s.StartsWith(Settings.HotKeyInstall) => await pluginManager.RequestInstallOrUpdate(s, token),
var s when s.StartsWith(Settings.HotkeyUninstall) => pluginManager.RequestUninstall(s), var s when s.StartsWith(Settings.HotkeyUninstall) => pluginManager.RequestUninstall(s),
var s when s.StartsWith(Settings.HotkeyUpdate) => pluginManager.RequestUpdate(s), var s when s.StartsWith(Settings.HotkeyUpdate) => await pluginManager.RequestUpdate(s, token),
_ => pluginManager.GetDefaultHotKeys().Where(hotkey => _ => pluginManager.GetDefaultHotKeys().Where(hotkey =>
{ {
hotkey.Score = StringMatcher.FuzzySearch(search, hotkey.Title).Score; hotkey.Score = StringMatcher.FuzzySearch(search, hotkey.Title).Score;

View file

@ -48,9 +48,23 @@ namespace Flow.Launcher.Plugin.PluginsManager
Settings = settings; Settings = settings;
} }
internal async Task UpdateManifest() private Task _downloadManifestTask = Task.CompletedTask;
internal Task UpdateManifest()
{ {
await pluginsManifest.DownloadManifest(); if (_downloadManifestTask.Status == TaskStatus.Running)
{
return _downloadManifestTask;
}
else
{
return _downloadManifestTask = pluginsManifest.DownloadManifest().ContinueWith(t =>
Context.API.ShowMsg("Plugin Manifest Download Fail.",
"Please check if you can connect to github.com. " +
"This error means you may not be able to Install and Update Plugin.", icoPath, false),
TaskContinuationOptions.OnlyOnFaulted);
}
} }
internal List<Result> GetDefaultHotKeys() internal List<Result> GetDefaultHotKeys()
@ -151,8 +165,15 @@ namespace Flow.Launcher.Plugin.PluginsManager
Context.API.RestartApp(); Context.API.RestartApp();
} }
internal List<Result> RequestUpdate(string search) internal async ValueTask<List<Result>> RequestUpdate(string search, CancellationToken token)
{ {
if (!pluginsManifest.UserPlugins.Any())
{
await UpdateManifest();
}
token.ThrowIfCancellationRequested();
var autocompletedResults = AutoCompleteReturnAllResults(search, var autocompletedResults = AutoCompleteReturnAllResults(search,
Settings.HotkeyUpdate, Settings.HotkeyUpdate,
"Update", "Update",
@ -276,20 +297,14 @@ namespace Flow.Launcher.Plugin.PluginsManager
.ToList(); .ToList();
} }
private Task _downloadManifestTask = Task.CompletedTask;
internal async ValueTask<List<Result>> RequestInstallOrUpdate(string searchName, CancellationToken token) internal async ValueTask<List<Result>> RequestInstallOrUpdate(string searchName, CancellationToken token)
{ {
if (!pluginsManifest.UserPlugins.Any() && if (!pluginsManifest.UserPlugins.Any())
_downloadManifestTask.Status != TaskStatus.Running)
{ {
_downloadManifestTask = pluginsManifest.DownloadManifest(); await UpdateManifest();
} }
await _downloadManifestTask; token.ThrowIfCancellationRequested();
if (token.IsCancellationRequested)
return null;
var searchNameWithoutKeyword = searchName.Replace(Settings.HotKeyInstall, string.Empty).Trim(); var searchNameWithoutKeyword = searchName.Replace(Settings.HotKeyInstall, string.Empty).Trim();

View file

@ -6,7 +6,7 @@
"Name": "Plugins Manager", "Name": "Plugins Manager",
"Description": "Management of installing, uninstalling or updating Flow Launcher plugins", "Description": "Management of installing, uninstalling or updating Flow Launcher plugins",
"Author": "Jeremy Wu", "Author": "Jeremy Wu",
"Version": "1.6.4", "Version": "1.7.0",
"Language": "csharp", "Language": "csharp",
"Website": "https://github.com/Flow-Launcher/Flow.Launcher", "Website": "https://github.com/Flow-Launcher/Flow.Launcher",
"ExecuteFileName": "Flow.Launcher.Plugin.PluginsManager.dll", "ExecuteFileName": "Flow.Launcher.Plugin.PluginsManager.dll",