JD2022-TU1/main/tools/services/JD.LocalizationExport.Service/API/ExportEventsWrapper.cs

61 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace JD.LocalizationExport.Service.API
{
// TODO: Refactor to introduce Deferrals if better solution (https://blog.stephencleary.com/2013/02/async-oop-5-events.html)
public class ExportEventsWrapper
{
public event Func<ExportType, Task> ExportStarted;
public event Func<ExportType, Task> ExportEnded;
public event Func<ExportType, string, Uri, Task> ExportFileStarted;
public event Func<ExportType, string, Uri, Task> ExportFileEnded;
public async Task InvokeExportStartedAsync(ExportType exportType)
{
if (ExportStarted != null)
{
Func<ExportType, Task>[] handlers = ExportStarted.GetInvocationList().Cast<Func<ExportType, Task>>().ToArray();
IEnumerable<Task> handlerInvocations = handlers.Select(h => h.Invoke(exportType));
await Task.WhenAll(handlerInvocations);
}
}
public async Task InvokeExportEndedAsync(ExportType exportType)
{
if (ExportEnded != null)
{
Func<ExportType, Task>[] handlers = ExportEnded.GetInvocationList().Cast<Func<ExportType, Task>>().ToArray();
IEnumerable<Task> handlerInvocations = handlers.Select(h => h.Invoke(exportType));
await Task.WhenAll(handlerInvocations);
}
}
public async Task InvokeExportFileStartedAsync(ExportType exportType, string filePath, Uri requestUri = null)
{
if (ExportFileStarted != null)
{
Func<ExportType, string, Uri, Task>[] handlers = ExportFileStarted.GetInvocationList().Cast<Func<ExportType, string, Uri, Task>>().ToArray();
IEnumerable<Task> handlerInvocations = handlers.Select(h => h.Invoke(exportType, filePath, requestUri));
await Task.WhenAll(handlerInvocations);
}
}
public async Task InvokeExportFileEndedAsync(ExportType exportType, string filePath, Uri requestUri = null)
{
if (ExportFileEnded != null)
{
Func<ExportType, string, Uri, Task>[] handlers = ExportFileEnded.GetInvocationList().Cast<Func<ExportType, string, Uri, Task>>().ToArray();
IEnumerable<Task> handlerInvocations = handlers.Select(h => h.Invoke(exportType, filePath, requestUri));
await Task.WhenAll(handlerInvocations);
}
}
}
}