Skip to content

Commit 485b314

Browse files
authored
Merge pull request #297 from taooceros/PluginManagerManifestTimeout
Plugin manager init timeout
2 parents e9e99ab + 2fecfa2 commit 485b314

File tree

3 files changed

+68
-36
lines changed

3 files changed

+68
-36
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,17 @@ public async Task InitAsync(PluginInitContext context)
3737
Settings = viewModel.Settings;
3838
contextMenu = new ContextMenu(Context);
3939
pluginManager = new PluginsManager(Context, Settings);
40-
await pluginManager.UpdateManifest();
41-
lastUpdateTime = DateTime.Now;
40+
var updateManifestTask = pluginManager.UpdateManifest();
41+
if (await Task.WhenAny(updateManifestTask, Task.Delay(500)) == updateManifestTask)
42+
{
43+
lastUpdateTime = DateTime.Now;
44+
}
45+
else
46+
{
47+
context.API.ShowMsg("Plugin Manifest Download Fail.",
48+
@"Please check internet transmission with Github.com.
49+
You may not be able to Install and Update Plugin.", pluginManager.icoPath);
50+
}
4251
}
4352

4453
public List<Result> LoadContextMenus(Result selectedResult)
@@ -61,7 +70,7 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
6170

6271
return search switch
6372
{
64-
var s when s.StartsWith(Settings.HotKeyInstall) => pluginManager.RequestInstallOrUpdate(s),
73+
var s when s.StartsWith(Settings.HotKeyInstall) => await pluginManager.RequestInstallOrUpdate(s, token),
6574
var s when s.StartsWith(Settings.HotkeyUninstall) => pluginManager.RequestUninstall(s),
6675
var s when s.StartsWith(Settings.HotkeyUpdate) => pluginManager.RequestUpdate(s),
6776
_ => pluginManager.GetDefaultHotKeys().Where(hotkey =>
@@ -93,4 +102,4 @@ public async Task ReloadDataAsync()
93102
lastUpdateTime = DateTime.Now;
94103
}
95104
}
96-
}
105+
}

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

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.Generic;
88
using System.IO;
99
using System.Linq;
10+
using System.Threading;
1011
using System.Threading.Tasks;
1112
using System.Windows;
1213

@@ -36,7 +37,7 @@ private bool ShouldHideWindow
3637
}
3738
}
3839

39-
private readonly string icoPath = "Images\\pluginsmanager.png";
40+
internal readonly string icoPath = "Images\\pluginsmanager.png";
4041

4142
internal PluginsManager(PluginInitContext context, Settings settings)
4243
{
@@ -64,27 +65,27 @@ internal List<Result> GetDefaultHotKeys()
6465
return false;
6566
}
6667
},
67-
new Result()
68+
new Result()
69+
{
70+
Title = Settings.HotkeyUninstall,
71+
IcoPath = icoPath,
72+
Action = _ =>
6873
{
69-
Title = Settings.HotkeyUninstall,
70-
IcoPath = icoPath,
71-
Action = _ =>
72-
{
73-
Context.API.ChangeQuery("pm uninstall ");
74-
return false;
75-
}
76-
},
77-
new Result()
74+
Context.API.ChangeQuery("pm uninstall ");
75+
return false;
76+
}
77+
},
78+
new Result()
79+
{
80+
Title = Settings.HotkeyUpdate,
81+
IcoPath = icoPath,
82+
Action = _ =>
7883
{
79-
Title = Settings.HotkeyUpdate,
80-
IcoPath = icoPath,
81-
Action = _ =>
82-
{
83-
Context.API.ChangeQuery("pm update ");
84-
return false;
85-
}
84+
Context.API.ChangeQuery("pm update ");
85+
return false;
8686
}
87-
};
87+
}
88+
};
8889
}
8990

9091
internal async Task InstallOrUpdate(UserPlugin plugin)
@@ -137,7 +138,8 @@ internal async Task InstallOrUpdate(UserPlugin plugin)
137138
catch (Exception e)
138139
{
139140
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"),
140-
string.Format(Context.API.GetTranslation("plugin_pluginsmanager_install_error_subtitle"), plugin.Name));
141+
string.Format(Context.API.GetTranslation("plugin_pluginsmanager_install_error_subtitle"),
142+
plugin.Name));
141143

