using System; using System.Collections.Generic; namespace ET { public class EventDispatcher<A> { private readonly Dictionary<Type, List<Action<A>>> _list = new(); public EventDispatcher() { } public Action<A> AddListener<T>(Action<A> action) { if (_list.TryGetValue(typeof(T), out List<Action<A>> acts)) { acts.Add(action); } else { _list.Add(typeof(T), new List<Action<A>> { action }); } return action; } public bool Notify(A ev) { if (_list.TryGetValue(ev.GetType(), out List<Action<A>> acts)) { foreach (var act in acts) { act.Invoke(ev); } return acts.Count > 0; } return false; } } public class EventDispatcher<KeyType, ActionParm> { private readonly Dictionary<KeyType, List<Action<ActionParm>>> _list = new(); public EventDispatcher() { } public Action<ActionParm> AddListener(KeyType kt, Action<ActionParm> action) { if (_list.TryGetValue(kt, out List<Action<ActionParm>> acts)) { acts.Add(action); } else { _list.Add(kt, new List<Action<ActionParm>> { action }); } return action; } public bool Notify(KeyType kt, ActionParm data) { if (_list.TryGetValue(kt, out List<Action<ActionParm>> acts)) { foreach (var act in acts) { act.Invoke(data); } return acts.Count > 0; } return false; } } }