-
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathStartup.cs
More file actions
78 lines (67 loc) · 2.44 KB
/
Startup.cs
File metadata and controls
78 lines (67 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using Beutl.Api;
using Beutl.Api.Services;
namespace Beutl.Services.StartupTasks;
public sealed class Startup
{
private readonly Dictionary<Type, Lazy<StartupTask>> _tasks = [];
private readonly BeutlApiApplication _apiApp;
public Startup(BeutlApiApplication apiApp)
{
_apiApp = apiApp;
Initialize();
}
private async void Initialize()
{
using (Activity? activity = Telemetry.StartActivity("Startup"))
{
RegisterAll();
await WaitAll().ConfigureAwait(false);
}
}
public async Task WaitAll()
{
await Task.WhenAll(_tasks.Values.Select(v => v.Value.Task)).ConfigureAwait(false);
}
public async Task WaitLoadingExtensions()
{
LoadInstalledExtensionTask t1 = GetTask<LoadInstalledExtensionTask>();
LoadPrimitiveExtensionTask t2 = GetTask<LoadPrimitiveExtensionTask>();
LoadSideloadExtensionTask t3 = GetTask<LoadSideloadExtensionTask>();
await Task.WhenAll(t1.Task, t2.Task, t3.Task).ConfigureAwait(false);
}
public T GetTask<T>()
where T : StartupTask
{
if (_tasks.TryGetValue(typeof(T), out Lazy<StartupTask>? lazy))
{
return (T)lazy.Value;
}
foreach (KeyValuePair<Type, Lazy<StartupTask>> item in _tasks)
{
if (item.Key.IsAssignableTo(typeof(T)))
{
return (T)item.Value.Value;
}
}
throw new Exception("Task not found");
}
private void RegisterAll()
{
Register(() => new AuthenticationTask(_apiApp));
Register(() => new ResolvePackageDependenciesTask(
_apiApp.GetResource<InstalledPackageRepository>(),
_apiApp.GetResource<PackageInstaller>()));
Register(() => new LoadInstalledExtensionTask(
_apiApp.GetResource<PackageManager>(), this));
Register(() => new LoadPrimitiveExtensionTask(_apiApp.GetResource<PackageManager>()));
Register(() => new LoadSideloadExtensionTask(_apiApp.GetResource<PackageManager>()));
Register(() => new AfterLoadingExtensionsTask(this));
Register(() => new CheckForUpdatesTask(_apiApp));
Register(() => new CheckForPackageUpdatesTask(this, _apiApp.GetResource<PackageManager>()));
}
private void Register<T>(Func<T> factory)
where T : StartupTask
{
_tasks.Add(typeof(T), new Lazy<StartupTask>(() => factory()));
}
}