Memento.Core
1.0.0
See the version list below for details.
dotnet add package Memento.Core --version 1.0.0
NuGet\Install-Package Memento.Core -Version 1.0.0
<PackageReference Include="Memento.Core" Version="1.0.0" />
paket add Memento.Core --version 1.0.0
#r "nuget: Memento.Core, 1.0.0"
// Install Memento.Core as a Cake Addin #addin nuget:?package=Memento.Core&version=1.0.0 // Install Memento.Core as a Cake Tool #tool nuget:?package=Memento.Core&version=1.0.0
Memento
Easy unidirectional store and red/undo library for state management for frontend apps on Blazor/.NET
Basic Concept
We provides a Store that allows you to share state between components. Stores are managed by a single Provider and can subscribe to state change notifications. Undirectional flow and immutable change of state provides a predictable architecture. In addition, we provide a store that easily implements Redo/Undo by managing in an immutable state.
For patterns like Flux or MVU
Besides a simple store pattern, we also provide patterns inspired by MVU patterns such as Flux and Elm. Since we change the state through the Reducer, we can change the state based on stricter rules and observe the state in detail.
Devtool
Redux DevTools is supported. Redux DevTools is a tool for debugging application's state changes. State can be time traveled and history can be viewed in DevTools.
See documentation for details of usage.
DEMO Page
https://le-nn.github.io/memento/
React or TS/JS bindings
Currently, moved to here https://github.com/le-nn/memento-js
Features
- Less boilarplate, less rule and simple usage
- Immutable and Unidirectional data flow
- Multiple stores but manged by single provider, so can observe and manage as one state tree
- Observe detailed status with command patterns and makes it easier to monitor what happened within the application
Concepts and Data Flow
<img width="800px" src="./Architecture.jpg"/>
Rules
- State should always be read-only.
- The UI then uses the new state to render its display.
For patterns like Flux
- Every Reducer that processes in the action will create new state to reflect the old state combined with the changes expected for the action.
- To change state our app should Dispatch via Reducer in the action method
Overview
This is an C# and Blazor example that implements counter.
Store
using Memento.Core;
using System.Collections.Immutable;
using static Memento.Sample.Blazor.Stores.AsyncCounterCommands;
namespace Memento.Sample.Blazor.Stores;
public record AsyncCounterState {
public int Count { get; init; } = 0;
public bool IsLoading { get; init; } = false;
public ImmutableArray<int> Histories { get; init; } = ImmutableArray.Create<int>();
}
public record AsyncCounterCommands: Command {
public record CountUp : AsyncCounterCommands;
public record Increment : AsyncCounterCommands;
public record SetCount(int Count) : AsyncCounterCommands;
public record BeginLoading : AsyncCounterCommands;
}
public class AsyncCounterStore : Store<AsyncCounterState, AsyncCounterCommands> {
public AsyncCounterStore() : base(() => new(), Reducer) { }
static AsyncCounterState Reducer(AsyncCounterState state, AsyncCounterCommands command) {
return command switch {
CountUp => state with {
Count = state.Count + 1,
IsLoading = false,
Histories = state.Histories.Add(state.Count + 1),
},
SetCount payload => state with {
Count = payload.Count,
},
Increment => state with {
Count = state.Count + 1,
},
BeginLoading => state with {
IsLoading = true,
},
_ => throw new CommandNotHandledException(command),
};
}
public async Task CountUpAsync() {
Dispatch(new BeginLoading());
await Task.Delay(800);
Dispatch(new CountUp());
}
public void CountUpManyTimes(int count) {
for (int i = 0; i < count; i++) {
Dispatch(new Increment());
}
}
public void SetCount(int c) {
Dispatch(new SetCount(c));
}
}
Razor view
@using Memento.Sample.Blazor.Stores
@using System.Text.Json
@page "/counter"
@inherits ObserverComponet
@inject AsyncCounterStore AsyncCounterStore
<PageTitle>Counter</PageTitle>
<h1>Async Counter</h1>
<p role="status">Current count: @AsyncCounterStore.State.Count</p>
<p role="status">Loading: @AsyncCounterStore.State.IsLoading</p>
<p role="status" class="mb-0">History</p>
<div class="d-flex">
[
@foreach (var item in string.Join(", ", AsyncCounterStore.State.Histories)) {
@item
}
]
</div>
<button class="mt-3 btn btn-primary" @onclick="IncrementCount">Count up</button>
<button class="mt-3 btn btn-primary" @onclick="CountupMany">Count up 10000 times</button>
@code {
async Task IncrementCount() {
await this.AsyncCounterStore.CountUpAsync();
}
void CountupMany() {
this.AsyncCounterStore.CountUpManyTimes(10000);
}
}
Compatibility and bindings
Package Name | Version | Lang | Platform | Package manager | Release Notes | Package provider |
---|---|---|---|---|---|---|
Memento.Core | 1.0.0 | C# | .NET 6 or later | NuGet | Notes | NuGet |
Memento.Blazor | 1.0.0 | Blazor | .NET 6 or later | NuGet | Notes | NuGet |
Documentation
License
Designed with ♥ by le-nn. Licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Memento.Core:
Package | Downloads |
---|---|
Memento.Blazor
Easy unidirectional store and red/undo library for state management for frontend apps on Blazor/.NET |
|
Memento.ReduxDevTool
Easy unidirectional store and red/undo library for state management for frontend apps on Blazor/.NET |
|
Memento.ReduxDevTool.Browser
Easy unidirectional store and red/undo library for state management for frontend apps on Blazor/.NET |
|
Memento.ReduxDevTool.Remote
Easy unidirectional store and red/undo library for state management for frontend apps on Blazor/.NET |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
1.7.0 | 532 | 2/19/2024 | |
1.6.0 | 368 | 11/21/2023 | |
1.5.0 | 322 | 7/21/2023 | |
1.4.1 | 418 | 3/31/2023 | |
1.4.0 | 405 | 3/23/2023 | |
1.3.1 | 610 | 2/21/2023 | |
1.1.0 | 503 | 2/21/2023 | |
1.0.0 | 601 | 1/5/2023 | |
0.2.0 | 475 | 11/13/2022 | |
0.1.0 | 652 | 9/5/2022 | |
0.0.5 | 561 | 8/8/2022 | |
0.0.4 | 462 | 7/11/2022 | |
0.0.3 | 452 | 7/11/2022 | |
0.0.2 | 478 | 7/6/2022 | |
0.0.1 | 449 | 7/5/2022 |