Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions StateR.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CounterApp", "samples\Count
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CounterApp.Tests", "samples\CounterApp\CounterApp.Tests\CounterApp.Tests.csproj", "{FBDEBA94-7F63-4CB5-AC13-4D0874730316}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StateR.Microsoft.Extensions.DependencyInjection", "src\StateR.Microsoft.Extensions.DependencyInjection\StateR.Microsoft.Extensions.DependencyInjection.csproj", "{5C432129-E637-4895-895D-1FDFDC61C049}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -119,6 +121,18 @@ Global
{FBDEBA94-7F63-4CB5-AC13-4D0874730316}.Release|x64.Build.0 = Release|Any CPU
{FBDEBA94-7F63-4CB5-AC13-4D0874730316}.Release|x86.ActiveCfg = Release|Any CPU
{FBDEBA94-7F63-4CB5-AC13-4D0874730316}.Release|x86.Build.0 = Release|Any CPU
{5C432129-E637-4895-895D-1FDFDC61C049}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C432129-E637-4895-895D-1FDFDC61C049}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C432129-E637-4895-895D-1FDFDC61C049}.Debug|x64.ActiveCfg = Debug|Any CPU
{5C432129-E637-4895-895D-1FDFDC61C049}.Debug|x64.Build.0 = Debug|Any CPU
{5C432129-E637-4895-895D-1FDFDC61C049}.Debug|x86.ActiveCfg = Debug|Any CPU
{5C432129-E637-4895-895D-1FDFDC61C049}.Debug|x86.Build.0 = Debug|Any CPU
{5C432129-E637-4895-895D-1FDFDC61C049}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C432129-E637-4895-895D-1FDFDC61C049}.Release|Any CPU.Build.0 = Release|Any CPU
{5C432129-E637-4895-895D-1FDFDC61C049}.Release|x64.ActiveCfg = Release|Any CPU
{5C432129-E637-4895-895D-1FDFDC61C049}.Release|x64.Build.0 = Release|Any CPU
{5C432129-E637-4895-895D-1FDFDC61C049}.Release|x86.ActiveCfg = Release|Any CPU
{5C432129-E637-4895-895D-1FDFDC61C049}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -132,6 +146,7 @@ Global
{E72B8B55-3F7B-4EE2-955B-B5FAB222EBD9} = {22B777AC-AFAE-422A-B4CD-48C907251D9E}
{99926EB0-84F9-4906-8F7E-4E1873A403EB} = {E72B8B55-3F7B-4EE2-955B-B5FAB222EBD9}
{FBDEBA94-7F63-4CB5-AC13-4D0874730316} = {E72B8B55-3F7B-4EE2-955B-B5FAB222EBD9}
{5C432129-E637-4895-895D-1FDFDC61C049} = {F0F6A2CA-0972-43BD-B777-B5656DFE20C3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6ADD1933-C449-475E-9409-AD333C1C48A0}
Expand Down
11 changes: 10 additions & 1 deletion samples/CounterApp/CounterApp/CounterApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@
</PropertyGroup>

<ItemGroup>
<Compile Remove="Features\WeatherForecast.cs" />
</ItemGroup>

<ItemGroup>
<None Include="Features\WeatherForecast.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="ForEvolve.Blazor.WebStorage" Version="0.2.5-gc839ff00f3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.1" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\StateR.Blazor.Experiments\StateR.Blazor.Experiments.csproj" />
<ProjectReference Include="..\..\..\src\StateR.Blazor\StateR.Blazor.csproj" />
<ProjectReference Include="..\..\..\src\StateR.Experiments\StateR.Experiments.csproj" />
<ProjectReference Include="..\..\..\src\StateR.Microsoft.Extensions.DependencyInjection\StateR.Microsoft.Extensions.DependencyInjection.csproj" />
</ItemGroup>

</Project>
18 changes: 6 additions & 12 deletions samples/CounterApp/CounterApp/Features/Counter.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
using FluentValidation;
using StateR;
using StateR.AfterEffects;
using StateR.Blazor.Persistance;
using ForEvolve.Blazor.WebStorage;
using StateR.Interceptors;
using StateR.Internal;
//using StateR.Blazor.Persistance;
using StateR.Updaters;
using System;
using System.Reflection;

