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