a predictable, immutable state container in go
Go
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.travis.yml
LICENSE
README.md
gredux.go
gredux_test.go

README.md

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/avahowell/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)