namespace CounterApp.Features;

public class Counter
{
[Persist]
//[Persist]
public record class State(int Count) : StateBase;

public class InitialState : IInitialState<State>
{
public State Value => new(0);
}

public record class Increment : IAction;
public record class Decrement : IAction;
public record class SetPositive(int Count) : IAction;
public record class SetNegative(int Count) : IAction;
public record class Increment : IAction<State>;
public record class Decrement : IAction<State>;
public record class SetPositive(int Count) : IAction<State>;
public record class SetNegative(int Count) : IAction<State>;

public class Updaters : IUpdater<Increment, State>, IUpdater<Decrement, State>, IUpdater<SetPositive, State>, IUpdater<SetNegative, State>
{
Expand Down
32 changes: 15 additions & 17 deletions samples/CounterApp/CounterApp/Features/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using StateR;
using StateR.AfterEffects;
using StateR.AsyncLogic;
using StateR.Interceptors;
using StateR.Internal;
using StateR.Updaters;
using System.Collections.Immutable;
Expand Down Expand Up @@ -37,13 +35,13 @@ public State Update(Reload action, State state)
=> state with { Status = AsyncOperationStatus.Idle, Forecasts = ImmutableList.Create<Forecast>() };
}

public class ReloadEffect : IAfterEffects<Reload>
{
public async Task HandleAfterEffectAsync(IDispatchContext<Reload> context, CancellationToken cancellationToken)
{
await context.Dispatcher.DispatchAsync(new Fetch(), cancellationToken);
}
}
//public class ReloadEffect : IAfterEffects<Reload>
//{
// public async Task HandleAfterEffectAsync(IDispatchContext<Reload> context, CancellationToken cancellationToken)
// {
// await context.Dispatcher.DispatchAsync(new Fetch(), cancellationToken);
// }
//}

public class FetchOperation : AsyncOperation<Fetch, State, Fetched>
{
Expand All @@ -61,12 +59,12 @@ protected override async Task<Fetched> LoadAsync(Fetch action, State initalState
}
}

public class Delays : IInterceptor<StatusUpdated<State>>
{
public async Task InterceptAsync(IDispatchContext<StatusUpdated<State>> context, CancellationToken cancellationToken)
{
Console.WriteLine($"{context.Action.GetType().GetStatorName()}: {context.Action.status}");
await Task.Delay(2000, cancellationToken);
}
}
//public class Delays : IInterceptor<StatusUpdated<State>>
//{
// public async Task InterceptAsync(IDispatchContext<StatusUpdated<State>> context, CancellationToken cancellationToken)
// {
// Console.WriteLine($"{context.Action.GetType().GetStatorName()}: {context.Action.status}");
// await Task.Delay(2000, cancellationToken);
// }
//}
}
7 changes: 4 additions & 3 deletions samples/CounterApp/CounterApp/Pages/FetchData.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using StateR.AsyncLogic
@*@using StateR.AsyncLogic
@using StateR.Blazor.Components
@page "/fetchdata"
@inherits StatorComponent
Expand All @@ -10,14 +10,14 @@
<p>This component demonstrates fetching data from the server.</p>
<p><strong>Async Status:</strong> @WeatherState.Current.Status</p>

<Dispatch TAction="Features.WeatherForecast.Fetch" />
<Dispatch TAction="Features.WeatherForecast.Fetch" />*@
@*<DispatchAction
TAction="Features.WeatherForecast.Fetch"
Action="new Features.WeatherForecast.Fetch()" />*@
@*<DispatchActionFactory
TAction="Features.WeatherForecast.Fetch"
ActionFactory="() => ValueTask.FromResult(new Features.WeatherForecast.Fetch())" />*@
<AsyncOperationSwitch Status="WeatherState.Current.Status">
@*<AsyncOperationSwitch Status="WeatherState.Current.Status">
<Idle>
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
Expand Down Expand Up @@ -60,3 +60,4 @@
</div>
</Failed>
</AsyncOperationSwitch>
*@
26 changes: 16 additions & 10 deletions samples/CounterApp/CounterApp/Program.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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<CounterApp.Features.Counter.State, CounterApp.Features.Counter.InitialState>()
.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()
)
;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using FluentValidation.Results
@*@using FluentValidation.Results
@using StateR.Validations.FluentValidation;
@inherits StatorComponent
@inject IState<ValidationState> ValidationState
Expand Down Expand Up @@ -29,3 +29,4 @@
await DispatchAsync(new CleanValidationError());
}
}
*@
4 changes: 2 additions & 2 deletions samples/CounterApp/CounterApp/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using StateR.Blazor.Components;
@*@using StateR.Blazor.Components;*@
@inherits LayoutComponentBase

