using EventBus; using System; using System.Collections.Generic; using System.Text; namespace EventBus { public abstract class BaseValue { public BaseValue(Properties? properties = null) { this.Properties = properties ?? Properties.Default; this.IntPtr = Guid.NewGuid(); } public DateTime DateTime => DateTime.Now; private Properties properties = Properties.Default; public Properties Properties { get => properties ?? Properties.Default; private set => properties = value; } public abstract bool HasDelegate { get; } public Guid IntPtr { get; internal set; } } public class ActionValue : BaseValue { public ActionValue(Action> action, Properties? properties = null) : base(properties) { this.Action = action; } public Action> Action { get;private set; } public override bool HasDelegate => Action != null; public bool Invoke(object sender, TData args, IBaseEventData eventData) { if (Action == null) return false; var eventargs = new EventArgs(args, IntPtr, eventData); Action.Invoke(sender, eventargs); return eventargs.Handle; } } public abstract class BaseFuncValue : BaseValue { public BaseFuncValue(Properties? properties = null) : base(properties) { } public abstract TResult Invoke(object sender, TData data, IBaseEventData eventData); public abstract List InvokeList(object sender, TData data, IBaseEventData eventData); public abstract bool IsListFunc { get; } } public class FuncValue : BaseFuncValue { public FuncValue(Func, TRestlt> func, Properties? properties = null) : base(properties) { Func = func; } internal Func, TRestlt> Func { get; private set; } public override bool IsListFunc => false; public override bool HasDelegate => Func != null; public override TRestlt Invoke(object sender, TData data, IBaseEventData eventData) { ArgumentNullException.ThrowIfNull(Func); return Func.Invoke(sender, new EventArgs(data, IntPtr, eventData, typeof(TRestlt))); } public override List InvokeList(object sender, TData data, IBaseEventData eventData) { throw new NotImplementedException(); } } internal class ListFuncValue : BaseFuncValue { public ListFuncValue(Func, List> func, Properties? properties = null) : base(properties) { Func = func; } public Func, List> Func { get; private set; } public override bool IsListFunc => true; public override bool HasDelegate => Func != null; public override TRestlt Invoke(object sender, TData data, IBaseEventData eventData) { throw new NotImplementedException(); } public override List InvokeList(object sender, TData data, IBaseEventData eventData) { ArgumentNullException.ThrowIfNull(Func); return Func.Invoke(sender, new EventArgs(data, IntPtr, eventData, typeof(TRestlt))); } } }