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 main() {
initialState := myState{}
reducer := func(state myState, action store.Action) (myState, error) {
switch action.(type) {
case *addAction:
reifiedAction := action.(*addAction)
return myState{
id: state.id,
value: state.value + reifiedAction.value,
}, nil
}
return state, nil
}
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
- add README
- add doc
- support Main/Background/Dispatch Scheduler(Experimental)
- remove explicit scheduler start/stop
- add AsyncAction for async work
- add AddReducer
- Disposer for unsubscribe from store
- make getState public for subscribers not to save the state locally
- add State history
- make age precisely
- add more testing