go-store
is a state holder in Redux pattern.
The State is read-only, the Changes are made with reducer in uni-directional way
Store
holds a state.
Action
is delivered to the store to change the state.
Reducer
changes the state with Action.
Store delivers the change to Subscriber
It can be installed by:
go get github.com/rookiecj/go-store
how to use as follows:
import (
"fmt"
"time"
"github.com/rookiecj/go-store/store"
)
type myState struct {
id int
value string
}
type addAction struct {
value string
}
func (c myState) StateInterface() {}
func (c *addAction) ActionInterface() {}
func main() {
initialState := myState{}
reducer := func(state myState, action store.Action) myState {
switch action.(type) {
case *addAction:
reifiedAction := action.(*addAction)
return myState{
id: state.id,
value: state.value + reifiedAction.value,
}
}
return initialState
}
stateStore := store.NewStore[myState](initialState, reducer)
stateStore.Subscribe(func(newState myState, oldState myState, action store.Action) {
fmt.Println("subscriber1", newState)
})
stateStore.Subscribe(func(newState myState, oldState myState, action store.Action) {
fmt.Println("subscriber2", newState)
})
stateStore.Dispatch(&addAction{
value: "1",
})
stateStore.Dispatch(&addAction{
value: "2",
})
stateStore.Dispatch(&addAction{
value: "3",
})
// store.waitForDispatch()
time.Sleep(100 * time.Millisecond)
}
- make sure all subscribers notified
- add Store callbacks like onFirstSubscribe
- add SubscribeOn
- support Main/Background/Dispatch Scheduler
- make age precisely
- add AddReducer
- add README
- add doc
- add more testing