using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Threading.Tasks; namespace Discord { internal class AsyncEvent where T : class { private readonly object _subLock = new object(); internal ImmutableArray _subscriptions; public bool HasSubscribers => _subscriptions.Length != 0; public IReadOnlyList Subscriptions => _subscriptions; public AsyncEvent() { _subscriptions = ImmutableArray.Create(); } 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> eventHandler) { var subscribers = eventHandler.Subscriptions; for (int i = 0; i < subscribers.Count; i++) await subscribers[i].Invoke().ConfigureAwait(false); } public static async Task InvokeAsync(this AsyncEvent> 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(this AsyncEvent> 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(this AsyncEvent> 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(this AsyncEvent> 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(this AsyncEvent> 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); } } }