88 lines
4.2 KiB
C#
88 lines
4.2 KiB
C#
using Firebird2D.Datatypes;
|
|
using Firebird2D.Interfaces;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Firebird2D.EventPipelines
|
|
{
|
|
/// <summary>
|
|
/// EventBus manages event pipelines and allows registering callbacks (hooks) if a pipeline is not Build Yet, as well as creating, retrieving, and destroying pipelines.
|
|
/// </summary>
|
|
public class EventBus : IEventBus
|
|
{
|
|
// Dictionary that stores various pipelines for specific FirebirdEvents.
|
|
// The key is the event, and the value is the corresponding pipeline.
|
|
private Dictionary<FirebirdEvent, IPipeline> pipelines = new();
|
|
|
|
// Dictionary that stores a list of callbacks for each FirebirdEvent.
|
|
// These callbacks will be invoked when the event's pipeline is build.
|
|
private Dictionary<FirebirdEvent, List<Action<IPipeline>>> pendingHooks = new();
|
|
|
|
/// <summary>
|
|
/// Registers a callback (hook) for a specific event. The callback will be invoked when the pipeline for this event is built.
|
|
/// </summary>
|
|
/// <param name="callback">The function to be called when the pipeline for the event is built.</param>
|
|
/// <param name="type">The event for which the callback should be registered.</param>
|
|
/// <exception cref="ArgumentNullException">Thrown when the callback is null.</exception>
|
|
public void OnPipelineIsBuild(Action<IPipeline> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Attempts to create or retrieve a pipeline for the given event.
|
|
/// If the pipeline already exists, it is returned.
|
|
/// Otherwise, a new pipeline is created and stored.
|
|
/// </summary>
|
|
/// <param name="type">The event for which the pipeline should be created or retrieved.</param>
|
|
/// <param name="argsType">The type of the arguments to be passed to the pipeline.</param>
|
|
/// <param name="master">The master object used when creating the pipeline.</param>
|
|
/// <returns>The pipeline for the given event.</returns>
|
|
public IPipeline GetOrBuildPipeline(FirebirdEvent type, Type argsType, object master)
|
|
{
|
|
if (pipelines.TryGetValue(type, out var pipeline)) return pipeline;
|
|
|
|
var genericPipelineType = typeof(Pipeline<>).MakeGenericType(argsType);
|
|
var pipelineInstance = (IPipeline)Activator.CreateInstance(genericPipelineType, master)!;
|
|
pipelines[type] = pipelineInstance;
|
|
return pipelineInstance;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves the pipeline for an event if it exists; otherwise, returns null.
|
|
/// </summary>
|
|
/// <param name="type">The event for which the pipeline should be retrieved.</param>
|
|
/// <param name="argsType">The type of the arguments for the pipeline.</param>
|
|
/// <returns>The pipeline for the event, or null if it does not exist.</returns>
|
|
public IPipeline? GetPipeline(FirebirdEvent type, Type argsType)
|
|
{
|
|
if (!pipelines.TryGetValue(type, out var pipeline)) return null;
|
|
return pipeline;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Destroys the pipeline for an event. Only the "master" of the pipeline is allowed to destroy it.
|
|
/// </summary>
|
|
/// <param name="type">The event for which the pipeline should be destroyed.</param>
|
|
/// <param name="master">The master object that has permission to destroy the pipeline.</param>
|
|
/// <exception cref="InvalidDataException">Thrown when a non-authorized user attempts to destroy the pipeline.</exception>
|
|
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);
|
|
}
|
|
}
|
|
}
|