Giter Club home page Giter Club logo

gredux's Introduction

gredux

Go Report Card Build Status codecov GoDoc

gredux is a golang implementation of a redux-esque state container. The aim is to provide a structure for writing applications which have consistent, predictable behaviour.

Example Usage

import (
	"github.com/johnathanhowell/gredux"
)

// Create an initial state for the Store
type counterState struct {
	count int
}

// Instantiate a new store around this state
store := gredux.New(counterState{0})

// Create a reducer which increments "count" when it receives an "increment" 
// action, and decrements when it receives a "decrement" action.
store.Reducer(func(state gredux.State, action gredux.Action) gredux.State {
	switch action.ID {
	case "increment":
		return counterState{state.(counterState).count + action.Data.(int)}
	case "decrement":
		return counterState{state.(counterState).count - action.Data.(int)}
	default:
		return state
	}
})

store.Dispatch(Action{"increment", 5})
store.Dispatch(Action{"decrement", 2})

fmt.Println(store.State().(counterState).count) // prints 3

// Register a func to be called after each state update
store.AfterUpdate(func(state State) {
	fmt.Println(state.(counterState).count) // prints the count after every state update
})
store.Dispatch(Action{"decrement", 2})

License

The MIT License (MIT)

gredux's People

Contributors

avahowell avatar dvrkps avatar

Watchers

James Cloos avatar iapilgrim 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.