ActiveValue.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using EventBus;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace EventBus
  6. {
  7. public abstract class BaseValue
  8. {
  9. public BaseValue(Properties? properties = null)
  10. {
  11. this.Properties = properties ?? Properties.Default;
  12. this.IntPtr = Guid.NewGuid();
  13. }
  14. public DateTime DateTime => DateTime.Now;
  15. private Properties properties = Properties.Default;
  16. public Properties Properties { get => properties ?? Properties.Default; private set => properties = value; }
  17. public abstract bool HasDelegate { get; }
  18. public Guid IntPtr { get; internal set; }
  19. }
  20. public class ActionValue<TData> : BaseValue
  21. {
  22. public ActionValue(Action<object, EventArgs<TData>> action, Properties? properties = null) : base(properties)
  23. {
  24. this.Action = action;
  25. }
  26. public Action<object, EventArgs<TData>> Action { get;private set; }
  27. public override bool HasDelegate => Action != null;
  28. public bool Invoke(object sender, TData args, IBaseEventData eventData)
  29. {
  30. if (Action == null) return false;
  31. var eventargs = new EventArgs<TData>(args, IntPtr, eventData);
  32. Action.Invoke(sender, eventargs);
  33. return eventargs.Handle;
  34. }
  35. }
  36. public abstract class BaseFuncValue<TData, TResult> : BaseValue
  37. {
  38. public BaseFuncValue(Properties? properties = null) : base(properties)
  39. {
  40. }
  41. public abstract TResult Invoke(object sender, TData data, IBaseEventData eventData);
  42. public abstract List<TResult> InvokeList(object sender, TData data, IBaseEventData eventData);
  43. public abstract bool IsListFunc { get; }
  44. }
  45. public class FuncValue<TData, TRestlt> : BaseFuncValue<TData, TRestlt>
  46. {
  47. public FuncValue(Func<object, EventArgs<TData>, TRestlt> func, Properties? properties = null) : base(properties)
  48. {
  49. Func = func;
  50. }
  51. internal Func<object, EventArgs<TData>, TRestlt> Func { get; private set; }
  52. public override bool IsListFunc => false;
  53. public override bool HasDelegate => Func != null;
  54. public override TRestlt Invoke(object sender, TData data, IBaseEventData eventData)
  55. {
  56. ArgumentNullException.ThrowIfNull(Func);
  57. return Func.Invoke(sender, new EventArgs<TData>(data, IntPtr, eventData, typeof(TRestlt)));
  58. }
  59. public override List<TRestlt> InvokeList(object sender, TData data, IBaseEventData eventData)
  60. {
  61. throw new NotImplementedException();
  62. }
  63. }
  64. internal class ListFuncValue<TData, TRestlt> : BaseFuncValue<TData, TRestlt>
  65. {
  66. public ListFuncValue(Func<object, EventArgs<TData>, List<TRestlt>> func, Properties? properties = null) : base(properties)
  67. {
  68. Func = func;
  69. }
  70. public Func<object, EventArgs<TData>, List<TRestlt>> Func { get; private set; }
  71. public override bool IsListFunc => true;
  72. public override bool HasDelegate => Func != null;
  73. public override TRestlt Invoke(object sender, TData data, IBaseEventData eventData)
  74. {
  75. throw new NotImplementedException();
  76. }
  77. public override List<TRestlt> InvokeList(object sender, TData data, IBaseEventData eventData)
  78. {
  79. ArgumentNullException.ThrowIfNull(Func);
  80. return Func.Invoke(sender, new EventArgs<TData>(data, IntPtr, eventData, typeof(TRestlt)));
  81. }
  82. }
  83. }