RG.Redux 1.0.4

dotnet add package RG.Redux --version 1.0.4                
NuGet\Install-Package RG.Redux -Version 1.0.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RG.Redux" Version="1.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RG.Redux --version 1.0.4                
#r "nuget: RG.Redux, 1.0.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RG.Redux as a Cake Addin
#addin nuget:?package=RG.Redux&version=1.0.4

// Install RG.Redux as a Cake Tool
#tool nuget:?package=RG.Redux&version=1.0.4                

RG.Redux

NuGet

A minimal Redux implementation

Creating Events, Reducer, and Store

// Events
public interface IFooEvent : IEvent { }
public record FooIncremented() : IFooEvent;
public record FooDecremented() : IFooEvent;
public record FooIncrementedBy(int Value) : IFooEvent;
public record FooReset() : IFooEvent;

// Store and Reducer
public record FooStore() : Store<int, IFooEvent>(
    reducer: (state, action) =>
        action switch {
            FooIncremented => state + 1,
            FooDecremented => state - 1,
            FooIncrementedBy { Value: var val } => state + val,
            FooReset => 0,
            _ => state
        }.
    initialState: 0
);

// Creating a Store instance
var fooStore = new FooStore();

Reading State

var state = fooStore.State;

Dispatching Event

fooStore.Dispatch(new FooDecremented());

Subscribing to Store updates

var subscription = fooStore.Subscribe(value => {
    Console.WriteLine(value);
});

Unsubscribing from Store updates

subscription.Dispose();

Querying Store updates using Reactive.Linq

var query = from value in fooStore
            where value % 2 == 0
            select value / 2;

var subscription = query.Subscribe(value => {
    Console.WriteLine(value);
});

Don't need event types?

Just mutate the State property

public record FooStore() : Store<int>(initialState: 0) {
    public void Increment() => State = State + 1;
    public void Decrement() => State = State - 1;
    public void IncrementBy(int x) => State = State + x;
    public void Reset() => State = 0;
}
Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 1,442 1/6/2023
1.0.3 325 1/6/2023
1.0.2 297 1/6/2023
1.0.1 327 12/10/2022
1.0.0 277 12/10/2022