Added plugin events
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
using HopFrame.Core.Config;
|
||||
|
||||
namespace HopFrame.Core.Events;
|
||||
namespace HopFrame.Core.Callbacks;
|
||||
|
||||
public static class EventTypes {
|
||||
public static class CallbackTypes {
|
||||
|
||||
private const string Prefix = "HopFrame.";
|
||||
private const string CreateEntryPrefix = Prefix + "Entry.Create.";
|
||||
@@ -13,18 +13,18 @@ public static class EventTypes {
|
||||
public static string UpdateEntry(TableConfig config) => UpdateEntryPrefix + config.PropertyName;
|
||||
public static string DeleteEntry(TableConfig config) => DeleteEntryPrefix + config.PropertyName;
|
||||
|
||||
public static string ConstructEventName(EventType type, TableConfig config) {
|
||||
public static string ConstructCallbackName(CallbackType type, TableConfig config) {
|
||||
return type switch {
|
||||
EventType.CreateEntry => CreateEntry(config),
|
||||
EventType.UpdateEntry => UpdateEntry(config),
|
||||
EventType.DeleteEntry => DeleteEntry(config),
|
||||
CallbackType.CreateEntry => CreateEntry(config),
|
||||
CallbackType.UpdateEntry => UpdateEntry(config),
|
||||
CallbackType.DeleteEntry => DeleteEntry(config),
|
||||
_ => Prefix
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public enum EventType {
|
||||
public enum CallbackType {
|
||||
CreateEntry = 0,
|
||||
UpdateEntry = 1,
|
||||
DeleteEntry = 2
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace HopFrame.Core.Events;
|
||||
namespace HopFrame.Core.Callbacks;
|
||||
|
||||
public readonly struct HopEventHandler(string eventType, Func<object, IServiceProvider, Task> handler) {
|
||||
public readonly struct HopCallbackHandler(string eventType, Func<object, IServiceProvider, Task> handler) {
|
||||
public Guid Id { get; } = Guid.CreateVersion7();
|
||||
public Func<object, IServiceProvider, Task> Handler { get; } = handler;
|
||||
public string EventType { get; } = eventType;
|
||||
14
src/HopFrame.Core/Callbacks/ICallbackEmitter.cs
Normal file
14
src/HopFrame.Core/Callbacks/ICallbackEmitter.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace HopFrame.Core.Callbacks;
|
||||
|
||||
public interface ICallbackEmitter {
|
||||
|
||||
Guid RegisterCallbackHandler(string @event, Func<object, IServiceProvider, Task> handler);
|
||||
|
||||
bool RemoveCallbackHandler(Guid id);
|
||||
|
||||
Task DispatchCallback(string @event, object argument = null!);
|
||||
|
||||
void RemoveAllCallbackHandlers(string @event);
|
||||
void RemoveAllCallbackHandlers();
|
||||
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using HopFrame.Core.Events;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using HopFrame.Core.Events;
|
||||
using HopFrame.Core.Callbacks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
@@ -8,18 +9,20 @@ public class HopFrameConfig {
|
||||
public bool DisplayUserInfo { get; set; } = true;
|
||||
public string? BasePolicy { get; set; }
|
||||
public string? LoginPageRewrite { get; set; }
|
||||
public List<HopEventHandler> Handlers { get; } = new();
|
||||
public List<HopCallbackHandler> Handlers { get; } = new();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A helper class for editing the <see cref="HopFrameConfig"/>
|
||||
/// </summary>
|
||||
public class HopFrameConfigurator(HopFrameConfig config) {
|
||||
public class HopFrameConfigurator(HopFrameConfig config, IServiceCollection collection = null!) {
|
||||
|
||||
/// <summary>
|
||||
/// The Internal HopFrame configuration that's modified by the helper functions
|
||||
/// </summary>
|
||||
public HopFrameConfig InnerConfig { get; } = config;
|
||||
|
||||
public IServiceCollection ServiceCollection { get; } = collection;
|
||||
|
||||
/// <summary>
|
||||
/// Adds all tables defined in the DbContext to the HopFrame ui and configures it using the provided configurator
|
||||
@@ -45,6 +48,18 @@ public class HopFrameConfigurator(HopFrameConfig config) {
|
||||
return new DbContextConfigurator<TDbContext>(context);
|
||||
}
|
||||
|
||||
public bool HasDbContext<TDbContext>() where TDbContext : DbContext {
|
||||
return InnerConfig.Contexts.Any(context => context.ContextType == typeof(TDbContext));
|
||||
}
|
||||
|
||||
public DbContextConfigurator<TDbContext>? GetDbContext<TDbContext>() where TDbContext : DbContext {
|
||||
var config = InnerConfig.Contexts
|
||||
.SingleOrDefault(context => context.ContextType == typeof(TDbContext));
|
||||
if (config is null) return null;
|
||||
|
||||
return new DbContextConfigurator<TDbContext>(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the name of the currently logged-in user should be displayed in the top right corner of the admin ui
|
||||
/// </summary>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using HopFrame.Core.Events;
|
||||
using HopFrame.Core.Callbacks;
|
||||
|
||||
namespace HopFrame.Core.Config;
|
||||
|
||||
@@ -191,26 +191,26 @@ public class TableConfigurator<TModel>(TableConfig config) {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an event handler of the provided type
|
||||
/// Adds a callback handler of the provided type
|
||||
/// </summary>
|
||||
/// <param name="type">The type of event that triggers the handler</param>
|
||||
/// <param name="type">The type of callback that triggers the handler</param>
|
||||
/// <param name="handler">The handler delegate</param>
|
||||
public TableConfigurator<TModel> AddEventHandler(EventType type, Func<TModel, IServiceProvider, Task> handler) {
|
||||
var eventName = EventTypes.ConstructEventName(type, InnerConfig);
|
||||
var handlerStore = new HopEventHandler(eventName, (o, provider) => handler.Invoke((TModel)o, provider));
|
||||
public TableConfigurator<TModel> AddCallbackHandler(CallbackType type, Func<TModel, IServiceProvider, Task> handler) {
|
||||
var eventName = CallbackTypes.ConstructCallbackName(type, InnerConfig);
|
||||
var handlerStore = new HopCallbackHandler(eventName, (o, provider) => handler.Invoke((TModel)o, provider));
|
||||
InnerConfig.ContextConfig.ParentConfig.Handlers.Add(handlerStore);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an event handler of the provided type
|
||||
/// Adds a callback handler of the provided type
|
||||
/// </summary>
|
||||
/// <param name="type">The type of event that triggers the handler</param>
|
||||
/// <param name="type">The type of callback that triggers the handler</param>
|
||||
/// <param name="handler">The handler delegate</param>
|
||||
public TableConfigurator<TModel> AddEventHandler(EventType type, Action<TModel, IServiceProvider> handler) {
|
||||
var eventName = EventTypes.ConstructEventName(type, InnerConfig);
|
||||
var handlerStore = new HopEventHandler(eventName, (o, provider) => {
|
||||
public TableConfigurator<TModel> AddCallbackHandler(CallbackType type, Action<TModel, IServiceProvider> handler) {
|
||||
var eventName = CallbackTypes.ConstructCallbackName(type, InnerConfig);
|
||||
var handlerStore = new HopCallbackHandler(eventName, (o, provider) => {
|
||||
handler.Invoke((TModel)o, provider);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
namespace HopFrame.Core.Events;
|
||||
|
||||
public interface IEventEmitter {
|
||||
|
||||
Guid RegisterEventHandler(string @event, Func<object, IServiceProvider, Task> handler);
|
||||
|
||||
bool RemoveEventHandler(Guid id);
|
||||
|
||||
Task DispatchEvent(string @event, object argument = null!);
|
||||
|
||||
void RemoveAllEventHandlers(string @event);
|
||||
void RemoveAllEventHandlers();
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using HopFrame.Core.Events;
|
||||
using HopFrame.Core.Callbacks;
|
||||
using HopFrame.Core.Services;
|
||||
using HopFrame.Core.Services.Implementations;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -16,7 +16,7 @@ public static class ServiceCollectionExtensions {
|
||||
public static IServiceCollection AddHopFrameServices(this IServiceCollection services) {
|
||||
services.AddScoped<IContextExplorer, ContextExplorer>();
|
||||
services.TryAddScoped<IHopFrameAuthHandler, DefaultAuthHandler>();
|
||||
services.TryAddScoped<IEventEmitter, EventEmitter>();
|
||||
services.TryAddScoped<ICallbackEmitter, CallbackEmitter>();
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
using HopFrame.Core.Config;
|
||||
using HopFrame.Core.Events;
|
||||
using HopFrame.Core.Callbacks;
|
||||
|
||||
namespace HopFrame.Core.Services.Implementations;
|
||||
|
||||
internal sealed class EventEmitter(IServiceProvider provider, HopFrameConfig config) : IEventEmitter {
|
||||
internal sealed class CallbackEmitter(IServiceProvider provider, HopFrameConfig config) : ICallbackEmitter {
|
||||
|
||||
public Guid RegisterEventHandler(string @event, Func<object, IServiceProvider, Task> handler) {
|
||||
var handlerStore = new HopEventHandler(@event, handler);
|
||||
public Guid RegisterCallbackHandler(string @event, Func<object, IServiceProvider, Task> handler) {
|
||||
var handlerStore = new HopCallbackHandler(@event, handler);
|
||||
config.Handlers.Add(handlerStore);
|
||||
return handlerStore.Id;
|
||||
}
|
||||
|
||||
public bool RemoveEventHandler(Guid id) {
|
||||
public bool RemoveCallbackHandler(Guid id) {
|
||||
var count = config.Handlers.RemoveAll(handler => handler.Id == id);
|
||||
return count > 0;
|
||||
}
|
||||
|
||||
public async Task DispatchEvent(string @event, object argument = null!) {
|
||||
public async Task DispatchCallback(string @event, object argument = null!) {
|
||||
var handlers = config.Handlers.Where(handler => handler.EventType == @event);
|
||||
var tasks = new List<Task>();
|
||||
|
||||
@@ -28,11 +28,11 @@ internal sealed class EventEmitter(IServiceProvider provider, HopFrameConfig con
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
public void RemoveAllEventHandlers(string @event) {
|
||||
public void RemoveAllCallbackHandlers(string @event) {
|
||||
config.Handlers.RemoveAll(handler => handler.EventType == @event);
|
||||
}
|
||||
|
||||
public void RemoveAllEventHandlers() {
|
||||
public void RemoveAllCallbackHandlers() {
|
||||
config.Handlers.Clear();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user