GithubHelp home page GithubHelp logo

odonno / reduxsimple Goto Github PK

View Code? Open in Web Editor NEW
143.0 8.0 19.0 1.83 MB

Simple Stupid Redux Store using Reactive Extensions

Home Page: http://www.nuget.org/packages/ReduxSimple/

License: MIT License

C# 100.00%
redux-store history dispatch redux csharp dotnet reactive-programming state state-management reducers

reduxsimple's Introduction

./images/logo.png

Redux Simple

CodeFactor

Package Versions
ReduxSimple NuGet
ReduxSimple.Entity NuGet
ReduxSimple.Uwp NuGet
ReduxSimple.Uwp.RouterStore NuGet
ReduxSimple.Uwp.DevTools NuGet

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.

Example app

There is a sample UWP application to show how ReduxSimple library can be used and the steps required to make a C#/XAML application using the Redux pattern.

You can follow this link: https://www.microsoft.com/store/apps/9PDBXGFZCVMS

Getting started

Like the original Redux library, you will have to initialize a new State when creating a Store + you will create Reducer 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

You will need to follow the following steps to create your own Redux Store:

  1. Create State definition
public record RootState
{
    public string CurrentPage { get; set; } = string.Empty;
    public ImmutableArray<string> Pages { get; set; } = ImmutableArray<string>.Empty;
}

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

  1. Create Action definitions
public class NavigateAction
{
    public string PageName { get; set; }
}

public class GoBackAction { }

public class ResetAction { }
  1. Create Reducer functions
public static class Reducers
{
    public static IEnumerable<On<RootState>> CreateReducers()
    {
        return new List<On<RootState>>
        {
            On<NavigateAction, RootState>(
                (state, action) => state with { Pages = state.Pages.Add(action.PageName) }
            ),
            On<GoBackAction, RootState>(
                state =>
                {
                    var newPages = state.Pages.RemoveAt(state.Pages.Length - 1);

                    return state with {
                        CurrentPage = newPages.LastOrDefault(),
                        Pages = newPages
                    };
                }
            ),
            On<ResetAction, RootState>(
                state => state with {
                    CurrentPage = string.Empty,
                    Pages = ImmutableArray<string>.Empty
                }
            )
        };
    }
}
  1. Create a new instance of your Store
sealed partial class App
{
    public static readonly ReduxStore<RootState> Store;

    static App()
    {
        Store = new ReduxStore<RootState>(CreateReducers());
    }
}
  1. And be ready to use your store inside your entire application...

Features

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.Select(state => state.CurrentPage)
    .Where(currentPage => currentPage == nameof(Page1))
    .UntilDestroyed(this)
    .Subscribe(_ =>
    {
        // TODO : Handle event when the current page is now "Page1"
    });
Reducers

Reducers are pure functions used to create a new state once an action is triggered.

Reducers on action

You can define a list of On functions where at least one action can be triggered.

return new List<On<RootState>>
{
    On<NavigateAction, RootState>(
        (state, action) => state with { Pages = state.Pages.Add(action.PageName) }
    ),
    On<GoBackAction, RootState>(
        state =>
        {
            var newPages = state.Pages.RemoveAt(state.Pages.Length - 1);

            return state with {
                CurrentPage = newPages.LastOrDefault(),
                Pages = newPages
            };
        }
    ),
    On<ResetAction, RootState>(
        state => state with {
            CurrentPage = string.Empty,
            Pages = ImmutableArray<string>.Empty
        }
    )
};

Sub-reducers aka feature reducers

Sub-reducers also known as feature reducers are nested reducers that are used to update a part of the state. They are mainly used in larger applications to split state and reducer logic in multiple parts.

The CreateSubReducers function helps you to create sub-reducers. This function has a few requirements:

  • a Selector - to be able to access the value of the current nested state
  • a Reducer - to explicitly detail how to update the parent state given a new value for the nested state
  • and the list of reducers using On pattern

First you need to create a new state lens for feature/nested states:

public static IEnumerable<On<RootState>> GetReducers()
{
    return CreateSubReducers(SelectCounterState)
        .On<IncrementAction>(state => state with { Count = state.Count + 1 })
        .On<DecrementAction>(state => state with { Count = state.Count - 1 })
        .ToList();
}

Then you can combine nested reducers into your root state:

public static IEnumerable<On<RootState>> CreateReducers()
{
    return CombineReducers(
        Counter.Reducers.GetReducers(),
        TicTacToe.Reducers.GetReducers(),
        TodoList.Reducers.GetReducers(),
        Pokedex.Reducers.GetReducers()
    );
}

