Skip to content

Commit

Permalink
Merge pull request #384 from TimeWarpEngineering/Cramer/2024-03-03/Fe…
Browse files Browse the repository at this point in the history
…atureNamespaces

Change BlazorState.Features to TimeWarp.Features
  • Loading branch information
StevenTCramer authored Mar 3, 2024
2 parents 5559fb7 + 75d0e8b commit b54d2da
Show file tree
Hide file tree
Showing 41 changed files with 173 additions and 157 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Authors>Steven T. Cramer</Authors>
<Product>Blazor State</Product>
<PackageVersion>11.0.0-beta.8+8.0.101</PackageVersion>
<PackageVersion>11.0.0-beta.9+8.0.101</PackageVersion>
<PackageProjectUrl>https://timewarpengineering.github.io/blazor-state/</PackageProjectUrl>
<PackageTags>TimeWarp-State; TimeWarpState; BlazorState; Blazor; State; Blazor-State; MediatR; Mediator; Pipeline; Redux; Flux</PackageTags>
<PackageIcon>Logo.png</PackageIcon>
Expand Down
4 changes: 2 additions & 2 deletions Samples/01-StateActionsHandlers/Sample/Client/App.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

using System.Threading.Tasks;
using BlazorState.Pipeline.ReduxDevTools;
using BlazorState.Features.JavaScriptInterop;
using BlazorState.Features.Routing;
using TimeWarp.Features.JavaScriptInterop;
using TimeWarp.Features.Routing;
using Microsoft.AspNetCore.Components;

public partial class App : ComponentBase
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using BlazorState.Features.Developer.Components
@using TimeWarp.Features.Developer.Components
@inherits BlazorState.Pipeline.ReduxDevTools.BlazorStateDevToolsComponent
<h3>Counter</h3>
@RenderModeDisplay
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
namespace SampleDotNet8.Client.Features.Counter;

using BlazorState.Features.Persistence.Abstractions;
using BlazorState.Features.Persistence.Attributes;
using BlazorState.Services;
using TimeWarp.Features.Persistence.Abstractions;
using System.Diagnostics.CodeAnalysis;

public partial class CounterState
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SampleDotNet8.Client.Features.Counter;

using BlazorState.Features.Persistence.Attributes;
using TimeWarp.Features.Persistence.Attributes;
using System.Text.Json.Serialization;

[PersistentState(PersistentStateMethod.LocalStorage)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace SampleDotNet8.Client.Features.Counter2;

using BlazorState.Features.Persistence.Abstractions;
using BlazorState.Features.Persistence.Attributes;
using TimeWarp.Features.Persistence.Abstractions;
using TimeWarp.Features.Persistence.Attributes;
using BlazorState.Services;
using System.Diagnostics.CodeAnalysis;

Expand Down Expand Up @@ -35,7 +35,7 @@ public override async Task Handle(Action aAction, CancellationToken aCancellatio
}
catch (Exception e)
{
// if this is a JavaScript not avaailable exception then we are prerendering and just ignore it
// if this is a JavaScript not available exception then we are prerendering and just ignore it
Console.WriteLine("**********************************");
Console.WriteLine(e);
Console.WriteLine("**********************************");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SampleDotNet8.Client.Features.Counter2;

using BlazorState.Features.Persistence.Attributes;
using TimeWarp.Features.Persistence.Attributes;
using System.Text.Json.Serialization;

[PersistentState(PersistentStateMethod.LocalStorage)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ else
<button class="btn btn-primary" @onclick="IncrementCount2">Click me</button>
}

<BlazorState.Features.ReduxDevTools.Components.ReduxDevTools />
<BlazorState.Features.JavaScriptInterop.Components.TimeWarpJavaScriptInterop />
<BlazorState.Features.Routing.Components.TimeWarpNavigation />
<TimeWarp.Features.ReduxDevTools.Components.ReduxDevTools />
<TimeWarp.Features.JavaScriptInterop.Components.TimeWarpJavaScriptInterop />
<TimeWarp.Features.Routing.Components.TimeWarpNavigation />
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace SampleDotNet8.Client;

using Blazored.LocalStorage;
using TimeWarp.State.Plus.PersistentState;
using BlazorState.Features.Persistence.Abstractions;
using TimeWarp.Features.Persistence.Abstractions;
public class Program
{
private static async Task Main(string[] args)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,78 +1,78 @@
@page "/weather"
@attribute [StreamRendering]

<PageTitle>Weather</PageTitle>

<h1>Weather</h1>

<p>This component demonstrates showing data.</p>

@if (System.OperatingSystem.IsBrowser())
{
<p>Render Mode: WebAssembly</p>
}
else
{
<p>Render Mode: Server (Not Wasm)</p>
}

@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}

<BlazorState.Features.ReduxDevTools.Components.ReduxDevTools />
<BlazorState.Features.JavaScriptInterop.Components.TimeWarpJavaScriptInterop />
<BlazorState.Features.Routing.Components.TimeWarpNavigation />


@code {
private WeatherForecast[]? forecasts;

protected override async Task OnInitializedAsync()
{
// Simulate asynchronous loading to demonstrate streaming rendering
await Task.Delay(500);

var startDate = DateOnly.FromDateTime(DateTime.Now);
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
}).ToArray();
}

private class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public string? Summary { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
}
@page "/weather"
@attribute [StreamRendering]

<PageTitle>Weather</PageTitle>

<h1>Weather</h1>

<p>This component demonstrates showing data.</p>

@if (System.OperatingSystem.IsBrowser())
{
<p>Render Mode: WebAssembly</p>
}
else
{
<p>Render Mode: Server (Not Wasm)</p>
}

@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}

