using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Firebird2D.EventPipelines { public class EventBus { private Dictionary pipelines = new(); private Dictionary>> pendingHooks = new(); public void OnPipelineExists(Action 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); } } }