-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathDownloadManager.cs
More file actions
79 lines (61 loc) · 2.8 KB
/
Copy pathDownloadManager.cs
File metadata and controls
79 lines (61 loc) · 2.8 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
79
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
namespace GeneralUpdate.Core.Download
{
[Obsolete("Use IDownloadOrchestrator + DefaultDownloadOrchestrator instead. Will be removed in v11.")]
public class DownloadManager(string path, string format, int timeOut)
{
#region Private Members
private readonly ImmutableList<DownloadTask>.Builder _downloadTasksBuilder = ImmutableList.Create<DownloadTask>().ToBuilder();
private ImmutableList<DownloadTask> _downloadTasks;
#endregion Private Members
#region Public Properties
public List<(object, string)> FailedVersions { get; } = new();
public string Path => path;
public string Format => format;
public int TimeOut => timeOut;
private ImmutableList<DownloadTask> DownloadTasks => _downloadTasks ?? _downloadTasksBuilder.ToImmutable();
public event EventHandler<MultiAllDownloadCompletedEventArgs> MultiAllDownloadCompleted;
public event EventHandler<MultiDownloadCompletedEventArgs> MultiDownloadCompleted;
public event EventHandler<MultiDownloadErrorEventArgs> MultiDownloadError;
public event EventHandler<MultiDownloadStatisticsEventArgs> MultiDownloadStatistics;
#endregion Public Properties
#region Public Methods
public async Task LaunchTasksAsync()
{
try
{
var downloadTasks = DownloadTasks.Select(task => task.LaunchAsync()).ToList();
await Task.WhenAll(downloadTasks);
MultiAllDownloadCompleted?.Invoke(this, new MultiAllDownloadCompletedEventArgs(true, FailedVersions));
}
catch (Exception ex)
{
MultiAllDownloadCompleted?.Invoke(this, new MultiAllDownloadCompletedEventArgs(false, FailedVersions));
throw new Exception($"Download manager error: {ex.Message}", ex);
}
}
public void OnMultiDownloadStatistics(object sender, MultiDownloadStatisticsEventArgs e)
=> MultiDownloadStatistics?.Invoke(this, e);
public void OnMultiAsyncCompleted(object sender, MultiDownloadCompletedEventArgs e)
=> MultiDownloadCompleted?.Invoke(this, e);
public void OnMultiDownloadError(object sender, MultiDownloadErrorEventArgs e)
{
MultiDownloadError?.Invoke(this, e);
FailedVersions.Add((e.Version, e.Exception.Message));
}
public void Add(DownloadTask task)
{
Debug.Assert(task != null);
if (!_downloadTasksBuilder.Contains(task))
{
_downloadTasksBuilder.Add(task);
}
}
#endregion Public Methods
}
}