<div class="page">
Expand All @@ -15,5 +15,5 @@
@Body
</article>
</main>
<ErrorToastStack />
@*<ErrorToastStack />*@
</div>
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
12 changes: 10 additions & 2 deletions src/StateR.Blazor/StatorComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ public abstract class StatorComponentBase : ComponentBase, IDisposable
[Inject]
public IDispatcher? Dispatcher { get; set; }

protected virtual async Task DispatchAsync<TAction>(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, TState>(TAction action, CancellationToken cancellationToken = default)
where TAction : IAction<TState>
where TState : StateBase
{
GuardAgainstNullDispatcher();
await Dispatcher.DispatchAsync<TAction, TState>(action, cancellationToken);
}

private void Dispose(bool disposing)
{
if (!_disposedValue)
Expand Down
4 changes: 2 additions & 2 deletions src/StateR.Experiments/AsyncLogic/AsyncError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class AsyncError
{
public record State : StateBase
{
public IAction? Action { get; init; }
public IAction<State>? Action { get; init; }
public AsyncState? InitialState { get; init; }
public AsyncState? ActualState { get; init; }
public Exception? Exception { get; init; }
Expand All @@ -21,7 +21,7 @@ public class InitialState : IInitialState<State>
public State Value => new();
}

public record Occured(IAction Action, AsyncState InitialState, AsyncState ActualState, Exception Exception) : IAction;
public record Occured(IAction<State> Action, AsyncState InitialState, AsyncState ActualState, Exception Exception) : IAction<State>;

public class Updaters : IUpdater<Occured, State>
{
Expand Down
2 changes: 1 addition & 1 deletion src/StateR.Experiments/AsyncLogic/StartupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static IStatorBuilder AddAsyncOperations(this IStatorBuilder builder)
builder.AddTypes(new[] { typeof(StatusUpdated<>) });

// Async Operation's Errors
builder.Services.TryAddSingleton<IActionHandler<AsyncError.Occured>, UpdaterActionHandler<AsyncError.State, AsyncError.Occured>>();
builder.Services.TryAddSingleton<IActionHandler<AsyncError.Occured>, UpdaterMiddleware<AsyncError.State, AsyncError.Occured>>();
builder.Services.TryAddSingleton<IUpdater<AsyncError.Occured, AsyncError.State>, AsyncError.Updaters>();
builder.Services.TryAddSingleton<IInitialState<AsyncError.State>, AsyncError.InitialState>();
builder.Services.TryAddSingleton<IState<AsyncError.State>, Internal.State<AsyncError.State>>();
Expand Down
18 changes: 18 additions & 0 deletions src/StateR.Experiments/StateR.Experiments.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
<RootNamespace>StateR</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Compile Remove="AsyncLogic\AsyncError.cs" />
<Compile Remove="AsyncLogic\AsyncOperation.cs" />
<Compile Remove="AsyncLogic\AsyncOperationStatus.cs" />
<Compile Remove="AsyncLogic\AsyncState.cs" />
<Compile Remove="AsyncLogic\StartupExtensions.cs" />
<Compile Remove="AsyncLogic\StatusUpdated.cs" />
</ItemGroup>

<ItemGroup>
<None Include="AsyncLogic\AsyncError.cs" />
<None Include="AsyncLogic\AsyncOperation.cs" />
<None Include="AsyncLogic\AsyncOperationStatus.cs" />
<None Include="AsyncLogic\AsyncState.cs" />
<None Include="AsyncLogic\StartupExtensions.cs" />
<None Include="AsyncLogic\StatusUpdated.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="10.3.6" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="10.3.6" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ValidationState, ValidationInitialState>()
.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);
Expand Down
Loading