This commit is contained in:
2025-05-30 16:05:49 +03:00
committed by Syntriax
parent 846aa75dd5
commit b5140a94de
57 changed files with 437 additions and 462 deletions

View File

@@ -3,31 +3,14 @@ using System.Collections.Generic;
namespace Syntriax.Engine.Core;
public class Event
public class Event<TSender>
{
private readonly List<Action> listeners = new(1000);
private readonly List<EventHandler> listeners = new(8);
public void AddListener(Action listener) => listeners.Add(listener);
public void RemoveListener(Action listener) => listeners.Remove(listener);
public void Invoke()
{
for (int i = 0; i < listeners.Count; i++)
try { listeners[i].Invoke(); }
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Method.DeclaringType?.FullName}.{listeners[i].Method.Name}()";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
}
}
public class Event<T1>
{
private readonly List<Action<T1>> listeners = new(1000);
public void AddListener(Action<T1> listener) => listeners.Add(listener);
public void RemoveListener(Action<T1> listener) => listeners.Remove(listener);
public void Invoke(T1 argument)
public void AddListener(EventHandler listener) => listeners.Add(listener);
public void RemoveListener(EventHandler listener) => listeners.Remove(listener);
public void Clear() => listeners.Clear();
public void Invoke(TSender argument)
{
for (int i = 0; i < listeners.Count; i++)
try { listeners[i].Invoke(argument); }
@@ -37,58 +20,27 @@ public class Event<T1>
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
}
public delegate void EventHandler(TSender sender);
}
public class Event<T1, T2>
public class Event<TSender, TArguments>
{
private readonly List<Action<T1, T2>> listeners = new(1000);
private readonly List<EventHandler> listeners = new(8);
public void AddListener(Action<T1, T2> listener) => listeners.Add(listener);
public void RemoveListener(Action<T1, T2> listener) => listeners.Remove(listener);
public void Invoke(T1 argument1, T2 argument2)
public void AddListener(EventHandler listener) => listeners.Add(listener);
public void RemoveListener(EventHandler listener) => listeners.Remove(listener);
public void Clear() => listeners.Clear();
public void Invoke(TSender sender, TArguments arguments)
{
for (int i = 0; i < listeners.Count; i++)
try { listeners[i].Invoke(argument1, argument2); }
try { listeners[i].Invoke(sender, arguments); }
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Method.DeclaringType?.FullName}.{listeners[i].Method.Name}({string.Join(", ", argument1, argument2)})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
}
}
public class Event<T1, T2, T3>
{
private readonly List<Action<T1, T2, T3>> listeners = new(1000);
public void AddListener(Action<T1, T2, T3> listener) => listeners.Add(listener);
public void RemoveListener(Action<T1, T2, T3> listener) => listeners.Remove(listener);
public void Invoke(T1 argument1, T2 argument2, T3 argument3)
{
for (int i = 0; i < listeners.Count; i++)
try { listeners[i].Invoke(argument1, argument2, argument3); }
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Method.DeclaringType?.FullName}.{listeners[i].Method.Name}({string.Join(", ", argument1, argument2, argument3)})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
}
}
public class Event<T1, T2, T3, T4>
{
private readonly List<Action<T1, T2, T3, T4>> listeners = new(1000);
public void AddListener(Action<T1, T2, T3, T4> listener) => listeners.Add(listener);
public void RemoveListener(Action<T1, T2, T3, T4> listener) => listeners.Remove(listener);
public void Invoke(T1 argument1, T2 argument2, T3 argument3, T4 argument4)
{
for (int i = 0; i < listeners.Count; i++)
try { listeners[i].Invoke(argument1, argument2, argument3, argument4); }
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Method.DeclaringType?.FullName}.{listeners[i].Method.Name}({string.Join(", ", argument1, argument2, argument3, argument4)})";
string methodCallRepresentation = $"{listeners[i].Method.DeclaringType?.FullName}.{listeners[i].Method.Name}({string.Join(", ", sender, arguments)})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
}
public delegate void EventHandler(TSender sender, TArguments arguments);
}