Added Event Pipeline System
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Mail;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.EventPipelines
|
||||
{
|
||||
public class Pipeline <T> : I_Pipeline where T : EventArgs
|
||||
{
|
||||
public object PipelineMaster { get; }
|
||||
|
||||
private event EventHandler<T>? OnEvent;
|
||||
|
||||
public Pipeline(object pipelineMaster)
|
||||
{
|
||||
PipelineMaster = pipelineMaster;
|
||||
}
|
||||
|
||||
public bool IsEventEmpty { get { return OnEvent != null; } }
|
||||
|
||||
public void Subscribe(Delegate handler)
|
||||
{
|
||||
OnEvent += (EventHandler<T>)handler;
|
||||
}
|
||||
|
||||
public void Unsubscribe(Delegate handler)
|
||||
{
|
||||
OnEvent -= (EventHandler<T>)handler;
|
||||
}
|
||||
|
||||
public void SendEvent(object sender, T args)
|
||||
{
|
||||
if (sender == null || args == null) throw new ArgumentNullException(nameof(sender));
|
||||
if (args is not T) throw new ArgumentException($"Invalid EventArgs type. Expected {typeof(T)}");
|
||||
if (sender.Equals(PipelineMaster)) throw new InvalidDataException("Only the pipeline master is allowed to send events through this pipeline.");
|
||||
|
||||
OnEvent?.Invoke(sender, args);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user