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.
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.AddReducer(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
})
// Register a hook for given action that will be invoked everytime that action is dispatched
// This hook must not dispatch another action otherwise deadlock will happen!
store.AddHook(func(state State) {
fmt.Println(state.(counterState).count) // prints the count after every state update
}, []string{"increment"})
store.Dispatch(Action{"decrement", 2})
The MIT License (MIT)