mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2024-11-24 11:42:45 +01:00
44 lines
1016 B
C#
44 lines
1016 B
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using DHT.Server.Data.Filters;
|
|
using DHT.Server.Database;
|
|
|
|
namespace DHT.Server.Download;
|
|
|
|
public sealed class Downloader {
|
|
private DownloaderTask? current;
|
|
public bool IsDownloading => current != null;
|
|
|
|
private readonly IDatabaseFile db;
|
|
private readonly int? concurrentDownloads;
|
|
private readonly SemaphoreSlim semaphore = new (1, 1);
|
|
|
|
internal Downloader(IDatabaseFile db, int? concurrentDownloads) {
|
|
this.db = db;
|
|
this.concurrentDownloads = concurrentDownloads;
|
|
}
|
|
|
|
public async Task<IObservable<DownloadItem>> Start(DownloadItemFilter filter) {
|
|
await semaphore.WaitAsync();
|
|
try {
|
|
current ??= new DownloaderTask(db, filter, concurrentDownloads);
|
|
return current.FinishedItems;
|
|
} finally {
|
|
semaphore.Release();
|
|
}
|
|
}
|
|
|
|
public async Task Stop() {
|
|
await semaphore.WaitAsync();
|
|
try {
|
|
if (current != null) {
|
|
await current.DisposeAsync();
|
|
current = null;
|
|
}
|
|
} finally {
|
|
semaphore.Release();
|
|
}
|
|
}
|
|
}
|