diff --git a/samples/Memento.Sample.Blazor/Stores/FluxAsyncCounterStore.cs b/samples/Memento.Sample.Blazor/Stores/FluxAsyncCounterStore.cs index 56ea550..2223b73 100644 --- a/samples/Memento.Sample.Blazor/Stores/FluxAsyncCounterStore.cs +++ b/samples/Memento.Sample.Blazor/Stores/FluxAsyncCounterStore.cs @@ -9,7 +9,7 @@ public record FluxAsyncCounterState { public bool IsLoading { get; init; } = false; - public ImmutableArray Histories { get; init; } = ImmutableArray.Create(); + public ImmutableArray Histories { get; init; } = []; } public record FluxAsyncCounterCommand : Command { diff --git a/src/Memento.Blazor/StateChangedObserver.cs b/src/Memento.Blazor/StateChangedObserver.cs index 2b98cf9..6a34df7 100644 --- a/src/Memento.Blazor/StateChangedObserver.cs +++ b/src/Memento.Blazor/StateChangedObserver.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Components; +using Memento.Blazor.Internal; +using Microsoft.AspNetCore.Components; using System.Collections.Concurrent; namespace Memento.Blazor; diff --git a/src/Memento.Core/AbstractStore.cs b/src/Memento.Core/AbstractStore.cs index 097a5c7..ec64d1d 100644 --- a/src/Memento.Core/AbstractStore.cs +++ b/src/Memento.Core/AbstractStore.cs @@ -88,6 +88,14 @@ public IDisposable Subscribe(IObserver> if (e is IStateChangedEventArgs e2) { observer.OnNext(e2); } + else { + observer.OnNext(new StateChangedEventArgs() { + LastState = State, + Message = e.Message, + State = State, + Sender = this, + }); + } })); } @@ -154,8 +162,8 @@ internal void ComputedAndApplyState(TState state, TMessage? command) { [MethodImpl(MethodImplOptions.AggressiveInlining)] (TState?, StateChangedEventArgs?) ComputeNewState() { - var previous = state; - var postState = OnBeforeDispatch(previous, command); + var previous = State; + var postState = OnBeforeDispatch(state, command); if (MiddlewareHandler.Invoke(postState, command) is TState s) { var newState = OnAfterDispatch(s, command); diff --git a/src/Memento.ReduxDevTool/Internals/LiftedHistoryContainer.cs b/src/Memento.ReduxDevTool/Internals/LiftedHistoryContainer.cs index 35e210c..4649e1c 100644 --- a/src/Memento.ReduxDevTool/Internals/LiftedHistoryContainer.cs +++ b/src/Memento.ReduxDevTool/Internals/LiftedHistoryContainer.cs @@ -167,7 +167,7 @@ public void SetStatesToStore(HistoryState? history) { public async Task CommitAsync() { var history = CurrentHistory; - _histories = ImmutableArray.Create(history.AsInitial()); + _histories = [history.AsInitial()]; await SyncWithPlugin(); _currentCursorId = 0; @@ -176,7 +176,7 @@ public async Task CommitAsync() { public async Task RollbackAsync() { var history = _histories.First(); - _histories = ImmutableArray.Create(history.AsInitial()); + _histories = [history.AsInitial()]; await SyncWithPlugin(); _currentCursorId = 0; diff --git a/test/Memento.Test/Core/FluxStoreTest.cs b/test/Memento.Test/Core/FluxStoreTest.cs index 9849cdb..f49f8bb 100644 --- a/test/Memento.Test/Core/FluxStoreTest.cs +++ b/test/Memento.Test/Core/FluxStoreTest.cs @@ -63,7 +63,7 @@ public async Task Command_CouldBeSubscribeCorrectly() { Assert.NotEqual(e.State, lastState); Assert.Equal(e.LastState, lastState); lastState = e.State; - commands.Add(e.Command); + commands.Add(e.Message); }); await store.CountUpAsync(); @@ -107,14 +107,14 @@ public async Task Force_ReplaceState() { await store.CountUpAsync(); Assert.True(commands is [ - { Command: FluxAsyncCounterCommands.BeginLoading }, - { Command: FluxAsyncCounterCommands.Increment }, - { Command: FluxAsyncCounterCommands.EndLoading }, - { Command: FluxAsyncCounterCommands.ModifyCount(1234) }, + { Message: FluxAsyncCounterCommands.BeginLoading }, + { Message: FluxAsyncCounterCommands.Increment }, + { Message: FluxAsyncCounterCommands.EndLoading }, + { Message: FluxAsyncCounterCommands.ModifyCount(1234) }, { State: FluxAsyncCounterState { Count: 5678 }, StateChangeType: StateChangeType.ForceReplaced }, - { Command: FluxAsyncCounterCommands.BeginLoading }, - { Command: FluxAsyncCounterCommands.Increment }, - { Command: FluxAsyncCounterCommands.EndLoading }, + { Message: FluxAsyncCounterCommands.BeginLoading }, + { Message: FluxAsyncCounterCommands.Increment }, + { Message: FluxAsyncCounterCommands.EndLoading }, ]); } diff --git a/test/Memento.Test/Core/Mock/AsyncCounterStore.cs b/test/Memento.Test/Core/Mock/AsyncCounterStore.cs index b7e36aa..3326e99 100644 --- a/test/Memento.Test/Core/Mock/AsyncCounterStore.cs +++ b/test/Memento.Test/Core/Mock/AsyncCounterStore.cs @@ -6,7 +6,7 @@ namespace Memento.Test.Core.Mock; // Define state to manage in store public record AsyncCounterState { public int Count { get; init; } = 0; - public ImmutableArray History { get; init; } = ImmutableArray.Create(); + public ImmutableArray History { get; init; } = []; public bool IsLoading { get; init; } = false; } diff --git a/test/Memento.Test/Core/Mock/FluxAsyncCounterStore.cs b/test/Memento.Test/Core/Mock/FluxAsyncCounterStore.cs index 665f8ea..e2558f2 100644 --- a/test/Memento.Test/Core/Mock/FluxAsyncCounterStore.cs +++ b/test/Memento.Test/Core/Mock/FluxAsyncCounterStore.cs @@ -8,7 +8,7 @@ namespace Memento.Test.Core.Mock; // Define state to manage in store public record FluxAsyncCounterState { public int Count { get; init; } = 0; - public ImmutableArray History { get; init; } = ImmutableArray.Create(); + public ImmutableArray History { get; init; } = []; public bool IsLoading { get; init; } = false; } @@ -45,7 +45,7 @@ static FluxAsyncCounterState Reducer(FluxAsyncCounterState state, FluxAsyncCount Count = payload.Value, History = state.History.Add(payload.Value), }, - _ => throw new CommandNotHandledException(command), + _ => throw new CommandNotHandledException(command), }; static FluxAsyncCounterState HandleIncrement(FluxAsyncCounterState state) { diff --git a/test/Memento.Test/Core/Mock/MockMiddleware.cs b/test/Memento.Test/Core/Mock/MockMiddleware.cs index 7338e23..9d8e4fa 100644 --- a/test/Memento.Test/Core/Mock/MockMiddleware.cs +++ b/test/Memento.Test/Core/Mock/MockMiddleware.cs @@ -16,7 +16,7 @@ public class MockMiddlewareHandler : MiddlewareHandler { public override RootState? HandleProviderDispatch( RootState? state, - IStateChangedEventArgs e, + IStateChangedEventArgs e, NextProviderMiddlewareCallback next ) { ProviderDispatchCalledCount++; @@ -25,7 +25,7 @@ NextProviderMiddlewareCallback next public override object? HandleStoreDispatch( object? state, - Command command, + object? command, NextStoreMiddlewareCallback next ) { HandleStoreDispatchCalledCount++; diff --git a/test/Memento.Test/Core/Mock/RedoUndoTodoStore.cs b/test/Memento.Test/Core/Mock/RedoUndoTodoStore.cs index 6e40bc5..0c5b2c2 100644 --- a/test/Memento.Test/Core/Mock/RedoUndoTodoStore.cs +++ b/test/Memento.Test/Core/Mock/RedoUndoTodoStore.cs @@ -5,7 +5,7 @@ namespace Memento.Sample.Blazor.Stores; public record RedoUndoTodoState { - public ImmutableArray Todos { get; init; } = ImmutableArray.Create(); + public ImmutableArray Todos { get; init; } = []; public bool IsLoading { get; init; } } diff --git a/test/Memento.Test/Core/ProviderTest.cs b/test/Memento.Test/Core/ProviderTest.cs index e795bdd..d61e6d1 100644 --- a/test/Memento.Test/Core/ProviderTest.cs +++ b/test/Memento.Test/Core/ProviderTest.cs @@ -26,9 +26,8 @@ public async Task Test() { await mementoProvider.InitializeAsync(); - await Assert.ThrowsAnyAsync(async () => { - await mementoProvider.InitializeAsync(); - }); + // Ensure root state is correct + await mementoProvider.InitializeAsync(); store.CountUp(); store.CountUp(); @@ -43,7 +42,7 @@ await Assert.ThrowsAnyAsync(async () => { var expected = JsonSerializer.Serialize(new { AsyncCounterStore = new AsyncCounterState { Count = 5, - History = ImmutableArray.Create(1, 2, 3, 4, 5), + History = [1, 2, 3, 4, 5], } }); var actual = JsonSerializer.Serialize(root.AsDictionary()); diff --git a/test/Memento.Test/Core/StoreTest.cs b/test/Memento.Test/Core/StoreTest.cs index aeb763b..91c4abc 100644 --- a/test/Memento.Test/Core/StoreTest.cs +++ b/test/Memento.Test/Core/StoreTest.cs @@ -1,6 +1,7 @@ using FluentAssertions; using Memento.Core; using Memento.Test.Core.Mock; +using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities; using System.Collections.Concurrent; using System.Reflection; @@ -55,28 +56,28 @@ public async Task Ensure_CanChangeStateAsync(int count) { public async Task Command_CouldBeSubscribeCorrectly() { var store = new AsyncCounterStore(); - var commands = new List(); + var messages = new List(); var lastState = store.State; using var subscription = store.Subscribe(e => { Assert.Equal(e.Sender, store); Assert.NotEqual(e.State, lastState); Assert.Equal(e.LastState, lastState); lastState = e.State; - commands.Add(e.Command); + messages.Add(e.Message); }); await store.CountUpAsync(); store.SetCount(1234); await store.CountUpAsync(); - Assert.True(commands is [ - Command.StateHasChanged, - Command.StateHasChanged, - Command.StateHasChanged, - Command.StateHasChanged, - Command.StateHasChanged, - Command.StateHasChanged, - Command.StateHasChanged, + Assert.True(messages is [ + null, + null, + null, + null, + null, + null, + null, ]); } @@ -84,7 +85,7 @@ public async Task Command_CouldBeSubscribeCorrectly() { public async Task Force_ReplaceState() { var store = new AsyncCounterStore(); - var commands = new List>(); + var events = new List>(); var lastState = store.State; using var subscription = store.Subscribe(e => { @@ -92,12 +93,12 @@ public async Task Force_ReplaceState() { Assert.NotEqual(e.State, lastState); Assert.Equal(e.LastState, lastState); lastState = e.State; - commands.Add(e); + events.Add(e); }); await store.CountUpAsync(); store.SetCount(1234); - if (store is IStore iStore) { + if (store is IStore iStore) { iStore.SetStateForce(store.State with { Count = 5678 }); @@ -105,24 +106,26 @@ public async Task Force_ReplaceState() { await store.CountUpAsync(); - Assert.True(commands is [ - { Command: Command.StateHasChanged }, - { Command: Command.StateHasChanged }, - { Command: Command.StateHasChanged }, - { Command: Command.StateHasChanged }, - { Command: null, State: AsyncCounterState { Count: 5678 }, StateChangeType: StateChangeType.ForceReplaced }, - { Command: Command.StateHasChanged }, - { Command: Command.StateHasChanged }, - { Command: Command.StateHasChanged }, + Assert.True(events is [ + { Message: null }, + { Message: null }, + { Message: null }, + { Message: null }, + { Message: null, State: AsyncCounterState { Count: 5678 }, StateChangeType: StateChangeType.ForceReplaced }, + { Message: null }, + { Message: null }, + { Message: null }, ]); } [Fact] public void Ensure_StateHasChangedInvoked() { var store = new AsyncCounterStore(); - var commands = new List>(); + var commands = new List>(); var lastState = store.State; - using var subscription = store.Subscribe(e => commands.Add(e)); + using var subscription = store.Subscribe(e => { + commands.Add(e); + }); store.StateHasChanged(); store.StateHasChanged(); @@ -144,16 +147,15 @@ public void Ensure_StateHasChangedInvoked() { [Fact] public void Ensure_Observable() { var store = new AsyncCounterStore(); - var observers = (store.GetType() - .BaseType - ?.BaseType - ?.BaseType - ?.GetField( - "_observers", - BindingFlags.NonPublic | BindingFlags.Instance - ) - ?.GetValue(store) as IDictionary>>> - ) ?? throw new Exception("_observers is not found."); + var observers = ( + store.GetType() + .BaseType + ?.BaseType + ?.BaseType + ?.BaseType + ?.GetField("_observers", BindingFlags.NonPublic | BindingFlags.Instance) + ?.GetValue(store) as ConcurrentDictionary>> + ) ?? throw new Exception("_observers is not found."); var disposables = new BlockingCollection(); Parallel.For(0, 10000, i => {