State Management for Go Backend applications inspired in Redux.
╔═════════╗ ╔══════════╗ ╔═══════════╗ ╔═════════════════╗ ║ Action ║──────>║ Reducer ║ ────> ║ Store ║ ────> ║ Application ║ ╚═════════╝ ╚══════════╝ ╚═══════════╝ ╚═════════════════╝ ^ │ └────────────────────────────────────────────────────────────┘
- Go:
go get github.com/luisvinicius167/godux
godux turns your data flow unidirectional:
- Create actions as pure functions.
- The Store dispatches actions.
- Return new Value based on your Store State.
- Application State is held in the store, as a single map.
- State is ready-only.
- Changes are made with pure functions.
A Store is basically a container that holds your application state.
store := godux.NewStore()
store.Setstate("count", 1)
store.Setstate("Title", "I like godux!")
Actions are just pure functions. Your Actions functions always return a godux.Action.
increment := func(number int) godux.Action {
return godux.Action{
Type: "INCREMENT",
Value: number,
}
}
Like Redux Concept: "Actions describe the fact that something happened, but don’t specify how the application’s state changes in response. This is the job of a reducer."
// reducer function
reducer := func(action godux.Action) interface{} {
switch action.Type {
case "INCREMENT":
return store.GetState("count").(int) + action.Value.(int)
case "DECREMENT":
return action.Value.(int) - store.GetState("count").(int)
default:
return store.GetAllState()
}
}
// Add your reducer function to return new values basend on your state
store.Reducer(reducer)
Dispatch an action is very easy.
// Receive new value
newCount := store.Dispatch(increment(1)) // return 2
-
- `` godux.newStore() ```: Create a single store with the state of your application.
- `` godux.SetState(name string, value interface{}) ```: Sets the state store.
- `` godux.GetState(name string) ```: Return your Store state value.
- `` godux.GetAllState() ```: Return whole state as a map.
-
store.Reducer(func(action godux.Action))
: Add the reducer function to your Store.
-
store.Dispatch(action godux.Action)
: Dispatch your action to your Reducer.
-
godux.Action( Type string, Value interface{})
: Your application action.
MIT License.