EasyEvent.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. mOnEvent += onEvent;
  43. return new CustomUnRegister(() => { UnRegister(onEvent); });
  44. }
  45. public void UnRegister(Action<T> onEvent) => mOnEvent -= onEvent;
  46. public void Trigger(T t) => mOnEvent?.Invoke(t);
  47. IUnRegister IEasyEvent.Register(Action onEvent)
  48. {
  49. return Register(Action);
  50. void Action(T _) => onEvent();
  51. }
  52. }
  53. public class EasyEvent<T, K> : IEasyEvent
  54. {
  55. private Action<T, K> mOnEvent = (t, k) => { };
  56. public IUnRegister Register(Action<T, K> onEvent)
  57. {
  58. mOnEvent += onEvent;
  59. return new CustomUnRegister(() => { UnRegister(onEvent); });
  60. }
  61. public void UnRegister(Action<T, K> onEvent) => mOnEvent -= onEvent;
  62. public void Trigger(T t, K k) => mOnEvent?.Invoke(t, k);
  63. IUnRegister IEasyEvent.Register(Action onEvent)
  64. {
  65. return Register(Action);
  66. void Action(T _, K __) => onEvent();
  67. }
  68. }
  69. public class EasyEvent<T, K, S> : IEasyEvent
  70. {
  71. private Action<T, K, S> mOnEvent = (t, k, s) => { };
  72. public IUnRegister Register(Action<T, K, S> onEvent)
  73. {
  74. mOnEvent += onEvent;
  75. return new CustomUnRegister(() => { UnRegister(onEvent); });
  76. }
  77. public void UnRegister(Action<T, K, S> onEvent) => mOnEvent -= onEvent;
  78. public void Trigger(T t, K k, S s) => mOnEvent?.Invoke(t, k, s);
  79. IUnRegister IEasyEvent.Register(Action onEvent)
  80. {
  81. return Register(Action);
  82. void Action(T _, K __, S ___) => onEvent();
  83. }
  84. }