52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Firebird2D.EventPipelines
|
|
{
|
|
public class EventBus
|
|
{
|
|
private Dictionary<FirebirdEvent, I_Pipeline> pipelines = new();
|
|
|
|
private Dictionary<FirebirdEvent, List<Action<I_Pipeline>>> pendingHooks = new();
|
|
|
|
public void OnPipelineExists<T>(Action<I_Pipeline> callback, FirebirdEvent type)
|
|
{
|
|
if (callback == null) throw new ArgumentNullException(nameof(callback));
|
|
|
|
if (!pendingHooks.TryGetValue(type, out var hookList))
|
|
{
|
|
hookList = new();
|
|
pendingHooks[type] = hookList;
|
|
}
|
|
|
|
hookList.Add(callback);
|
|
}
|
|
|
|
public I_Pipeline GetOrBuildPipeline(FirebirdEvent type, Type argsType, object master)
|
|
{
|
|
if (pipelines.TryGetValue(type, out var pipeline)) return pipeline;
|
|
|
|
var genericPipelineType = typeof(Pipeline<>).MakeGenericType(argsType);
|
|
var pipelineInstance = (I_Pipeline)Activator.CreateInstance(genericPipelineType, master)!;
|
|
pipelines[type] = pipelineInstance;
|
|
return pipelineInstance;
|
|
}
|
|
|
|
public I_Pipeline? GetPipeline(FirebirdEvent type, Type argsType)
|
|
{
|
|
if (!pipelines.TryGetValue(type, out var pipeline)) return null;
|
|
return pipeline;
|
|
}
|
|
|
|
public void DestroyPipeline(FirebirdEvent type, object master)
|
|
{
|
|
if (!pipelines.ContainsKey(type)) return;
|
|
if (!pipelines[type].PipelineMaster.Equals(master)) throw new InvalidDataException("Only the pipeline master is allowed to delete this pipeline.");
|
|
pipelines.Remove(type);
|
|
}
|
|
}
|
|
}
|