123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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<TData> : BaseValue
- {
- public ActionValue(Action<object, EventArgs<TData>> action, Properties? properties = null) : base(properties)
- {
- this.Action = action;
- }
- public Action<object, EventArgs<TData>> 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<TData>(args, IntPtr, eventData);
- Action.Invoke(sender, eventargs);
- return eventargs.Handle;
- }
- }
- public abstract class BaseFuncValue<TData, TResult> : BaseValue
- {
- public BaseFuncValue(Properties? properties = null) : base(properties)
- {
- }
- public abstract TResult Invoke(object sender, TData data, IBaseEventData eventData);
- public abstract List<TResult> InvokeList(object sender, TData data, IBaseEventData eventData);
- public abstract bool IsListFunc { get; }
- }
- public class FuncValue<TData, TRestlt> : BaseFuncValue<TData, TRestlt>
- {
- public FuncValue(Func<object, EventArgs<TData>, TRestlt> func, Properties? properties = null) : base(properties)
- {
- Func = func;
- }
- internal Func<object, EventArgs<TData>, 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<TData>(data, IntPtr, eventData, typeof(TRestlt)));
- }
- public override List<TRestlt> InvokeList(object sender, TData data, IBaseEventData eventData)
- {
- throw new NotImplementedException();
- }
- }
- internal class ListFuncValue<TData, TRestlt> : BaseFuncValue<TData, TRestlt>
- {
- public ListFuncValue(Func<object, EventArgs<TData>, List<TRestlt>> func, Properties? properties = null) : base(properties)
- {
- Func = func;
- }
- public Func<object, EventArgs<TData>, List<TRestlt>> 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<TRestlt> InvokeList(object sender, TData data, IBaseEventData eventData)
- {
- ArgumentNullException.ThrowIfNull(Func);
- return Func.Invoke(sender, new EventArgs<TData>(data, IntPtr, eventData, typeof(TRestlt)));
- }
- }
- }
|