EasyEvent.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. public struct CustomUnRegister : IUnRegister
  4. {
  5. private Action mOnUnRegister { get; set; }
  6. public CustomUnRegister(Action onUnRegister) => mOnUnRegister = onUnRegister;
  7. public void UnRegister()
  8. {
  9. mOnUnRegister.Invoke();
  10. mOnUnRegister = null;
  11. }
  12. }
  13. public interface IUnRegister
  14. {
  15. void UnRegister();
  16. }
  17. public interface IUnRegisterList
  18. {
  19. List<IUnRegister> UnregisterList { get; }
  20. }
  21. public interface IEasyEvent
  22. {
  23. IUnRegister Register(Action onEvent);
  24. }
  25. public class EasyEvent : IEasyEvent
  26. {
  27. private Action mOnEvent = () => { };
  28. public IUnRegister Register(Action onEvent)
  29. {
  30. mOnEvent += onEvent;
  31. return new CustomUnRegister(() => { UnRegister(onEvent); });
  32. }
  33. public void UnRegister(Action onEvent) => mOnEvent -= onEvent;
  34. public void Trigger() => mOnEvent?.Invoke();
  35. }
  36. public class EasyEvent<T> : IEasyEvent
  37. {
  38. private Action<T> mOnEvent = e => { };
  39. public IUnRegister Register(Action<T> onEvent)
  40. {
  41. mOnEvent += onEvent;
  42. return new CustomUnRegister(() => { UnRegister(onEvent); });
  43. }
  44. public void UnRegister(Action<T> onEvent) => mOnEvent -= onEvent;
  45. public void Trigger(T t) => mOnEvent?.Invoke(t);
  46. IUnRegister IEasyEvent.Register(Action onEvent)
  47. {
  48. return Register(Action);
  49. void Action(T _) => onEvent();
  50. }
  51. }
  52. public class EasyEvent<T, K> : IEasyEvent
  53. {
  54. private Action<T, K> mOnEvent = (t, k) => { };
  55. public IUnRegister Register(Action<T, K> onEvent)
  56. {
  57. mOnEvent += onEvent;
  58. return new CustomUnRegister(() => { UnRegister(onEvent); });
  59. }
  60. public void UnRegister(Action<T, K> onEvent) => mOnEvent -= onEvent;
  61. public void Trigger(T t, K k) => mOnEvent?.Invoke(t, k);
  62. IUnRegister IEasyEvent.Register(Action onEvent)
  63. {
  64. return Register(Action);
  65. void Action(T _, K __) => onEvent();
  66. }
  67. }
  68. public class EasyEvent<T, K, S> : IEasyEvent
  69. {
  70. private Action<T, K, S> mOnEvent = (t, k, s) => { };
  71. public IUnRegister Register(Action<T, K, S> onEvent)
  72. {
  73. mOnEvent += onEvent;
  74. return new CustomUnRegister(() => { UnRegister(onEvent); });
  75. }
  76. public void UnRegister(Action<T, K, S> onEvent) => mOnEvent -= onEvent;
  77. public void Trigger(T t, K k, S s) => mOnEvent?.Invoke(t, k, s);
  78. IUnRegister IEasyEvent.Register(Action onEvent)
  79. {
  80. return Register(Action);
  81. void Action(T _, K __, S ___) => onEvent();
  82. }
  83. }