ReduxSimple 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ReduxSimple --version 1.1.0                
NuGet\Install-Package ReduxSimple -Version 1.1.0                
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="ReduxSimple" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ReduxSimple --version 1.1.0                
#r "nuget: ReduxSimple, 1.1.0"                
#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 ReduxSimple as a Cake Addin
#addin nuget:?package=ReduxSimple&version=1.1.0

// Install ReduxSimple as a Cake Tool
#tool nuget:?package=ReduxSimple&version=1.1.0                

Redux Simple

Simple Stupid Redux Store using Reactive Extensions

Redux Simple is a .NET library based on Redux principle. Redux Simple is written with Rx.NET and built with the minimum of code you need to scale your whatever .NET application you want to design.

Getting started

Like the original Redux library, you will have to initialize a new State when creating a Store + you will create Reduce functions each linked to an Action which will possibly update this State.

In your app, you can:

  • Dispatch new Action to change the State
  • and listen to events/changes using the Subscribe method

We will go through using an example.

The default State

Each State should immutable. That's why we prefer to use immutable types for each property of the State.

public class AppState
{
    public string CurrentPage { get; set; } = string.Empty;
    public ImmutableArray<string> Pages { get; set; } = ImmutableArray<string>.Empty;
}

A simple Store

You will need 3 steps to create your own Redux Store:

  1. Create Actions used in the Reduce function
public class NavigateAction
{
    public string PageName { get; set; }
}

public class GoBackAction { }

public class ResetAction { }
  1. Create a new Store class inherited from ReduxStore
public sealed class MyAppStore : ReduxStore<AppState>
{
    public override AppState Reduce(AppState state, object action)
    {
        switch (action)
        {
            case NavigateAction navigateAction:
                return Reduce(state, navigateAction);
            case GoBackAction goBackAction:
                return Reduce(state, goBackAction);
            case ResetAction resetAction:
                return Reduce(state, resetAction);
        }

        return base.Reduce(state, action);
    }

    private static AppState Reduce(AppState state, NavigateAction action)
    {
        return new AppState
        {
            CurrentPage = action.PageName,
            Pages = state.Pages.Add(action.PageName)
        };
    }
    private static AppState Reduce(AppState state, GoBackAction action)
    {
        var newPages = state.Pages.RemoveAt(state.Pages.Length - 1);

        return new AppState
        {
            CurrentPage = newPages.LastOrDefault(),
            Pages = newPages
        };
    }
    private static AppState Reduce(AppState state, ResetAction action)
    {
        return new AppState
        {
            CurrentPage = string.Empty,
            Pages = ImmutableArray<string>.Empty
        };
    }
}
  1. Create a new instance of your Store
sealed partial class App
{
    public static readonly MyAppStore Store;

    static App()
    {
        Store = new MyAppStore();
    }
}

Dispatch & Subscribe

You can now dispatch new actions using your globally accessible Store.

using static MyApp.App; // static reference on top of your file

Store.Dispatch(new NavigateAction { PageName = "Page1" });
Store.Dispatch(new NavigateAction { PageName = "Page2" });
Store.Dispatch(new GoBackAction());

And subscribe to either state changes or actions raised.

using static MyApp.App; // static reference on top of your file

Store.ObserveAction<NavigateAction>().Subscribe(_ =>
{
    // TODO : Handle navigation
});

Store.ObserveState()
    .Where(state => state.CurrentPage == nameof(Page1))
    .Subscribe(_ =>
    {
        // TODO : Handle event when the current page is now "Page1"
    });

Based on what you need, you can observe the entire state or just a part of it.

Store.ObserveState()
    .Subscribe(state =>
    {
        // Listening to the full state (when any property changes)
    });

Store.ObserveState(state => state.CurrentPage)
    .Subscribe(currentPage =>
    {
        // Listening to the "CurrentPage" property of the state (when only this property changes)
    });

Store.ObserveState(state => new { state.CurrentPage, state.Errors })
    .Subscribe(x =>
    {
        // Listening to few properties of the state (when any of these properties changes)
    });

Asynchronous Actions

When you work with asynchronous tasks (side effects), you can follow the following rule:

  • Create 3 actions - a normal/start action, a fulfilled action and a failed action
  • Reduce/Handle response on fulfilled action
  • Reduce/Handle error on failed action

