Added plugin events
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
namespace HopFrame.Web.Plugins.Annotations;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class EventHandlerAttribute : Attribute;
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace HopFrame.Web.Plugins.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Configures the method as a plugin configurator, so the method gets called, when the plugin is registered.
|
||||
/// Only works on static methods
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class PluginConfiguratorAttribute : Attribute;
|
||||
18
src/HopFrame.Web/Plugins/Events/EntryEvent.cs
Normal file
18
src/HopFrame.Web/Plugins/Events/EntryEvent.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using HopFrame.Web.Components.Pages;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
public sealed class DeleteEntryEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender) {
|
||||
public required object Entity { get; init; }
|
||||
}
|
||||
|
||||
public sealed class CreateEntryEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender);
|
||||
|
||||
public sealed class UpdateEntryEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender) {
|
||||
public required object Entity { get; init; }
|
||||
}
|
||||
|
||||
public sealed class SelectEntryEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender) {
|
||||
public required object Entity { get; init; }
|
||||
public required bool Selected { get; set; }
|
||||
}
|
||||
27
src/HopFrame.Web/Plugins/Events/HopFrameEventArgs.cs
Normal file
27
src/HopFrame.Web/Plugins/Events/HopFrameEventArgs.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using HopFrame.Core.Config;
|
||||
using HopFrame.Web.Components.Dialogs;
|
||||
using HopFrame.Web.Components.Pages;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
public abstract class HopFrameEventArgs(object internalSender) {
|
||||
internal object InternalSender { get; } = internalSender;
|
||||
public bool IsCanceled { get; protected set; }
|
||||
|
||||
|
||||
public void SetCancelled(bool canceled) => IsCanceled = canceled;
|
||||
}
|
||||
|
||||
public abstract class HopFrameEventArgs<TSender>(TSender sender) : HopFrameEventArgs(sender) where TSender : class {
|
||||
public TSender Sender => (TSender)InternalSender;
|
||||
}
|
||||
|
||||
public abstract class HopFrameTablePageEventArgs(HopFrameTablePage sender)
|
||||
: HopFrameEventArgs<HopFrameTablePage>(sender) {
|
||||
public required TableConfig Table { get; init; }
|
||||
}
|
||||
|
||||
public abstract class HopFrameEditorEventArgs(HopFrameEditor sender)
|
||||
: HopFrameEventArgs<HopFrameEditor>(sender) {
|
||||
public required TableConfig Table { get; init; }
|
||||
}
|
||||
9
src/HopFrame.Web/Plugins/Events/PageChangeEvent.cs
Normal file
9
src/HopFrame.Web/Plugins/Events/PageChangeEvent.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using HopFrame.Web.Components.Pages;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
public sealed class PageChangeEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender) {
|
||||
public required int CurrentPage { get; init; }
|
||||
public required int TotalPages { get; init; }
|
||||
public required int NewPage { get; set; }
|
||||
}
|
||||
9
src/HopFrame.Web/Plugins/Events/PluginEventContainer.cs
Normal file
9
src/HopFrame.Web/Plugins/Events/PluginEventContainer.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
internal sealed class PluginEventContainer {
|
||||
public required MethodInfo Handler { get; init; }
|
||||
public required Type EventType { get; init; }
|
||||
public required bool IsAwaitable { get; init; }
|
||||
}
|
||||
7
src/HopFrame.Web/Plugins/Events/ReloadEvent.cs
Normal file
7
src/HopFrame.Web/Plugins/Events/ReloadEvent.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using HopFrame.Web.Components.Pages;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
public sealed class ReloadEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender) {
|
||||
|
||||
}
|
||||
7
src/HopFrame.Web/Plugins/Events/SearchEvent.cs
Normal file
7
src/HopFrame.Web/Plugins/Events/SearchEvent.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using HopFrame.Web.Components.Pages;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
public sealed class SearchEvent(HopFrameTablePage sender) : HopFrameTablePageEventArgs(sender) {
|
||||
public required string SearchTerm { get; set; }
|
||||
}
|
||||
9
src/HopFrame.Web/Plugins/Events/ValidationEvent.cs
Normal file
9
src/HopFrame.Web/Plugins/Events/ValidationEvent.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using HopFrame.Core.Config;
|
||||
using HopFrame.Web.Components.Dialogs;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Events;
|
||||
|
||||
public sealed class ValidationEvent(HopFrameEditor sender) : HopFrameEditorEventArgs(sender) {
|
||||
public required IList<string> Errors { get; init; }
|
||||
public required PropertyConfig Property { get; init; }
|
||||
}
|
||||
7
src/HopFrame.Web/Plugins/HopFramePlugin.cs
Normal file
7
src/HopFrame.Web/Plugins/HopFramePlugin.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace HopFrame.Web.Plugins;
|
||||
|
||||
public abstract class HopFramePlugin {
|
||||
|
||||
|
||||
|
||||
}
|
||||
5
src/HopFrame.Web/Plugins/IPluginOrchestrator.cs
Normal file
5
src/HopFrame.Web/Plugins/IPluginOrchestrator.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace HopFrame.Web.Plugins;
|
||||
|
||||
public interface IPluginOrchestrator {
|
||||
public Task<TEvent> DispatchEvent<TEvent>(TEvent @event, CancellationToken ct = new());
|
||||
}
|
||||
46
src/HopFrame.Web/Plugins/Internal/PluginOrchestrator.cs
Normal file
46
src/HopFrame.Web/Plugins/Internal/PluginOrchestrator.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using HopFrame.Web.Plugins.Annotations;
|
||||
using HopFrame.Web.Plugins.Events;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace HopFrame.Web.Plugins.Internal;
|
||||
|
||||
internal sealed class PluginOrchestrator(IServiceProvider services) : IPluginOrchestrator {
|
||||
|
||||
public static void RegisterPlugin(IServiceCollection collection, Type plugin) {
|
||||
var methods = plugin.GetMethods()
|
||||
.Where(method => method.GetCustomAttributes(true)
|
||||
.Any(attr => attr is EventHandlerAttribute));
|
||||
|
||||
foreach (var method in methods) {
|
||||
var awaitable = method.ReturnType.IsAssignableFrom(typeof(Task));
|
||||
var eventType = method
|
||||
.GetParameters()
|
||||
.FirstOrDefault(param => param.ParameterType.IsAssignableTo(typeof(HopFrameEventArgs)))?.ParameterType;
|
||||
|
||||
if (eventType is null) continue;
|
||||
var container = new PluginEventContainer {
|
||||
EventType = eventType,
|
||||
IsAwaitable = awaitable,
|
||||
Handler = method
|
||||
};
|
||||
collection.AddSingleton(container);
|
||||
collection.AddScoped(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TEvent> DispatchEvent<TEvent>(TEvent @event, CancellationToken ct = new()) {
|
||||
var eventContainers = services.GetRequiredService<IEnumerable<PluginEventContainer>>()
|
||||
.Where(container => container.EventType == typeof(TEvent));
|
||||
|
||||
foreach (var container in eventContainers) {
|
||||
var plugin = services.GetRequiredService(container.Handler.DeclaringType!);
|
||||
var result = container.Handler.Invoke(plugin, [@event]);
|
||||
|
||||
if (container.IsAwaitable)
|
||||
await (Task)result!;
|
||||
}
|
||||
|
||||
return @event;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user