<TimeWarp.Features.ReduxDevTools.Components.ReduxDevTools />
<TimeWarp.Features.JavaScriptInterop.Components.TimeWarpJavaScriptInterop />
<TimeWarp.Features.Routing.Components.TimeWarpNavigation />


@code {
private WeatherForecast[]? forecasts;

protected override async Task OnInitializedAsync()
{
// Simulate asynchronous loading to demonstrate streaming rendering
await Task.Delay(500);

var startDate = DateOnly.FromDateTime(DateTime.Now);
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
}).ToArray();
}

private class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public string? Summary { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
}
12 changes: 6 additions & 6 deletions Source/BlazorState/EventIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ internal class EventIds
public static readonly EventId JsonRequestOfInvalidType = new(203, nameof(JsonRequestOfInvalidType));
// Routing
// Pipeline - Middleware
// CloneState
public static readonly EventId CloneStateBehavior_Initializing = new(400, nameof(CloneStateBehavior_Initializing));
public static readonly EventId CloneStateBehavior_Cloning = new(401, nameof(CloneStateBehavior_Cloning));
public static readonly EventId CloneStateBehavior_Ignoring = new(402, nameof(CloneStateBehavior_Ignoring));
public static readonly EventId CloneStateBehavior_Exception = new(403, nameof(CloneStateBehavior_Exception));
public static readonly EventId CloneStateBehavior_Restored = new(404, nameof(CloneStateBehavior_Restored));
// StateTransaction
public static readonly EventId StateTransactionBehavior_Initializing = new(400, nameof(StateTransactionBehavior_Initializing));
public static readonly EventId StateTransactionBehavior_Cloning = new(401, nameof(StateTransactionBehavior_Cloning));
public static readonly EventId StateTransactionBehavior_Ignoring = new(402, nameof(StateTransactionBehavior_Ignoring));
public static readonly EventId StateTransactionBehavior_Exception = new(403, nameof(StateTransactionBehavior_Exception));
public static readonly EventId StateTransactionBehavior_Restored = new(404, nameof(StateTransactionBehavior_Restored));

// ReduxDevTools
public static readonly EventId StartHandler_Initializing = new(500, nameof(StartHandler_Initializing));
Expand Down
2 changes: 1 addition & 1 deletion Source/BlazorState/Extensions/BlazorStateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class BlazorStateOptions
///// </summary>
public IEnumerable<Assembly> Assemblies { get; set; }

public bool UseCloneStateBehavior { get; set; } = true;
public bool UseStateTransactionBehavior { get; set; } = true;

public bool UseRouting { get; set; } = true;

Expand Down
8 changes: 4 additions & 4 deletions Source/BlazorState/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public static IServiceCollection AddBlazorState
EnsureStates(aServiceCollection, blazorStateOptions);
EnsureMediator(aServiceCollection, blazorStateOptions);