142144
Log.Exception("PluginsManager", "An error occured while downloading plugin", e, "InstallOrUpdate");
143145

@@ -164,7 +166,8 @@ internal List<Result> RequestUpdate(string search)
164166
from existingPlugin in Context.API.GetAllPlugins()
165167
join pluginFromManifest in pluginsManifest.UserPlugins
166168
on existingPlugin.Metadata.ID equals pluginFromManifest.ID
167-
where existingPlugin.Metadata.Version.CompareTo(pluginFromManifest.Version) < 0 // if current version precedes manifest version
169+
where existingPlugin.Metadata.Version.CompareTo(pluginFromManifest.Version) <
170+
0 // if current version precedes manifest version
168171
select
169172
new
170173
{
@@ -214,22 +217,29 @@ on existingPlugin.Metadata.ID equals pluginFromManifest.ID
214217

215218
Task.Run(async delegate
216219
{
217-
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
218-
Context.API.GetTranslation("plugin_pluginsmanager_please_wait"));
220+
Context.API.ShowMsg(
221+
Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
222+
Context.API.GetTranslation("plugin_pluginsmanager_please_wait"));
219223

220-
await Http.DownloadAsync(x.PluginNewUserPlugin.UrlDownload, downloadToFilePath).ConfigureAwait(false);
224+
await Http.DownloadAsync(x.PluginNewUserPlugin.UrlDownload, downloadToFilePath)
225+
.ConfigureAwait(false);
221226

222-
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
223-
Context.API.GetTranslation("plugin_pluginsmanager_download_success"));
227+
Context.API.ShowMsg(
228+
Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"),
229+
Context.API.GetTranslation("plugin_pluginsmanager_download_success"));
224230

225231
Install(x.PluginNewUserPlugin, downloadToFilePath);
226232

227233
Context.API.RestartApp();
228234
}).ContinueWith(t =>
229235
{
230-
Log.Exception("PluginsManager", $"Update failed for {x.Name}", t.Exception.InnerException, "RequestUpdate");
231-
Context.API.ShowMsg(Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"),
232-
string.Format(Context.API.GetTranslation("plugin_pluginsmanager_install_error_subtitle"), x.Name));
236+
Log.Exception("PluginsManager", $"Update failed for {x.Name}",
237+
t.Exception.InnerException, "RequestUpdate");
238+
Context.API.ShowMsg(
239+
Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"),
240+
string.Format(
241+
Context.API.GetTranslation("plugin_pluginsmanager_install_error_subtitle"),
242+
x.Name));
233243
}, TaskContinuationOptions.OnlyOnFaulted);
234244

235245
return true;
@@ -264,8 +274,21 @@ internal List<Result> Search(IEnumerable<Result> results, string searchName)
264274
.ToList();
265275
}
266276

267-
internal List<Result> RequestInstallOrUpdate(string searchName)
277+
private Task _downloadManifestTask = Task.CompletedTask;
278+
279+
internal async ValueTask<List<Result>> RequestInstallOrUpdate(string searchName, CancellationToken token)
268280
{
281+
if (!pluginsManifest.UserPlugins.Any() &&
282+
_downloadManifestTask.Status != TaskStatus.Running)
283+
{
284+
_downloadManifestTask = pluginsManifest.DownloadManifest();
285+
}
286+
287+
await _downloadManifestTask;
288+
289+
if (token.IsCancellationRequested)
290+
return null;
291+
269292
var searchNameWithoutKeyword = searchName.Replace(Settings.HotKeyInstall, string.Empty).Trim();
270293

271294
var results =
@@ -406,4 +429,4 @@ private List<Result> AutoCompleteReturnAllResults(string search, string hotkey,
406429
return new List<Result>();
407430
}
408431
}
409-
}
432+
}

Plugins/Flow.Launcher.Plugin.PluginsManager/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"Name": "Plugins Manager",
77
"Description": "Management of installing, uninstalling or updating Flow Launcher plugins",
88
"Author": "Jeremy Wu",
9-
"Version": "1.5.0",
9+
"Version": "1.5.1",
1010
"Language": "csharp",
1111
"Website": "https://github.com/Flow-Launcher/Flow.Launcher",
1212
"ExecuteFileName": "Flow.Launcher.Plugin.PluginsManager.dll",

0 commit comments

Comments
 (0)