Giter Club home page Giter Club logo

bridge.redux's Introduction

Bridge.Redux - Wiki

Bindings of the Redux library for the Bridge transpiler. It provides a type-safe interface to interact with the redux library as well as DSL's to idiomatically create reducers from C#. thunk and logger middleware are built-in as well.

Examples

Installation

The easy and convenient way in to copy and paste the contents of Generated.Redux.cs in your project as a source file depecndency so you can tweak it however you like with no binary dependencies.

The Other way is to install it from nuget, download the latest prelease version (the one with "CI-{version}" tag)

Usage

First define your state object, in our case it is a simple counter

// State (object), it should to be a simple object literal that only holds data
// its properties however, could be anything. 
[ObjectLiteral]
class Counter
{
    public int Value { get; set; }
}

Then define the actions upon which the reduction is based

// Actions
public class IncrementValue { }
public class DecrementValue { }

// Use fields instead of properties because properties will be translated to getter and setter methods
public class UpdateTodo 
{
    public int Id;
    public string Description;
}

Now for actually managing the state, lets create a counter reducer, Bridge.Redux provides a clean DSL to compose lambda's into a reducer based on the types of actions

var counterReducer = 
    BuildReducer
      .For<Counter>() 
      .WhenActionHasType<IncrementValue>((state, action) =>
      {
          return new Counter { Value = state.Value + 1 };
      }) 
      .WhenActionHasType<DecrementValue>((state, action) =>
      {
          return new Counter { Value = state.Value - 1 };
      })
      .Build();

Create your store

var initialState = new Counter { Value = 0 };
var store = Redux.CreateStore(counterReducer, initialState);

Subscribe (listen) to store dispatches

store.Subscribe(() => Console.WriteLine($"Current value => {store.GetState().Value}"));

Finally dispatch actions

store.Dispatch(new IncrementValue());
// Current value => 1
store.Dispatch(new IncrementValue());
// Current value => 2
store.Dispatch(new IncrementValue());
// Current value => 3

There you have it, a working redux app, utilizing C#'s type-system :)

bridge.redux's People

Contributors

zaid-ajaj avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.