And so inject your reducers into the Store:

public static readonly ReduxStore<RootState> Store =
    new ReduxStore<RootState>(CreateReducers(), RootState.InitialState);

Remember that following this pattern, you can have an infinite number of layers for your state.

Selectors

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

Note that every selector is a memoized selector by design, which means that a next value will only be subscribed if there is a difference with the previous value.

Full state

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

Inline function

You can use functions to select a part of the state, like this:

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

Simple selectors

Simple selectors are like functions but the main benefits are that they can be reused in multiple components and they can be reused to create other selectors.

public static ISelectorWithoutProps<RootState, string> SelectCurrentPage = CreateSelector(
    (RootState state) => state.CurrentPage
);
public static ISelectorWithoutProps<RootState, ImmutableArray<string>> SelectPages = CreateSelector(
    (RootState state) => state.Pages
);

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

Reuse selectors - without props

Note that you can combine multiple selectors to create a new one.

public static ISelectorWithoutProps<RootState, bool> SelectHasPreviousPage = CreateSelector(
    SelectPages,
    (ImmutableArray<string> pages) => pages.Count() > 1
);

Reuse selectors - with props

You can also use variables out of the store to create a new selector.

public static ISelectorWithProps<RootState, string, bool> SelectIsPageSelected = CreateSelector(
    SelectCurrentPage,
    (string currentPage, string selectedPage) => currentPage == selectedPage
);

And then use it this way:

Store.Select(SelectIsPageSelected, "mainPage")
    .Subscribe(isMainPageSelected =>
    {
        // TODO
    });

Combine selectors

Sometimes, you need to consume multiple selectors. In some cases, you just want to combine them. This is what you can do with CombineSelectors function. Here is an example:

Store.Select(
    CombineSelectors(SelectGameEnded, SelectWinner)
)
    .Subscribe(x =>
    {
        var (gameEnded, winner) = x;

        // TODO
    });
Effects - Asynchronous Actions

Side effects are functions that runs outside of the predictable State -> UI cycle. Effects does not interfere with the UI directly and can dispatch a new action in the ReduxStore when necessary.

The 3-actions pattern

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

  • Create 3 actions - a 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.

public class GetTodosAction { }
public class GetTodosFulfilledAction
{
    public ImmutableList<Todo> Todos { get; set; }
}
public class GetTodosFailedAction
{
    public int StatusCode { get; set; }
    public string Reason { get; set; }
}
Store.Dispatch(new GetTodosAction());

Create and register effect

You now need to observe this action and execute an HTTP call that will then dispatch the result to the store.

public static Effect<RootState> GetTodos = CreateEffect<RootState>(
    () => Store.ObserveAction<GetTodosAction>()
        .Select(_ =>
            _todoApi.GetTodos()
                .Select(todos =>
                {
                    return new GetTodosFulfilledAction
                    {
                        Todos = todos.ToImmutableList()
                    };
                })
                .Catch(e =>
                {
                    return Observable.Return(
                        new GetTodosFailedAction
                        {
                            StatusCode = e.StatusCode,
                            Reason = e.Reason
                        }
                    );
                })
        )
        .Switch(),
    true // indicates if the ouput of the effect should be dispatched to the store
);

And remember to always register your effect to the store.

Store.RegisterEffects(
    GetTodos
);
Time travel

By default, ReduxStore only support the default behavior which is a forward-only state. You can however set enableTimeTravel to true in order to debug your application with some interesting features: handling Undo and Redo actions.

Enable time travel

sealed partial class App
{
    public static readonly ReduxStore<RootState> Store;

    static App()
    {
        Store = new ReduxStore<RootState>(CreateReducers(), true);
    }
}

Go back in time...

When the Store contains 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.Select()
    .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();
}
Reset state

You can also reset the entire Store (reset current state and list of actions) by using the following method.

Store.Reset();

You can then handle the reset event on your application.

Store.ObserveReset()
    .Subscribe(_ =>
    {
        // TODO : Handle event when the Store is reset
        // (example: flush navigation history and restart from login page)
    });
Entity management (in preview)

When dealing with entities, you often repeat the same process to add, update and remove entity from your collection state. With the ReduxSimple.Entity package, you can simplify the management of entities using the following pattern:

  1. Start creating an EntityState and an EntityAdapter
public record TodoItemEntityState : EntityState<int, TodoItem>
{
}

