# README
redux
This is a redux implementation in Go.
I hope this project can help you manage the complex update flow in Go.
Origin version
Install
$ go get https://github.com/dannypsnl/redux/...
Usage
pkgs
redux/store
New
Create New Store by reducers(at least one reducer)Dispatch
recieve then send action to every reducers to update state
And you should not callDispatch
in Subscribetor, you will get dead lock.Subscribe
recieve a func without args will be invoked by every next Dispatch
And you should not callSubscribe
in Subscribetor, you will get a panic warning.Marshal
return state as JSON format string
redux/action
By this module, we can have a better
New
recieve a string and return a pointer to Action for youAction
is a type containType
&Args
Type
is just a string help reducer juage what should them do.
Args
is amap[string]interface{}
contain a lot values, think about we Dispatch login Action
We need user & password to do this State update, so we will put user & password's value in the Action::Args
Again, only reducer should use Args, so cast is safety.Arg
help you append Argument onto Action with fluent API.
Example
Basic Example
Ignore other packages
import(
"github.com/dannypsnl/redux/store"
"github.com/dannypsnl/redux/action"
)
func counter(state interface{}, action action.Action) interface{} {
// Initial State
if state == nil {
return 0
}
switch action.Type {
case "INC":
return state.(int)+1
case "DEC":
return state.(int)-1
default:
return state
}
}
func main() {
store := store.New(counter) // counter state be initial at here, it's 0
// Subscribe's function will be invoke when Dispatch
store.Subscribe(func() {
fmt.Printf("Now state is %v\n", store.GetState("counter"))
fmt.Printf("%s\n", store.Marshal()) // `{ counter: 1 }`, 1 should be current state, Let's print out the json format of our store
})
store.Dispatch(action.New("INC")) // state increase by action, now is 1
}