if (blazorStateOptions.UseCloneStateBehavior)
if (blazorStateOptions.UseStateTransactionBehavior)
{
aServiceCollection.AddScoped(typeof(IPipelineBehavior<,>), typeof(CloneStateBehavior<,>));
aServiceCollection.AddScoped(typeof(IPipelineBehavior<,>), typeof(StateTransactionBehavior<,>));
}

if (blazorStateOptions.UseRouting)
Expand All @@ -59,8 +59,8 @@ private static void EnsureHttpClient(IServiceCollection aServiceCollection)
{
var blazorHostingLocation = new BlazorHostingLocation();

// Test.App.Server Side Blazor doesn't register HttpClient by default
if (!blazorHostingLocation.IsServerSide) return;
// Test.App.Client Side Blazor registers HttpClient by default
if (blazorHostingLocation.IsClientSide) return;

// Double check that nothing is registered.
if (aServiceCollection.HasRegistrationFor(typeof(HttpClient))) return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
#nullable enable
namespace BlazorState.Pipeline.State;
namespace TimeWarp.Features.StateTransaction;

internal sealed class CloneStateBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
/// <summary>
/// Represents a pipeline behavior in BlazorState that clones the current state before processing a request.
/// This behavior ensures that the state can be reverted to its original form in case of an error during the request handling.
/// The cloning process is contingent upon the state implementing <see cref="ICloneable"/>, allowing for a deep copy.
/// If the state does not implement <see cref="ICloneable"/>, it falls back to a custom clone method. This behavior is
/// critical for maintaining application consistency and enables undo functionality.
/// </summary>
/// <remarks>
/// This behavior is part of the BlazorState pipeline, intercepting actions (requests) to clone the relevant state before
/// proceeding. If an action fails, the system reverts to the cloned state, thus preventing partial state updates
/// from corrupting the application state. It uses MediatR's pipeline behavior feature to hook into the request handling
/// process.
/// </remarks>
/// <typeparam name="TRequest"></typeparam>
/// <typeparam name="TResponse"></typeparam>
public sealed class StateTransactionBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : notnull, IAction
{
private readonly ILogger Logger;
private readonly IPublisher Publisher;
private readonly IStore Store;

public CloneStateBehavior
public StateTransactionBehavior
(
ILogger<CloneStateBehavior<TRequest, TResponse>> logger,
ILogger<StateTransactionBehavior<TRequest, TResponse>> logger,
IStore store,
IPublisher publisher
)
{
Logger = logger;
Logger.LogDebug(EventIds.CloneStateBehavior_Initializing, "constructing");
Logger.LogDebug(EventIds.StateTransactionBehavior_Initializing, "constructing");
Store = store;
Publisher = publisher;
}
Expand All @@ -43,7 +58,7 @@ CancellationToken aCancellationToken

Logger.LogDebug
(
EventIds.CloneStateBehavior_Cloning,
EventIds.StateTransactionBehavior_Cloning,
"Clone State of type {declaringType} originalState.Guid:{originalState_Guid} newState.Guid:{newState_Guid}",
enclosingStateType,
originalState.Guid,
Expand All @@ -60,20 +75,20 @@ CancellationToken aCancellationToken
catch (Exception exception)
{
// If something fails we restore system to previous state.
Logger.LogWarning(EventIds.CloneStateBehavior_Exception, exception, "Error cloning State");
Logger.LogWarning(EventIds.StateTransactionBehavior_Exception, exception, "Error cloning State");

Store.SetState(originalState);

Logger.LogWarning
(
EventIds.CloneStateBehavior_Restored,
EventIds.StateTransactionBehavior_Restored,
"Restored State of type: {enclosingStateType}",
enclosingStateType
);

var exceptionNotification = new ExceptionNotification
{
RequestName = nameof(CloneStateBehavior<TRequest, TResponse>),
RequestName = nameof(StateTransactionBehavior<TRequest, TResponse>),
Exception = exception
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@code {
@namespace TimeWarp.Features.Developer
@code {

[Inject] private ReduxDevToolsInterop ReduxDevToolsInterop { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@namespace TimeWarp.Features.Developer
<div class="render-mode-display">
<div>Current Render Mode: @CurrentRenderMode</div>
<div>Configured Render Mode: @ConfiguredRenderMode</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

@using BlazorState.Features.JavaScriptInterop;
@namespace TimeWarp.Features.JavaScriptInterop

@code {

Expand Down
Loading

0 comments on commit b54d2da

Please sign in to comment.