public static class Entities
{
    public static EntityAdapter<int, TodoItem> TodoItemAdapter = EntityAdapter<int, TodoItem>.Create(item => item.Id);
}
  1. Use the EntityState in your state
public record TodoListState
{
    public TodoItemEntityState Items { get; set; }
    public TodoFilter Filter { get; set; }
}
  1. Then use the EntityAdapter in reducers
On<CompleteTodoItemAction, TodoListState>(
    (state, action) =>
    {
        return state with
        {
            Items = TodoItemAdapter.UpsertOne(new { action.Id, Completed = true }, state.Items)
        };
    }
)
  1. And use the EntityAdapter in selectors
private static readonly ISelectorWithoutProps<RootState, TodoItemEntityState> SelectItemsEntityState = CreateSelector(
    SelectTodoListState,
    state => state.Items
);
private static readonly EntitySelectors<RootState, int, TodoItem> TodoItemSelectors = TodoItemAdapter.GetSelectors(SelectItemsEntityState);
public static ISelectorWithoutProps<RootState, List<TodoItem>> SelectItems = TodoItemSelectors.SelectEntities;
Router (in preview)

You can observe router changes in your own state. You first need to create a State which inherits from IBaseRouterState.

public class RootState : IBaseRouterState
{
    public RouterState Router { get; set; }

    public static RootState InitialState =>
        new RootState
        {
            Router = RouterState.InitialState
        };
}

For UWP

In order to get router information, you need to enable the feature like this (in App.xaml.cs):

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    // TODO : Initialize rootFrame

    // Enable router store feature
    Store.EnableRouterFeature(rootFrame);
}
Redux DevTools (in preview)

./images/devtools.PNG

Sometimes, it can be hard to debug your application. So there is a perfect tool called Redux DevTools which help you with that:

  • list all dispatched actions
  • payload of the action and details of the new state after dispatch
  • differences between previous and next state
  • replay mechanism (time travel)

For UWP

In order to make the Redux DevTools work, you need to enable time travel.

public static readonly ReduxStore<RootState> Store =
    new ReduxStore<RootState>(CreateReducers(), RootState.InitialState, true);

And then display the Redux DevTools view using a separate window.

await Store.OpenDevToolsAsync();

reduxsimple's People

Contributors

akaegi avatar mhusainisurge avatar odonno avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

reduxsimple's Issues

Improve Redux DevTools

  • Improve action/state design (like in the original redux devtools)
  • Show diff state (between previous and new state)
  • Save/Restore (Download/Upload) state (with a json file)

Add XML doc

Add XML doc on C# class, fields, properties and methods. At least on each publicly visible member.

Create a Redux DevTools component

  • Create a UWP component to be used as-is or in a separate window (picture-in-picture)
    • Display a list of all actions in the store
    • Show current action selected (name + payload)
    • Show state related to the action (output state)
    • Show diff state (between previous and new state)
    • Time travelling (back/forward/reset/slider)
    • Save/Restore state (with a json file)
  • Update sample app to use Redux DevTools component in a separate window (picture-in-picture)
  • Publish a NuGet package for UWP

Documentation for 1.1

Provide documentation on the partial state observation:

  • Documentation on how to Observe a single object property
  • Documentation on how to Observe multiple properties

Provide documentation on the ReduxStoreWithHistory class:

  • Undo operation
  • Redo operation
  • Subscribe to Undone actions

Provide documentation on Reset() method:

  • Documentation on Reset

Add list of contributions:

  • List contributions made by contributors on readme file

Create example projects

Samples will be writen in UWP

  • Counter app
  • Tic Tac Toe game
  • ToDo list app
  • Pokedex app using a distant API (with cancellation feature)
  • Create a UI component to navigate through history
  • Link sample app to readme file

Contributors guide

  • Fill XML documentation for public properties, methods, etc..
  • CI is successful
  • A score on each file listened by codefactor

Filter actions based on where they came from (ObserveAction)

In order to make the sample app work, I need to dispatch action to start asynchronous task.

But the thing is that if using the History, the asynchronous break the existing timeline. So, there is different strategies to fix that:

  1. Add a boolean to the result of the observable to tell if the action is dispatched normally or if it comes from the History
  2. Or add a filter enum/boolean on ObserveAction to be able to observe actions only if dispatched in a normal app (not from history)

Why do you use "where T : class" in ObserveAction<T>?

According to System.Reactive.Linq, the extension method OfType has no bounds - it can be of any type. So, is there any specific reason why you use "where T : class" in the ObserveAction functions where the only generic part is OfType ?

