apieceoffruit

Signals

Mar 22nd, 2023
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.49 KB | None | 0 0
  1.  public static class Signals
  2.     {
  3.         public static T Get<T>() where T : ISignal, new() => SignalCollection.Get<T>();
  4.  
  5.         public static void Add(string hash, Action action) => SignalCollection.Add(hash, action);
  6.  
  7.         public static void Remove(string hash, Action action) =>
  8.             SignalCollection.Remove(hash, action);
  9.  
  10.         static readonly SignalCollection SignalCollection = new();
  11.     }
  12.    
  13.     public interface ISignal
  14.     {
  15.         string Hash { get; }
  16.  
  17.         public bool HasHash(string hash) => Hash == hash;
  18.     }
  19.  
  20.     public class SignalCollection
  21.     {
  22.         readonly Dictionary<Type, ISignal> _signals = new();
  23.        
  24.         public T Get<T>() where T : ISignal, new()
  25.         {
  26.             if (_signals.TryGetValue(typeof(T), out var signal))
  27.                 return (T)signal;
  28.             return (T)Bind(typeof(T));
  29.         }
  30.  
  31.         public void Add(string hash, Action action)
  32.         {
  33.             var s = Get(hash) as ASignal;
  34.             s?.AddListener(action);
  35.         }
  36.  
  37.         public void Remove(string hash, Action action)
  38.         {
  39.             var s = Get(hash) as ASignal;
  40.             s?.RemoveListener(action);
  41.         }
  42.  
  43.         ISignal Get(string hash) => _signals.Values.FirstOrDefault(signal => signal.HasHash(hash));
  44.        
  45.         ISignal Bind(Type signalType)
  46.         {
  47.             if(_signals.TryGetValue(signalType, out var signal))
  48.             {
  49.                 Debug.LogError($"Signal already registered for type {signalType}");
  50.                 return signal;
  51.             }
  52.             signal = (ISignal)Activator.CreateInstance(signalType);
  53.             _signals.Add(signalType, signal);
  54.             return signal;
  55.         }
  56.     }
  57.    
  58.     public abstract class ABaseSignal : ISignal
  59.     {
  60.         string _hash;
  61.         public string Hash => string.IsNullOrEmpty(_hash) ?  (_hash = GetType().ToString()) : _hash;
  62.     }
  63.    
  64.     public abstract class ASignal : ABaseSignal
  65.     {
  66.         public void AddListener(Action handler) => _action += handler;
  67.         public void RemoveListener(Action action) => _action -= action;
  68.         public void Dispatch() => _action?.Invoke();
  69.         Action _action;
  70.     }
  71.    
  72.     public abstract class ASignal<T>: ABaseSignal
  73.     {
  74.         public void Dispatch(T arg) => _action?.Invoke(arg);
  75.         public void AddListener(Action<T> action) => _action += action;
  76.         public void RemoveListener(Action<T> action) => _action -= action;
  77.         Action<T> _action;
  78.     }
Add Comment
Please, Sign In to add comment