You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
3.1 KiB

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
namespace Discord
{
internal class AsyncEvent<T>
where T : class
{
private readonly object _subLock = new object();
internal ImmutableArray<T> _subscriptions;
public bool HasSubscribers => _subscriptions.Length != 0;
public IReadOnlyList<T> Subscriptions => _subscriptions;
public AsyncEvent()
{
_subscriptions = ImmutableArray.Create<T>();
}
public void Add(T subscriber)
{
Preconditions.NotNull(subscriber, nameof(subscriber));
lock (_subLock)
_subscriptions = _subscriptions.Add(subscriber);
}
public void Remove(T subscriber)
{
Preconditions.NotNull(subscriber, nameof(subscriber));
lock (_subLock)
_subscriptions = _subscriptions.Remove(subscriber);
}
}
internal static class EventExtensions
{
public static async Task InvokeAsync(this AsyncEvent<Func<Task>> eventHandler)
{
var subscribers = eventHandler.Subscriptions;
for (int i = 0; i < subscribers.Count; i++)
await subscribers[i].Invoke().ConfigureAwait(false);
}
public static async Task InvokeAsync<T>(this AsyncEvent<Func<T, Task>> eventHandler, T arg)
{
var subscribers = eventHandler.Subscriptions;
for (int i = 0; i < subscribers.Count; i++)
await subscribers[i].Invoke(arg).ConfigureAwait(false);
}
public static async Task InvokeAsync<T1, T2>(this AsyncEvent<Func<T1, T2, Task>> eventHandler, T1 arg1, T2 arg2)
{
var subscribers = eventHandler.Subscriptions;
for (int i = 0; i < subscribers.Count; i++)
await subscribers[i].Invoke(arg1, arg2).ConfigureAwait(false);
}
public static async Task InvokeAsync<T1, T2, T3>(this AsyncEvent<Func<T1, T2, T3, Task>> eventHandler, T1 arg1, T2 arg2, T3 arg3)
{
var subscribers = eventHandler.Subscriptions;
for (int i = 0; i < subscribers.Count; i++)
await subscribers[i].Invoke(arg1, arg2, arg3).ConfigureAwait(false);
}
public static async Task InvokeAsync<T1, T2, T3, T4>(this AsyncEvent<Func<T1, T2, T3, T4, Task>> eventHandler, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
var subscribers = eventHandler.Subscriptions;
for (int i = 0; i < subscribers.Count; i++)
await subscribers[i].Invoke(arg1, arg2, arg3, arg4).ConfigureAwait(false);
}
public static async Task InvokeAsync<T1, T2, T3, T4, T5>(this AsyncEvent<Func<T1, T2, T3, T4, T5, Task>> eventHandler, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
{
var subscribers = eventHandler.Subscriptions;
for (int i = 0; i < subscribers.Count; i++)
await subscribers[i].Invoke(arg1, arg2, arg3, arg4, arg5).ConfigureAwait(false);
}
}
}