EasyEvent.cs 2.7 KB

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