For example, it prevents using struct, or enum as action. Maybe you intended to use action only as a reference type and not as a struct, enum, etc. Just want to know. Thanks

Combine selectors

CombineSelectors function for combined selectors (use CombineLatest operator + Tuple.Create on every selector)

Encapsulate Reduce method

Does the Reduce method need to be public? I suppose there's no harm, but there seems to be no real need to expose it by default.

Selectors

Add ability to create selectors with Rx.NET in order to optimize applications and avoid repeatable code.

  • Memoized selectors (all selectors should be memoized)
  • 2 to N selector/map functions in parameters
  • Selectors with props parameter

Example:

Store.Select(SelectRecentlyWatchedMovies, 20)
    .Subscribe(...);

Hide Dispatch method member

With the new ReduxStoreWithHistory class, I noticed that we can override both the Dispatch and Reduce methods in derived class.

The first behavior of the Store class was to only allow overriding of Reduce function because the Dispatch function is internal and should remain as so.

So, I propose to use new instead of override keyword.

Use a functional component approach for sample app

In order to make XAML application easier/faster to create with a Redux pattern, we should consider a functional component approach for XAML components using a React-like paradigm.

Some ideas:

  • virtual dom but for XAML (virtual-xaml?)
  • components written entirely in C# (to enable reuse/composition/HOC)
  • JSX for C#/XAML combo (*.csaml files?)
    • merge both world (convert XAML into C#/XamlDirect component?)

Enable stats

Each ReduxStore could track information such as stats. Questions remains:

  • Is it interesting to provide stats for developers?
  • What kind of stats should we track?
    • Number of dispatch per action?
    • Total number of actions dispatched?
    • Session duration based on the first action/store creation and last action dispatched?
    • Other?

Manage entities

Like @ngrx/entities, add an adapter function to handle list of entity. Using an ImmutableList<T>.

How would I await an effect?

I'm looking for something like:

await Store.DispatchAsync(new LoadMyEntitiesAction());
DoSomethingWhenTheAboveLineIsFinished();

I can't do:
await Store.Dispatch(new LoadMyEntitiesAction())
since Dispatch is returning void.

Maybe there's a concept I'm missing here?

Create Reset method

Add ability to reset the ReduxStore:

  • Actions collection should be empty
  • State should be reset to the original value

Routing store/state?

Is it possible to create a routing store package that can be used as-is for any application with a navigation strategy?

If possible, it can be built using https://ngrx.io/guide/router-store as an inspiration and it should provide:

  • a state object definition
  • actions dispatched on navigation events
  • selectors

Grouped memoized selector

Keyed memoized selector: memoize each selector value based on a key

Use GroupBy operator followed by a DistinctUntilChanged to memoize selector by each group/key.

Create Visual Studio templates for every kind of applications

Create a basic starting template for each kind of application so devs will have a default ReduxStore and they will ready to scale their app.

At least a basic Template for:

  • Console application
  • WPF application
  • UWP application
  • Xamarin application (iOS? Android? Forms?)
  • ASP.NET Core application (maybe not useful for backend server)
  • Blazor application (can be an advantage for web application written in C#)

Subscribe does not fire for initial State

I am having a singleton Store that i inject into my ViewModel.
When i navigate to it i am subscribing to changes in store like this.

Store
.ObserveState(x => x.IsEnabled)
.Subscribe()

My problem is that it does not fire for initial value, because you are using Subject<TState>() instead of BehaviorSubject<TState>(initialState). Is this by intention or not?
As of now, my only alternative is to use .StartWith(Store.State.IsEnabled), but this is not that pretty.

Add unit tests

Add unit tests to make sure the code functions as expected and continues to do so as it evolves.

Import/export state

Hydratation of the state using any kind of storage.

For UWP, two scenarii:

  • offline mode
  • app suspended/terminated (reuse state when app is reopened and possibly handle server synchronization)

Is there a simple way to test Equality on POCO object?

I added some unit tests in order to test the DistinctUntilChanged observable but one thing I notice is that if we observe the object directly, the distinct is made on the reference and not the properties so even if the state have unchanged property values, the observable considers the object as new and not distinct.

I solved it by overriding the Equals method.

public override bool Equals(object obj)

But it feels odd to override the method since the object can be used in other part of dev apps. So, my questions are :

  1. Is there an easy way to test POCO equality? Like an external library or with a simple function?
  2. How can we apply it in the DistinctUntilChanged method?

Maybe @mhusainisurge have an idea?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.