> context, CancellationToken cancellationToken)
+ // {
+ // Console.WriteLine($"{context.Action.GetType().GetStatorName()}: {context.Action.status}");
+ // await Task.Delay(2000, cancellationToken);
+ // }
+ //}
}
diff --git a/samples/CounterApp/CounterApp/Pages/FetchData.razor b/samples/CounterApp/CounterApp/Pages/FetchData.razor
index bfb84d9..f66ae6b 100644
--- a/samples/CounterApp/CounterApp/Pages/FetchData.razor
+++ b/samples/CounterApp/CounterApp/Pages/FetchData.razor
@@ -1,4 +1,4 @@
-@using StateR.AsyncLogic
+@*@using StateR.AsyncLogic
@using StateR.Blazor.Components
@page "/fetchdata"
@inherits StatorComponent
@@ -10,14 +10,14 @@
This component demonstrates fetching data from the server.
Async Status: @WeatherState.Current.Status
-
+*@
@**@
@**@
-
+@*
Loading...
@@ -60,3 +60,4 @@
+*@
\ No newline at end of file
diff --git a/samples/CounterApp/CounterApp/Program.cs b/samples/CounterApp/CounterApp/Program.cs
index 3065212..0742e60 100644
--- a/samples/CounterApp/CounterApp/Program.cs
+++ b/samples/CounterApp/CounterApp/Program.cs
@@ -1,14 +1,11 @@
using CounterApp;
-using CounterApp.Features;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using StateR;
-using StateR.AfterEffects;
-using StateR.Blazor.Persistance;
-using StateR.Blazor.ReduxDevTools;
+//using StateR.Blazor.Persistance;
+//using StateR.Blazor.ReduxDevTools;
using ForEvolve.Blazor.WebStorage;
-using StateR.Experiments.AsyncLogic;
-using StateR.Interceptors;
+//using StateR.Experiments.AsyncLogic;
using StateR.Validations.FluentValidation;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
@@ -42,12 +39,21 @@ public static void RegisterServices(this IServiceCollection services)
{
var appAssembly = typeof(App).Assembly;
services
- .AddStateR(appAssembly)
- .AddAsyncOperations()
- .AddReduxDevTools()
+ .AddStateR()
+
+ // TODO: scan for types instead
+ .AddState()
+ .AddAction(typeof(CounterApp.Features.Counter.Increment))
+ .AddAction(typeof(CounterApp.Features.Counter.Decrement))
+ .AddAction(typeof(CounterApp.Features.Counter.SetPositive))
+ .AddAction(typeof(CounterApp.Features.Counter.SetNegative))
+ .AddUpdaters(typeof(CounterApp.Features.Counter.Updaters))
+
+ //.AddAsyncOperations()
+ //.AddReduxDevTools()
.AddFluentValidation(appAssembly)
.Apply(buidler => buidler
- .AddPersistence()
+ //.AddPersistence()
.AddStateValidation()
)
;
diff --git a/samples/CounterApp/CounterApp/Shared/FluentValidationSummary.razor b/samples/CounterApp/CounterApp/Shared/FluentValidationSummary.razor
index 3c74c88..3bd1c75 100644
--- a/samples/CounterApp/CounterApp/Shared/FluentValidationSummary.razor
+++ b/samples/CounterApp/CounterApp/Shared/FluentValidationSummary.razor
@@ -1,4 +1,4 @@
-@using FluentValidation.Results
+@*@using FluentValidation.Results
@using StateR.Validations.FluentValidation;
@inherits StatorComponent
@inject IState ValidationState
@@ -29,3 +29,4 @@
await DispatchAsync(new CleanValidationError());
}
}
+*@
\ No newline at end of file
diff --git a/samples/CounterApp/CounterApp/Shared/MainLayout.razor b/samples/CounterApp/CounterApp/Shared/MainLayout.razor
index 8751a37..73eba5d 100644
--- a/samples/CounterApp/CounterApp/Shared/MainLayout.razor
+++ b/samples/CounterApp/CounterApp/Shared/MainLayout.razor
@@ -1,4 +1,4 @@
-@using StateR.Blazor.Components;
+@*@using StateR.Blazor.Components;*@
@inherits LayoutComponentBase
@@ -15,5 +15,5 @@
@Body
-
+ @**@
diff --git a/src/StateR.Blazor.Experiments/ReduxDevTools/ReduxDevToolsInterop.cs b/src/StateR.Blazor.Experiments/ReduxDevTools/ReduxDevToolsInterop.cs
index cf9133d..3e98ce1 100644
--- a/src/StateR.Blazor.Experiments/ReduxDevTools/ReduxDevToolsInterop.cs
+++ b/src/StateR.Blazor.Experiments/ReduxDevTools/ReduxDevToolsInterop.cs
@@ -1,7 +1,6 @@
using Microsoft.JSInterop;
using StateR.Internal;
using StateR.Updaters;
-using StateR.Updaters.Hooks;
using System.Collections;
using System.Reflection;
using System.Text.Json;
diff --git a/src/StateR.Blazor/StatorComponentBase.cs b/src/StateR.Blazor/StatorComponentBase.cs
index b0bcaa4..f90d387 100644
--- a/src/StateR.Blazor/StatorComponentBase.cs
+++ b/src/StateR.Blazor/StatorComponentBase.cs
@@ -10,13 +10,21 @@ public abstract class StatorComponentBase : ComponentBase, IDisposable
[Inject]
public IDispatcher? Dispatcher { get; set; }
- protected virtual async Task DispatchAsync(TAction action, CancellationToken cancellationToken = default)
- where TAction : IAction
+ protected virtual async Task DispatchAsync(object action, CancellationToken cancellationToken = default)
{
GuardAgainstNullDispatcher();
await Dispatcher.DispatchAsync(action, cancellationToken);
}
+
+ protected virtual async Task DispatchAsync(TAction action, CancellationToken cancellationToken = default)
+ where TAction : IAction
+ where TState : StateBase
+ {
+ GuardAgainstNullDispatcher();
+ await Dispatcher.DispatchAsync(action, cancellationToken);
+ }
+
private void Dispose(bool disposing)
{
if (!_disposedValue)
diff --git a/src/StateR.Experiments/AsyncLogic/AsyncError.cs b/src/StateR.Experiments/AsyncLogic/AsyncError.cs
index a160565..5a97a8b 100644
--- a/src/StateR.Experiments/AsyncLogic/AsyncError.cs
+++ b/src/StateR.Experiments/AsyncLogic/AsyncError.cs
@@ -6,7 +6,7 @@ public class AsyncError
{
public record State : StateBase
{
- public IAction? Action { get; init; }
+ public IAction? Action { get; init; }
public AsyncState? InitialState { get; init; }
public AsyncState? ActualState { get; init; }
public Exception? Exception { get; init; }
@@ -21,7 +21,7 @@ public class InitialState : IInitialState
public State Value => new();
}
- public record Occured(IAction Action, AsyncState InitialState, AsyncState ActualState, Exception Exception) : IAction;
+ public record Occured(IAction Action, AsyncState InitialState, AsyncState ActualState, Exception Exception) : IAction;
public class Updaters : IUpdater
{
diff --git a/src/StateR.Experiments/AsyncLogic/StartupExtensions.cs b/src/StateR.Experiments/AsyncLogic/StartupExtensions.cs
index e7fbd92..9496dda 100644
--- a/src/StateR.Experiments/AsyncLogic/StartupExtensions.cs
+++ b/src/StateR.Experiments/AsyncLogic/StartupExtensions.cs
@@ -13,7 +13,7 @@ public static IStatorBuilder AddAsyncOperations(this IStatorBuilder builder)
builder.AddTypes(new[] { typeof(StatusUpdated<>) });
// Async Operation's Errors
- builder.Services.TryAddSingleton, UpdaterActionHandler>();
+ builder.Services.TryAddSingleton, UpdaterMiddleware>();
builder.Services.TryAddSingleton, AsyncError.Updaters>();
builder.Services.TryAddSingleton, AsyncError.InitialState>();
builder.Services.TryAddSingleton, Internal.State>();
diff --git a/src/StateR.Experiments/StateR.Experiments.csproj b/src/StateR.Experiments/StateR.Experiments.csproj
index 95009eb..0cf5587 100644
--- a/src/StateR.Experiments/StateR.Experiments.csproj
+++ b/src/StateR.Experiments/StateR.Experiments.csproj
@@ -5,6 +5,24 @@
StateR
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/StateR.Experiments/Validations/FluentValidation/StartupExtensions.cs b/src/StateR.Experiments/Validations/FluentValidation/StartupExtensions.cs
index 963b5e7..c7994a1 100644
--- a/src/StateR.Experiments/Validations/FluentValidation/StartupExtensions.cs
+++ b/src/StateR.Experiments/Validations/FluentValidation/StartupExtensions.cs
@@ -1,7 +1,7 @@
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
-using StateR.Interceptors;
+using StateR.Pipeline;
using System.Reflection;
namespace StateR.Validations.FluentValidation;
@@ -13,17 +13,16 @@ public static IStatorBuilder AddFluentValidation(this IStatorBuilder builder, pa
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
ArgumentNullException.ThrowIfNull(assembliesToScan, nameof(assembliesToScan));
- // Validation action
- builder.AddTypes(new[] {
- typeof(AddValidationErrors),
- typeof(ReplaceValidationErrors),
- typeof(ValidationUpdaters),
- typeof(ValidationInitialState),
- typeof(ValidationState),
- });
+ // Add state, actions, and updaters
+ builder
+ .AddState()
+ .AddAction(typeof(AddValidationErrors))
+ .AddAction(typeof(ReplaceValidationErrors))
+ .AddUpdaters(typeof(ValidationUpdaters))
+ ;
- // Validation interceptor and state
- builder.Services.TryAddSingleton(typeof(IInterceptor<>), typeof(ValidationInterceptor<>));
+ // Validation interceptor
+ builder.Services.TryAddSingleton(typeof(IActionFilter<,>), typeof(ValidationFilter<,>));
// Scan for validators
builder.Services.AddValidatorsFromAssemblies(assembliesToScan, ServiceLifetime.Singleton);
diff --git a/src/StateR.Experiments/Validations/FluentValidation/StateValidationDecorator.cs b/src/StateR.Experiments/Validations/FluentValidation/StateValidationDecorator.cs
index 91a8a2b..5769a29 100644
--- a/src/StateR.Experiments/Validations/FluentValidation/StateValidationDecorator.cs
+++ b/src/StateR.Experiments/Validations/FluentValidation/StateValidationDecorator.cs
@@ -1,10 +1,7 @@
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
-using StateR.ActionHandlers;
using StateR.Internal;
-using System;
-using System.Reflection;
namespace StateR.Validations.FluentValidation;
@@ -53,53 +50,54 @@ public static class StateValidatorStartupExtensions
{
public static IStatorBuilder AddStateValidation(this IStatorBuilder builder)
{
- RegisterStateDecorator(builder.Services, builder.All);
- ActionHandlerDecorator(builder.Services);
+ //RegisterStateDecorator(builder.Services, builder.All);
+ //ActionHandlerDecorator(builder.Services);
return builder;
}
- private static void ActionHandlerDecorator(IServiceCollection services)
- {
- Console.WriteLine("- Decorate, ValidationExceptionActionHandlersManagerDecorator>()");
- services.Decorate();
- }
- private static void RegisterStateDecorator(IServiceCollection services, IEnumerable types)
- {
- var states = TypeScanner.FindStates(types);
- Console.WriteLine("StateValidator:");
- foreach (var state in states)
- {
- Console.WriteLine($"- Decorate, StateValidationDecorator<{state.GetStatorName()}>>()");
+ //private static void ActionHandlerDecorator(IServiceCollection services)
+ //{
+ // Console.WriteLine("- Decorate, ValidationExceptionActionHandlersManagerDecorator>()");
+ // services.Decorate();
+ //}
- // Equivalent to: Decorate, StateValidationDecorator>();
- var stateType = typeof(IState<>).MakeGenericType(state);
- var stateSessionDecoratorType = typeof(StateValidationDecorator<>).MakeGenericType(state);
- services.Decorate(stateType, stateSessionDecoratorType);
- }
- }
+ //private static void RegisterStateDecorator(IServiceCollection services, IEnumerable types)
+ //{
+ // var states = TypeScanner.FindStates(types);
+ // Console.WriteLine("StateValidator:");
+ // foreach (var state in states)
+ // {
+ // Console.WriteLine($"- Decorate, StateValidationDecorator<{state.GetStatorName()}>>()");
+
+ // // Equivalent to: Decorate, StateValidationDecorator>();
+ // var stateType = typeof(IState<>).MakeGenericType(state);
+ // var stateSessionDecoratorType = typeof(StateValidationDecorator<>).MakeGenericType(state);
+ // services.Decorate(stateType, stateSessionDecoratorType);
+ // }
+ //}
}
-public class ValidationExceptionActionHandlersManagerDecorator : IActionHandlersManager
-{
- private readonly IActionHandlersManager _next;
- public ValidationExceptionActionHandlersManagerDecorator(IActionHandlersManager next)
- {
- _next = next ?? throw new ArgumentNullException(nameof(next));
- }
+//public class ValidationExceptionActionHandlersManagerDecorator : IActionFilter
+//{
+// private readonly IActionHandlersManager _next;
+// public ValidationExceptionActionHandlersManagerDecorator(IActionHandlersManager next)
+// {
+// _next = next ?? throw new ArgumentNullException(nameof(next));
+// }
- public async Task DispatchAsync(IDispatchContext dispatchContext)
- where TAction : IAction
- {
- try
- {
- await _next.DispatchAsync(dispatchContext);
- }
- catch (ValidationException ex)
- {
- await dispatchContext.Dispatcher.DispatchAsync(
- new AddValidationErrors(ex.Errors),
- dispatchContext.CancellationToken
- );
- }
- }
-}
+// public async Task DispatchAsync(IDispatchContext dispatchContext)
+// where TAction : IAction
+// {
+// try
+// {
+// await _next.DispatchAsync(dispatchContext);
+// }
+// catch (ValidationException ex)
+// {
+// await dispatchContext.Dispatcher.DispatchAsync(
+// new AddValidationErrors(ex.Errors),
+// dispatchContext.CancellationToken
+// );
+// }
+// }
+//}
diff --git a/src/StateR.Experiments/Validations/FluentValidation/ValidationInterceptor.cs b/src/StateR.Experiments/Validations/FluentValidation/ValidationFilter.cs
similarity index 68%
rename from src/StateR.Experiments/Validations/FluentValidation/ValidationInterceptor.cs
rename to src/StateR.Experiments/Validations/FluentValidation/ValidationFilter.cs
index 9a140b9..fa14a1c 100644
--- a/src/StateR.Experiments/Validations/FluentValidation/ValidationInterceptor.cs
+++ b/src/StateR.Experiments/Validations/FluentValidation/ValidationFilter.cs
@@ -1,22 +1,25 @@
using FluentValidation;
using FluentValidation.Results;
-using StateR.Interceptors;
+using StateR.Pipeline;
using StateR.Updaters;
using System.Collections.Immutable;
namespace StateR.Validations.FluentValidation;
-public class ValidationInterceptor : IInterceptor
- where TAction : IAction
+public class ValidationFilter : IActionFilter
+ where TAction : IAction
+ where TState : StateBase
{
private readonly IEnumerable> _validators;
- public ValidationInterceptor(IEnumerable> validators)
+ public ValidationFilter(IEnumerable> validators)
{
_validators = validators;
}
- public async Task InterceptAsync(IDispatchContext context, CancellationToken cancellationToken)
+ public async Task InvokeAsync(IDispatchContext context, ActionDelegate? next, CancellationToken cancellationToken)
{
+ ArgumentNullException.ThrowIfNull(next);
+
var result = _validators
.Select(validator => validator.Validate(context.Action));
if (result?.Any(validator => !validator.IsValid) ?? false)
@@ -27,6 +30,18 @@ public async Task InterceptAsync(IDispatchContext context, Cancellation
await context.Dispatcher.DispatchAsync(new AddValidationErrors(errors), cancellationToken);
context.Cancel();
}
+ try
+ {
+ await next(context, cancellationToken);
+ }
+ catch (ValidationException ex)
+ {
+ Console.WriteLine(ex.Message);
+ await context.Dispatcher.DispatchAsync(
+ new AddValidationErrors(ex.Errors),
+ context.CancellationToken
+ );
+ }
}
}
@@ -39,10 +54,10 @@ public class ValidationInitialState : IInitialState
public ValidationState Value => new(ImmutableList.Create());
}
-public record class AddValidationErrors(IEnumerable Errors) : IAction;
-public record class ReplaceValidationErrors(IEnumerable Errors) : IAction;
-public record class CleanValidationError() : IAction;
-public record class RemoveValidationError(ValidationFailure Error) : IAction;
+public record class AddValidationErrors(IEnumerable Errors) : IAction;
+public record class ReplaceValidationErrors(IEnumerable Errors) : IAction;
+public record class CleanValidationError() : IAction;
+public record class RemoveValidationError(ValidationFailure Error) : IAction;
public class ValidationUpdaters :
IUpdater,
diff --git a/src/StateR.Microsoft.Extensions.DependencyInjection/Internal/TypeScanner.cs b/src/StateR.Microsoft.Extensions.DependencyInjection/Internal/TypeScanner.cs
new file mode 100644
index 0000000..7a0801a
--- /dev/null
+++ b/src/StateR.Microsoft.Extensions.DependencyInjection/Internal/TypeScanner.cs
@@ -0,0 +1,59 @@
+using StateR.Pipeline;
+using StateR.Updaters;
+using System.Reflection;
+
+namespace StateR.Internal;
+
+public static class TypeScannerExtensions
+{
+ public static IEnumerable FindStates(this IEnumerable types)
+ {
+ var states = types
+ .Where(type => !type.IsAbstract && type.IsSubclassOf(typeof(StateBase)));
+ return states;
+ }
+
+ public static IEnumerable FindInitialStates(this IEnumerable types)
+ {
+ var initialStates = types
+ .Where(type => !type.IsAbstract && type
+ .GetTypeInfo()
+ .GetInterfaces()
+ .Any(i => i == typeof(IInitialState<>))
+ );
+ return initialStates;
+ }
+
+ public static IEnumerable FindActions(this IEnumerable types)
+ {
+ var actions = types
+ .Where(type => !type.IsAbstract && type
+ .GetTypeInfo()
+ .GetInterfaces()
+ .Any(i => i == typeof(IAction<>))
+ );
+ return actions;
+ }
+
+ public static IEnumerable FindUpdaters(this IEnumerable types)
+ {
+ var updaters = types
+ .Where(type => !type.IsAbstract && type
+ .GetTypeInfo()
+ .GetInterfaces()
+ .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IUpdater<,>))
+ );
+ return updaters;
+ }
+
+ public static IEnumerable FindActionFilters(this IEnumerable types)
+ {
+ var handlers = types
+ .Where(type => !type.IsAbstract && type
+ .GetTypeInfo()
+ .GetInterfaces()
+ .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IActionFilter<,>))
+ );
+ return handlers;
+ }
+}
\ No newline at end of file
diff --git a/src/StateR.Microsoft.Extensions.DependencyInjection/StateR.Microsoft.Extensions.DependencyInjection.csproj b/src/StateR.Microsoft.Extensions.DependencyInjection/StateR.Microsoft.Extensions.DependencyInjection.csproj
new file mode 100644
index 0000000..b289158
--- /dev/null
+++ b/src/StateR.Microsoft.Extensions.DependencyInjection/StateR.Microsoft.Extensions.DependencyInjection.csproj
@@ -0,0 +1,17 @@
+
+
+
+ net6.0
+ enable
+ enable
+ StateR
+
+
+
+
+
+
+
+
+
+
diff --git a/src/StateR.Microsoft.Extensions.DependencyInjection/StatorStartupExtensions.cs b/src/StateR.Microsoft.Extensions.DependencyInjection/StatorStartupExtensions.cs
new file mode 100644
index 0000000..03a8b60
--- /dev/null
+++ b/src/StateR.Microsoft.Extensions.DependencyInjection/StatorStartupExtensions.cs
@@ -0,0 +1,104 @@
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyInjection.Extensions;
+using StateR.Internal;
+using StateR.Pipeline;
+using StateR.Updaters;
+using System.Reflection;
+
+namespace StateR;
+
+public static class StatorStartupExtensions
+{
+ public static IStatorBuilder AddStateR(this IServiceCollection services)
+ {
+ services.TryAddSingleton();
+ services.TryAddSingleton();
+ services.TryAddSingleton();
+ services.TryAddSingleton();
+
+ return new StatorBuilder(services);
+ }
+
+ public static IStatorBuilder ScanAndAddStates(this IStatorBuilder builder, params Assembly[] assembliesToScan)
+ {
+ var initialStates = assembliesToScan
+ .SelectMany(a => a.GetTypes())
+ .FindInitialStates()
+ ;
+
+ foreach (var initialState in initialStates)
+ {
+ var state = initialState.GenericTypeArguments[0];
+ builder.AddState(state, initialState);
+ }
+
+ return builder;
+ }
+
+ public static IServiceCollection Apply(this IStatorBuilder builder, Action? postConfiguration = null)
+ {
+ // Register States
+ foreach (var state in builder.States)
+ {
+ Console.WriteLine($"state: {state.FullName}");
+
+ // Equivalent to: AddSingleton, State>();
+ var stateServiceType = typeof(IState<>).MakeGenericType(state);
+ var stateImplementationType = typeof(State<>).MakeGenericType(state);
+ builder.Services.AddSingleton(stateServiceType, stateImplementationType);
+ }
+
+ // Register Initial States
+ builder.Services.Scan(s => s
+ .AddTypes(builder.InitialStates)
+
+ // Equivalent to: AddSingleton, Implementation>();
+ .AddClasses(classes => classes.AssignableTo(typeof(IInitialState<>)))
+ .AsImplementedInterfaces()
+ .WithSingletonLifetime()
+ );
+
+ // Register Updaters and their respective IActionFilter
+ var iUpdaterType = typeof(IUpdater<,>);
+ var updaterHandler = typeof(UpdaterMiddleware<,>);
+ var handlerType = typeof(IActionFilter<,>);
+ foreach (var updater in builder.Updaters)
+ {
+ Console.WriteLine($"updater: {updater.FullName}");
+ var interfaces = updater.GetInterfaces()
+ .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == iUpdaterType);
+ foreach (var @interface in interfaces)
+ {
+ // Equivalent to: AddSingleton, UpdaterMiddleware>
+ var actionType = @interface.GenericTypeArguments[0];
+ var stateType = @interface.GenericTypeArguments[1];
+ var iMiddlewareServiceType = handlerType.MakeGenericType(actionType, stateType);
+ var updaterMiddlewareImplementationType = updaterHandler.MakeGenericType(stateType, actionType);
+ builder.Services.AddSingleton(iMiddlewareServiceType, updaterMiddlewareImplementationType);
+
+ // Equivalent to: AddSingleton, Updater>();
+ builder.Services.AddSingleton(@interface, updater);
+
+ Console.WriteLine($"- AddSingleton<{iMiddlewareServiceType.GetStatorName()}, {updaterMiddlewareImplementationType.GetStatorName()}>()");
+ Console.WriteLine($"- AddSingleton<{@interface.GetStatorName()}, {updater.GetStatorName()}>()");
+ }
+ }
+
+ var iActionFilterType = typeof(IActionFilter<,>);
+ foreach (var filter in builder.ActionFilters)
+ {
+ var interfaces = filter.GetInterfaces()
+ .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == iActionFilterType);
+ foreach (var @interface in interfaces)
+ {
+ var actionType = @interface.GenericTypeArguments[0];
+ var stateType = @interface.GenericTypeArguments[1];
+ var filterType = iActionFilterType.MakeGenericType(actionType, stateType);
+
+ builder.Services.AddSingleton(@interface, filter);
+ }
+ }
+
+ return builder.Services;
+ }
+}
diff --git a/src/StateR/ActionHandlers/ActionHandlersManager.cs b/src/StateR/ActionHandlers/ActionHandlersManager.cs
deleted file mode 100644
index df9c673..0000000
--- a/src/StateR/ActionHandlers/ActionHandlersManager.cs
+++ /dev/null
@@ -1,29 +0,0 @@
-using Microsoft.Extensions.DependencyInjection;
-using StateR.ActionHandlers.Hooks;
-
-namespace StateR.ActionHandlers;
-
-public class ActionHandlersManager : IActionHandlersManager
-{
- private readonly IActionHandlerHooksCollection _hooksCollection;
- private readonly IServiceProvider _serviceProvider;
-
- public ActionHandlersManager(IActionHandlerHooksCollection hooksCollection, IServiceProvider serviceProvider)
- {
- _hooksCollection = hooksCollection ?? throw new ArgumentNullException(nameof(hooksCollection));
- _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
- }
-
- public async Task DispatchAsync(IDispatchContext dispatchContext) where TAction : IAction
- {
- var updaterHandlers = _serviceProvider.GetServices>().ToList();
- foreach (var handler in updaterHandlers)
- {
- dispatchContext.CancellationToken.ThrowIfCancellationRequested();
-
- await _hooksCollection.BeforeHandlerAsync(dispatchContext, handler, dispatchContext.CancellationToken);
- await handler.HandleAsync(dispatchContext, dispatchContext.CancellationToken);
- await _hooksCollection.AfterHandlerAsync(dispatchContext, handler, dispatchContext.CancellationToken);
- }
- }
-}
diff --git a/src/StateR/ActionHandlers/Hooks/ActionHandlerHooksCollection.cs b/src/StateR/ActionHandlers/Hooks/ActionHandlerHooksCollection.cs
deleted file mode 100644
index f2d8eab..0000000
--- a/src/StateR/ActionHandlers/Hooks/ActionHandlerHooksCollection.cs
+++ /dev/null
@@ -1,28 +0,0 @@
-namespace StateR.ActionHandlers.Hooks;
-
-public class ActionHandlerHooksCollection : IActionHandlerHooksCollection
-{
- private readonly IEnumerable _beforeActionHooks;
- private readonly IEnumerable _afterActionHooks;
- public ActionHandlerHooksCollection(IEnumerable beforeActionHooks, IEnumerable afterActionHooks)
- {
- _beforeActionHooks = beforeActionHooks ?? throw new ArgumentNullException(nameof(beforeActionHooks));
- _afterActionHooks = afterActionHooks ?? throw new ArgumentNullException(nameof(afterActionHooks));
- }
-
- public async Task BeforeHandlerAsync(IDispatchContext context, IActionHandler actionHandler, CancellationToken cancellationToken) where TAction : IAction
- {
- foreach (var hook in _beforeActionHooks)
- {
- await hook.BeforeHandlerAsync(context, actionHandler, cancellationToken);
- }
- }
-
- public async Task AfterHandlerAsync(IDispatchContext context, IActionHandler actionHandler, CancellationToken cancellationToken) where TAction : IAction
- {
- foreach (var hook in _afterActionHooks)
- {
- await hook.AfterHandlerAsync(context, actionHandler, cancellationToken);
- }
- }
-}
diff --git a/src/StateR/ActionHandlers/Hooks/IActionHandlerHooksCollection.cs b/src/StateR/ActionHandlers/Hooks/IActionHandlerHooksCollection.cs
deleted file mode 100644
index 2964422..0000000
--- a/src/StateR/ActionHandlers/Hooks/IActionHandlerHooksCollection.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-namespace StateR.ActionHandlers.Hooks;
-
-public interface IActionHandlerHooksCollection
-{
- Task BeforeHandlerAsync(IDispatchContext context, IActionHandler actionHandler, CancellationToken cancellationToken) where TAction : IAction;
- Task AfterHandlerAsync(IDispatchContext context, IActionHandler actionHandler, CancellationToken cancellationToken) where TAction : IAction;
-}
diff --git a/src/StateR/ActionHandlers/Hooks/IAfterActionHook.cs b/src/StateR/ActionHandlers/Hooks/IAfterActionHook.cs
deleted file mode 100644
index b1ecd82..0000000
--- a/src/StateR/ActionHandlers/Hooks/IAfterActionHook.cs
+++ /dev/null
@@ -1,6 +0,0 @@
-namespace StateR.ActionHandlers.Hooks;
-
-public interface IAfterActionHook
-{
- Task AfterHandlerAsync(IDispatchContext context, IActionHandler actionHandler, CancellationToken cancellationToken) where TAction : IAction;
-}
diff --git a/src/StateR/ActionHandlers/Hooks/IBeforeActionHook.cs b/src/StateR/ActionHandlers/Hooks/IBeforeActionHook.cs
deleted file mode 100644
index c69f7c8..0000000
--- a/src/StateR/ActionHandlers/Hooks/IBeforeActionHook.cs
+++ /dev/null
@@ -1,6 +0,0 @@
-namespace StateR.ActionHandlers.Hooks;
-
-public interface IBeforeActionHook
-{
- Task BeforeHandlerAsync(IDispatchContext context, IActionHandler actionHandler, CancellationToken cancellationToken) where TAction : IAction;
-}
diff --git a/src/StateR/ActionHandlers/IActionHandler.cs b/src/StateR/ActionHandlers/IActionHandler.cs
deleted file mode 100644
index 4f7747f..0000000
--- a/src/StateR/ActionHandlers/IActionHandler.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-namespace StateR.ActionHandlers;
-
-public interface IActionHandler
- where TAction : IAction
-{
- Task HandleAsync(IDispatchContext context, CancellationToken cancellationToken);
-}
diff --git a/src/StateR/ActionHandlers/IActionHandlersManager.cs b/src/StateR/ActionHandlers/IActionHandlersManager.cs
deleted file mode 100644
index aa0a671..0000000
--- a/src/StateR/ActionHandlers/IActionHandlersManager.cs
+++ /dev/null
@@ -1,3 +0,0 @@
-namespace StateR.ActionHandlers;
-
-public interface IActionHandlersManager : IDispatchManager { }
diff --git a/src/StateR/AfterEffects/AfterEffectsManager.cs b/src/StateR/AfterEffects/AfterEffectsManager.cs
deleted file mode 100644
index 72cdb58..0000000
--- a/src/StateR/AfterEffects/AfterEffectsManager.cs
+++ /dev/null
@@ -1,29 +0,0 @@
-using Microsoft.Extensions.DependencyInjection;
-using StateR.AfterEffects.Hooks;
-
-namespace StateR.AfterEffects;
-
-public class AfterEffectsManager : IAfterEffectsManager
-{
- private readonly IAfterEffectHooksCollection _hooks;
- private readonly IServiceProvider _serviceProvider;
-
- public AfterEffectsManager(IAfterEffectHooksCollection hooks, IServiceProvider serviceProvider)
- {
- _hooks = hooks ?? throw new ArgumentNullException(nameof(hooks));
- _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
- }
-
- public async Task DispatchAsync(IDispatchContext dispatchContext) where TAction : IAction
- {
- var afterEffects = _serviceProvider.GetServices>().ToList();
- foreach (var afterEffect in afterEffects)
- {
- dispatchContext.CancellationToken.ThrowIfCancellationRequested();
-
- await _hooks.BeforeHandlerAsync(dispatchContext, afterEffect, dispatchContext.CancellationToken);
- await afterEffect.HandleAfterEffectAsync(dispatchContext, dispatchContext.CancellationToken);
- await _hooks.AfterHandlerAsync(dispatchContext, afterEffect, dispatchContext.CancellationToken);
- }
- }
-}
diff --git a/src/StateR/AfterEffects/Hooks/AfterEffectHooksCollection.cs b/src/StateR/AfterEffects/Hooks/AfterEffectHooksCollection.cs
deleted file mode 100644
index 1e6a603..0000000
--- a/src/StateR/AfterEffects/Hooks/AfterEffectHooksCollection.cs
+++ /dev/null
@@ -1,29 +0,0 @@
-namespace StateR.AfterEffects.Hooks;
-
-public class AfterEffectHooksCollection : IAfterEffectHooksCollection
-{
- private readonly IEnumerable _beforeAfterEffectHooks;
- private readonly IEnumerable _afterAfterEffectHooks;
- public AfterEffectHooksCollection(IEnumerable beforeAfterEffectHooks, IEnumerable afterAfterEffectHooks)
- {
- _beforeAfterEffectHooks = beforeAfterEffectHooks ?? throw new ArgumentNullException(nameof(beforeAfterEffectHooks));
- _afterAfterEffectHooks = afterAfterEffectHooks ?? throw new ArgumentNullException(nameof(afterAfterEffectHooks));
- }
-
- public async Task BeforeHandlerAsync(IDispatchContext context, IAfterEffects afterEffect, CancellationToken cancellationToken) where TAction : IAction
- {
- foreach (var hook in _beforeAfterEffectHooks)
- {
- await hook.BeforeHandlerAsync(context, afterEffect, cancellationToken);
- }
- }
-
- public async Task AfterHandlerAsync(IDispatchContext context, IAfterEffects afterEffect, CancellationToken cancellationToken) where TAction : IAction
- {
- foreach (var hook in _afterAfterEffectHooks)
- {
- await hook.AfterHandlerAsync(context, afterEffect, cancellationToken);
- }
- }
-}
-
diff --git a/src/StateR/AfterEffects/Hooks/IAfterAfterEffectHook.cs b/src/StateR/AfterEffects/Hooks/IAfterAfterEffectHook.cs
deleted file mode 100644
index 8b8d888..0000000
--- a/src/StateR/AfterEffects/Hooks/IAfterAfterEffectHook.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-namespace StateR.AfterEffects.Hooks;
-
-public interface IAfterAfterEffectHook
-{
- Task AfterHandlerAsync(IDispatchContext context, IAfterEffects afterEffect, CancellationToken cancellationToken) where TAction : IAction;
-}
-
diff --git a/src/StateR/AfterEffects/Hooks/IAfterEffectHooksCollection.cs b/src/StateR/AfterEffects/Hooks/IAfterEffectHooksCollection.cs
deleted file mode 100644
index d0e4ce9..0000000
--- a/src/StateR/AfterEffects/Hooks/IAfterEffectHooksCollection.cs
+++ /dev/null
@@ -1,8 +0,0 @@
-namespace StateR.AfterEffects.Hooks;
-
-public interface IAfterEffectHooksCollection
-{
- Task BeforeHandlerAsync(IDispatchContext context, IAfterEffects afterEffect, CancellationToken cancellationToken) where TAction : IAction;
- Task AfterHandlerAsync(IDispatchContext context, IAfterEffects afterEffect, CancellationToken cancellationToken) where TAction : IAction;
-}
-
diff --git a/src/StateR/AfterEffects/Hooks/IBeforeAfterEffectHook.cs b/src/StateR/AfterEffects/Hooks/IBeforeAfterEffectHook.cs
deleted file mode 100644
index 7a473b7..0000000
--- a/src/StateR/AfterEffects/Hooks/IBeforeAfterEffectHook.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-namespace StateR.AfterEffects.Hooks;
-
-public interface IBeforeAfterEffectHook
-{
- Task BeforeHandlerAsync(IDispatchContext context, IAfterEffects afterEffect, CancellationToken cancellationToken) where TAction : IAction;
-}
-
diff --git a/src/StateR/AfterEffects/IAfterEffects.cs b/src/StateR/AfterEffects/IAfterEffects.cs
deleted file mode 100644
index b8761a2..0000000
--- a/src/StateR/AfterEffects/IAfterEffects.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-namespace StateR.AfterEffects;
-
-public interface IAfterEffects
- where TAction : IAction
-{
- Task HandleAfterEffectAsync(IDispatchContext context, CancellationToken cancellationToken);
-}
diff --git a/src/StateR/AfterEffects/IAfterEffectsManager.cs b/src/StateR/AfterEffects/IAfterEffectsManager.cs
deleted file mode 100644
index 20aa657..0000000
--- a/src/StateR/AfterEffects/IAfterEffectsManager.cs
+++ /dev/null
@@ -1,3 +0,0 @@
-namespace StateR.AfterEffects;
-
-public interface IAfterEffectsManager : IDispatchManager { }
diff --git a/src/StateR/DispatchContext.cs b/src/StateR/DispatchContext.cs
index 8f7ea55..085ca0b 100644
--- a/src/StateR/DispatchContext.cs
+++ b/src/StateR/DispatchContext.cs
@@ -1,7 +1,8 @@
namespace StateR;
-public class DispatchContext : IDispatchContext
- where TAction : IAction
+public class DispatchContext : IDispatchContext
+ where TAction : IAction
+ where TState : StateBase
{
private readonly CancellationTokenSource _cancellationTokenSource;
public DispatchContext(TAction action, IDispatcher dispatcher, CancellationTokenSource cancellationTokenSource)
@@ -16,12 +17,11 @@ public DispatchContext(TAction action, IDispatcher dispatcher, CancellationToken
public CancellationToken CancellationToken => _cancellationTokenSource.Token;
public void Cancel()
- => throw new DispatchCancelledException(Action);
- //=> _cancellationTokenSource.Cancel(true);
+ => throw new DispatchCancelledException(Action.GetType());
}
public class DispatchCancelledException : Exception
{
- public DispatchCancelledException(IAction action)
- : base($"The dispatch operation '{action.GetType().FullName}' has been cancelled.") { }
+ public DispatchCancelledException(Type actionType)
+ : base($"The dispatch operation '{actionType.FullName}' has been cancelled.") { }
}
\ No newline at end of file
diff --git a/src/StateR/DispatchContextFactory.cs b/src/StateR/DispatchContextFactory.cs
index 4238ffc..9ba0810 100644
--- a/src/StateR/DispatchContextFactory.cs
+++ b/src/StateR/DispatchContextFactory.cs
@@ -2,7 +2,8 @@
public class DispatchContextFactory : IDispatchContextFactory
{
- public IDispatchContext Create(TAction action, IDispatcher dispatcher, CancellationTokenSource cancellationTokenSource)
- where TAction : IAction
- => new DispatchContext(action, dispatcher, cancellationTokenSource);
+ public IDispatchContext Create(TAction action, IDispatcher dispatcher, CancellationTokenSource cancellationTokenSource)
+ where TAction : IAction
+ where TState : StateBase
+ => new DispatchContext(action, dispatcher, cancellationTokenSource);
}
diff --git a/src/StateR/Dispatcher.cs b/src/StateR/Dispatcher.cs
index 607c2fb..2d3836b 100644
--- a/src/StateR/Dispatcher.cs
+++ b/src/StateR/Dispatcher.cs
@@ -1,44 +1,60 @@
using Microsoft.Extensions.Logging;
-using StateR.ActionHandlers;
-using StateR.AfterEffects;
-using StateR.Interceptors;
+using StateR.Pipeline;
using System;
namespace StateR;
public class Dispatcher : IDispatcher
{
- private readonly IInterceptorsManager _interceptorsManager;
- private readonly IActionHandlersManager _actionHandlersManager;
- private readonly IAfterEffectsManager _afterEffectsManager;
private readonly IDispatchContextFactory _dispatchContextFactory;
+ private readonly IPipelineFactory _pipelineFactory;
private readonly ILogger _logger;
- public Dispatcher(IDispatchContextFactory dispatchContextFactory, IInterceptorsManager interceptorsManager, IActionHandlersManager actionHandlersManager, IAfterEffectsManager afterEffectsManager, ILogger logger)
+ public Dispatcher(IDispatchContextFactory dispatchContextFactory, IPipelineFactory actionFilterFactory, ILogger logger)
{
_dispatchContextFactory = dispatchContextFactory ?? throw new ArgumentNullException(nameof(dispatchContextFactory));
- _interceptorsManager = interceptorsManager ?? throw new ArgumentNullException(nameof(interceptorsManager));
- _actionHandlersManager = actionHandlersManager ?? throw new ArgumentNullException(nameof(actionHandlersManager));
- _afterEffectsManager = afterEffectsManager ?? throw new ArgumentNullException(nameof(afterEffectsManager));
+ _pipelineFactory = actionFilterFactory ?? throw new ArgumentNullException(nameof(actionFilterFactory));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
- public async Task DispatchAsync(TAction action, CancellationToken cancellationToken) where TAction : IAction
+ public async Task DispatchAsync(TAction action, CancellationToken cancellationToken)
+ where TAction : IAction
+ where TState : StateBase
{
using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
- var dispatchContext = _dispatchContextFactory.Create(action, this, cancellationTokenSource);
- //
- // TODO: design how to handle OperationCanceledException
- //
+ var dispatchContext = _dispatchContextFactory.Create(action, this, cancellationTokenSource);
+ var pipeline = _pipelineFactory.Create(dispatchContext);
try
{
- await _interceptorsManager.DispatchAsync(dispatchContext);
- await _actionHandlersManager.DispatchAsync(dispatchContext);
- await _afterEffectsManager.DispatchAsync(dispatchContext);
+ await pipeline.Invoke(dispatchContext, cancellationToken).ConfigureAwait(false);
}
catch (DispatchCancelledException ex)
{
_logger.LogWarning(ex, ex.Message);
}
}
+
+ public Task DispatchAsync(object action, CancellationToken cancellationToken)
+ {
+ var actionType = action
+ .GetType();
+ var actionInterface = actionType.GetInterfaces()
+ .FirstOrDefault(@interface => @interface.IsGenericType && @interface.GetGenericTypeDefinition() == typeof(IAction<>));
+ if (actionInterface == null)
+ {
+ // TODO: Find a better exception
+ throw new InvalidOperationException($"The action must implement the {typeof(IAction<>).Name} interface.");
+ }
+ var stateType = actionInterface.GetGenericArguments()[0];
+ var method = GetType()
+ .GetMethods()
+ .FirstOrDefault(m => m.IsGenericMethod && m.Name == nameof(DispatchAsync));
+ if(method == null)
+ {
+ throw new MissingMethodException(nameof(Dispatcher), nameof(DispatchAsync));
+ }
+ var genericMethod = method.MakeGenericMethod(actionType, stateType);
+ var task = genericMethod.Invoke(this, new[] { action, cancellationToken });
+ return (Task)task!;
+ }
}
diff --git a/src/StateR/IAction.cs b/src/StateR/IAction.cs
index 648e6b5..c4138a9 100644
--- a/src/StateR/IAction.cs
+++ b/src/StateR/IAction.cs
@@ -1,3 +1,3 @@
namespace StateR;
-public interface IAction { }
+public interface IAction where TState : StateBase { }
diff --git a/src/StateR/IDispatchContext.cs b/src/StateR/IDispatchContext.cs
index 0f96e31..bfe5295 100644
--- a/src/StateR/IDispatchContext.cs
+++ b/src/StateR/IDispatchContext.cs
@@ -1,7 +1,8 @@
namespace StateR;
-public interface IDispatchContext
- where TAction : IAction
+public interface IDispatchContext
+ where TAction : IAction
+ where TState : StateBase
{
IDispatcher Dispatcher { get; }
TAction Action { get; }
diff --git a/src/StateR/IDispatchContextFactory.cs b/src/StateR/IDispatchContextFactory.cs
index 6613960..1abb378 100644
--- a/src/StateR/IDispatchContextFactory.cs
+++ b/src/StateR/IDispatchContextFactory.cs
@@ -2,5 +2,7 @@
public interface IDispatchContextFactory
{
- IDispatchContext Create(TAction action, IDispatcher dispatcher, CancellationTokenSource cancellationTokenSource) where TAction : IAction;
+ IDispatchContext Create(TAction action, IDispatcher dispatcher, CancellationTokenSource cancellationTokenSource)
+ where TAction : IAction
+ where TState : StateBase;
}
diff --git a/src/StateR/IDispatchManager.cs b/src/StateR/IDispatchManager.cs
deleted file mode 100644
index 3cdc889..0000000
--- a/src/StateR/IDispatchManager.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-namespace StateR;
-
-public interface IDispatchManager
-{
- Task DispatchAsync(IDispatchContext dispatchContext)
- where TAction : IAction;
-}
diff --git a/src/StateR/IDispatcher.cs b/src/StateR/IDispatcher.cs
index 6ef0bf0..510ccf1 100644
--- a/src/StateR/IDispatcher.cs
+++ b/src/StateR/IDispatcher.cs
@@ -2,5 +2,8 @@
public interface IDispatcher
{
- Task DispatchAsync(TAction action, CancellationToken cancellationToken) where TAction : IAction;
+ Task DispatchAsync(object action, CancellationToken cancellationToken);
+ Task DispatchAsync(TAction action, CancellationToken cancellationToken)
+ where TAction : IAction
+ where TState : StateBase;
}
diff --git a/src/StateR/IStatorBuilder.cs b/src/StateR/IStatorBuilder.cs
index 2a2bd3d..25beaf9 100644
--- a/src/StateR/IStatorBuilder.cs
+++ b/src/StateR/IStatorBuilder.cs
@@ -1,21 +1,23 @@
using Microsoft.Extensions.DependencyInjection;
+using System.Collections.ObjectModel;
namespace StateR;
public interface IStatorBuilder
{
IServiceCollection Services { get; }
- List Actions { get; }
- List States { get; }
- //List Interceptors { get; }
- List ActionHandlers { get; }
- //List AfterEffects { get; }
- List Updaters { get; }
- List All { get; }
+ ReadOnlyCollection States { get; }
+ ReadOnlyCollection InitialStates { get; }
+ ReadOnlyCollection Actions { get; }
+ ReadOnlyCollection Updaters { get; }
+ ReadOnlyCollection ActionFilters { get; }
- IStatorBuilder AddTypes(IEnumerable types);
- IStatorBuilder AddStates(IEnumerable states);
- IStatorBuilder AddActions(IEnumerable states);
- IStatorBuilder AddUpdaters(IEnumerable states);
- IStatorBuilder AddActionHandlers(IEnumerable types);
+ IStatorBuilder AddState()
+ where TState : StateBase
+ where TInitialState : IInitialState;
+ IStatorBuilder AddState(Type state, Type initialState);
+
+ IStatorBuilder AddAction(Type actionType);
+ IStatorBuilder AddUpdaters(Type updaterType);
+ IStatorBuilder AddActionFilter(Type actionFilterType);
}
diff --git a/src/StateR/Interceptors/Hooks/IAfterInterceptorHook.cs b/src/StateR/Interceptors/Hooks/IAfterInterceptorHook.cs
deleted file mode 100644
index d57c3b8..0000000
--- a/src/StateR/Interceptors/Hooks/IAfterInterceptorHook.cs
+++ /dev/null
@@ -1,6 +0,0 @@
-namespace StateR.Interceptors.Hooks;
-
-public interface IAfterInterceptorHook
-{
- Task AfterHandlerAsync(IDispatchContext context, IInterceptor interceptor, CancellationToken cancellationToken) where TAction : IAction;
-}
diff --git a/src/StateR/Interceptors/Hooks/IBeforeInterceptorHook.cs b/src/StateR/Interceptors/Hooks/IBeforeInterceptorHook.cs
deleted file mode 100644
index 218ab38..0000000
--- a/src/StateR/Interceptors/Hooks/IBeforeInterceptorHook.cs
+++ /dev/null
@@ -1,6 +0,0 @@
-namespace StateR.Interceptors.Hooks;
-
-public interface IBeforeInterceptorHook
-{
- Task BeforeHandlerAsync(IDispatchContext context, IInterceptor interceptor, CancellationToken cancellationToken) where TAction : IAction;
-}
diff --git a/src/StateR/Interceptors/Hooks/IInterceptorsHooksCollection.cs b/src/StateR/Interceptors/Hooks/IInterceptorsHooksCollection.cs
deleted file mode 100644
index fa4b8f3..0000000
--- a/src/StateR/Interceptors/Hooks/IInterceptorsHooksCollection.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-namespace StateR.Interceptors.Hooks;
-
-public interface IInterceptorsHooksCollection
-{
- Task BeforeHandlerAsync(IDispatchContext context, IInterceptor interceptor, CancellationToken cancellationToken) where TAction : IAction;
- Task AfterHandlerAsync(IDispatchContext context, IInterceptor interceptor, CancellationToken cancellationToken) where TAction : IAction;
-}
diff --git a/src/StateR/Interceptors/Hooks/InterceptorsHooksCollection.cs b/src/StateR/Interceptors/Hooks/InterceptorsHooksCollection.cs
deleted file mode 100644
index ed254bc..0000000
--- a/src/StateR/Interceptors/Hooks/InterceptorsHooksCollection.cs
+++ /dev/null
@@ -1,28 +0,0 @@
-namespace StateR.Interceptors.Hooks;
-
-public class InterceptorsHooksCollection : IInterceptorsHooksCollection
-{
- private readonly IEnumerable _beforeInterceptorHooks;
- private readonly IEnumerable _afterInterceptorHooks;
- public InterceptorsHooksCollection(IEnumerable beforeInterceptorHooks, IEnumerable afterInterceptorHooks)
- {
- _beforeInterceptorHooks = beforeInterceptorHooks ?? throw new ArgumentNullException(nameof(beforeInterceptorHooks));
- _afterInterceptorHooks = afterInterceptorHooks ?? throw new ArgumentNullException(nameof(afterInterceptorHooks));
- }
-
- public async Task BeforeHandlerAsync(IDispatchContext context, IInterceptor interceptor, CancellationToken cancellationToken) where TAction : IAction
- {
- foreach (var hook in _beforeInterceptorHooks)
- {
- await hook.BeforeHandlerAsync(context, interceptor, cancellationToken);
- }
- }
-
- public async Task AfterHandlerAsync(IDispatchContext context, IInterceptor interceptor, CancellationToken cancellationToken) where TAction : IAction
- {
- foreach (var hook in _afterInterceptorHooks)
- {
- await hook.AfterHandlerAsync(context, interceptor, cancellationToken);
- }
- }
-}
diff --git a/src/StateR/Interceptors/IInterceptor.cs b/src/StateR/Interceptors/IInterceptor.cs
deleted file mode 100644
index 0ca7b9d..0000000
--- a/src/StateR/Interceptors/IInterceptor.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-namespace StateR.Interceptors;
-
-public interface IInterceptor
- where TAction : IAction
-{
- Task InterceptAsync(IDispatchContext context, CancellationToken cancellationToken);
-}
diff --git a/src/StateR/Interceptors/IInterceptorsManager.cs b/src/StateR/Interceptors/IInterceptorsManager.cs
deleted file mode 100644
index 23f6853..0000000
--- a/src/StateR/Interceptors/IInterceptorsManager.cs
+++ /dev/null
@@ -1,3 +0,0 @@
-namespace StateR.Interceptors;
-
-public interface IInterceptorsManager : IDispatchManager { }
diff --git a/src/StateR/Interceptors/InterceptorsManager.cs b/src/StateR/Interceptors/InterceptorsManager.cs
deleted file mode 100644
index d23c6ea..0000000
--- a/src/StateR/Interceptors/InterceptorsManager.cs
+++ /dev/null
@@ -1,29 +0,0 @@
-using Microsoft.Extensions.DependencyInjection;
-using StateR.Interceptors.Hooks;
-
-namespace StateR.Interceptors;
-
-public class InterceptorsManager : IInterceptorsManager
-{
- private readonly IInterceptorsHooksCollection _hooks;
- private readonly IServiceProvider _serviceProvider;
-
- public InterceptorsManager(IInterceptorsHooksCollection hooks, IServiceProvider serviceProvider)
- {
- _hooks = hooks ?? throw new ArgumentNullException(nameof(hooks));
- _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
- }
-
- public async Task DispatchAsync(IDispatchContext dispatchContext) where TAction : IAction
- {
- var interceptors = _serviceProvider.GetServices>().ToList();
- foreach (var interceptor in interceptors)
- {
- dispatchContext.CancellationToken.ThrowIfCancellationRequested();
-
- await _hooks.BeforeHandlerAsync(dispatchContext, interceptor, dispatchContext.CancellationToken);
- await interceptor.InterceptAsync(dispatchContext, dispatchContext.CancellationToken);
- await _hooks.AfterHandlerAsync(dispatchContext, interceptor, dispatchContext.CancellationToken);
- }
- }
-}
diff --git a/src/StateR/Internal/StatorBuilder.cs b/src/StateR/Internal/StatorBuilder.cs
index d83df9e..22585e8 100644
--- a/src/StateR/Internal/StatorBuilder.cs
+++ b/src/StateR/Internal/StatorBuilder.cs
@@ -1,38 +1,183 @@
using Microsoft.Extensions.DependencyInjection;
+using StateR.Pipeline;
+using StateR.Updaters;
+using System.Collections.ObjectModel;
namespace StateR.Internal;
public class StatorBuilder : IStatorBuilder
{
+ private readonly List _states = new();
+ private readonly List _initialStates = new();
+ private readonly List _actions = new();
+ private readonly List _updaters = new();
+ private readonly List _actionFilters = new();
+
public StatorBuilder(IServiceCollection services)
{
Services = services ?? throw new ArgumentNullException(nameof(services));
}
+
+ #region IOldStatorBuilder
+
public IStatorBuilder AddTypes(IEnumerable types)
=> AddDistinctTypes(All, types);
public IStatorBuilder AddStates(IEnumerable types)
- => AddDistinctTypes(States, types);
+ => AddDistinctTypes(_states, types);
public IStatorBuilder AddActions(IEnumerable types)
- => AddDistinctTypes(Actions, types);
+ => AddDistinctTypes(_actions, types);
public IStatorBuilder AddUpdaters(IEnumerable types)
- => AddDistinctTypes(Updaters, types);
+ => AddDistinctTypes(_updaters, types);
public IStatorBuilder AddActionHandlers(IEnumerable types)
=> AddDistinctTypes(ActionHandlers, types);
public IServiceCollection Services { get; }
- public List Actions { get; } = new List();
- public List States { get; } = new List();
public List Interceptors { get; } = new List();
public List ActionHandlers { get; } = new List();
public List AfterEffects { get; } = new List