1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-10 15:20:48 -05:00

implement new eventing system

This commit is contained in:
RaidMax
2023-04-05 09:54:57 -05:00
parent 53a6ef2ec3
commit ebe69a94ad
39 changed files with 1410 additions and 526 deletions

View File

@ -0,0 +1,44 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace SharedLibraryCore.Events;
public static class EventExtensions
{
public static Task InvokeAsync<TEventType>(this Func<TEventType, CancellationToken, Task> function,
TEventType eventArgType, CancellationToken token)
{
if (function is null)
{
return Task.CompletedTask;
}
return Task.WhenAll(function.GetInvocationList().Cast<Func<TEventType, CancellationToken, Task>>()
.Select(x => RunHandler(x, eventArgType, token)));
}
private static async Task RunHandler<TEventType>(Func<TEventType, CancellationToken, Task> handler,
TEventType eventArgType, CancellationToken token)
{
if (token == CancellationToken.None)
{
// special case to allow tasks like request after delay to run longer
await handler(eventArgType, token);
}
using var timeoutToken = new CancellationTokenSource(Utilities.DefaultCommandTimeout);
using var tokenSource =
CancellationTokenSource.CreateLinkedTokenSource(token, timeoutToken.Token);
try
{
await handler(eventArgType, tokenSource.Token);
}
catch (Exception)
{
// ignored
}
}
}