Here is a concrete example.

List of actions
public class GetTodosAction { }
public class GetTodosFulfilledAction
{
    public ImmutableArray<Todo> Todos { get; set; }
}
public class GetTodosFailedAction
{
    public int StatusCode { get; set; }
    public string Reason { get; set; }
}
Store.Dispatch(new GetTodosAction());
Reduce functions
private static AppState Reduce(AppState state, GetTodosAction action)
{
    return new AppState
    {
        Loading = true,
        Todos = state.Todos
    };
}
private static AppState Reduce(AppState state, GetTodosFulfilledAction action)
{
    return new AppState
    {
        Loading = false,
        Todos = action.Todos.ToImmutableArray()
    };
}
private static AppState Reduce(AppState state, GetTodosFailedAction action)
{
    return new AppState
    {
        Loading = false,
        Todos = ImmutableArray<Todo>.Empty
    };
}

Time travel / History

The simpliest version of a Redux Store is by using the ReduxStore class. You can however use the ReduxStoreWithHistory class to implement a Store with time travel feature : handling Undo and Redo actions.

Go back in time...

When you there are stored actions (ie. actions of the past), you can go back in time.

if (Store.CanUndo)
{
    Store.Undo();
}

It will then fires an UndoneAction event you can subscribe to.

Store.ObserveState()
    .Subscribe(_ =>
    {
        // TODO : Handle event when the State changed 
        // You can observe the previous state generated or...
    });

Store.ObserveUndoneAction()
    .Subscribe(_ =>
    {
        // TODO : Handle event when an Undo event is triggered 
        // ...or you can observe actions undone
    });
...And then rewrite history

Once you got back in time, you have two choices:

  1. Start a new timeline
  2. Stay on the same timeline of events
Start a new timeline

Once you dispatched a new action, the new State is updated and the previous timeline is erased from history: all previous actions are gone.

// Dispatch the next actions
Store.Dispatch(new NavigateAction { PageName = "Page1" });
Store.Dispatch(new NavigateAction { PageName = "Page2" });

if (Store.CanUndo)
{
    // Go back in time (Page 2 -> Page 1)
    Store.Undo();
}

// Dispatch a new action (Page 1 -> Page 3)
Store.Dispatch(new NavigateAction { PageName = "Page3" });
Stay on the same timeline of events

You can stay o nthe same timeline by dispatching the same set of actions you did previously.

// Dispatch the next actions
Store.Dispatch(new NavigateAction { PageName = "Page1" });
Store.Dispatch(new NavigateAction { PageName = "Page2" });

if (Store.CanUndo)
{
    // Go back in time (Page 2 -> Page 1)
    Store.Undo();
}

if (Store.CanRedo)
{
    // Go forward (Page 1 -> Page 2)
    Store.Redo();
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (7)

Showing the top 5 NuGet packages that depend on ReduxSimple:

Package Downloads
ReduxSimple.Entity

Simple Stupid Redux Store using Reactive Extensions

Rownd.Xamarin

Integrate simple, frictionless authentication into your Xamarin app.

ReduxSimple.DevTools

Simple Stupid Redux Store using Reactive Extensions

ReduxSimple.Uwp.DevTools

Simple Stupid Redux Store using Reactive Extensions - DevTools for UWP applications

ReduxSimple.Uwp.RouterStore

Simple Stupid Redux Store using Reactive Extensions - Binding between Store and Routing in UWP applications

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.7.0 19,776 12/20/2021
3.6.1 5,410 4/19/2021
3.6.0 1,159 2/15/2021
3.5.2 1,010 1/13/2021
3.5.1 3,718 10/16/2020
3.5.0 549 10/10/2020
3.4.0 1,996 4/24/2020
3.3.0 561 4/16/2020
3.2.0 590 4/16/2020
3.1.0 654 2/26/2020
3.0.1 730 1/25/2020
3.0.0 682 11/11/2019
2.1.0 663 9/9/2019
2.0.1 3,067 5/30/2019
2.0.0 2,273 12/22/2018
1.2.0 1,002 5/21/2018
1.1.1 994 4/29/2018
1.1.0 1,110 4/28/2018
1.0.0 1,128 1/22/2018