2019-06-27 18:21:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-06-28 20:27:16 +00:00
|
|
|
"github.com/dannypsnl/redux/v2/rematch"
|
|
|
|
"github.com/dannypsnl/redux/v2/store"
|
2019-06-27 18:21:51 +00:00
|
|
|
)
|
|
|
|
|
2019-06-27 19:50:10 +00:00
|
|
|
// This might need renaming, since it also contains the Client.
|
|
|
|
// I need the client to make the RPC calls.
|
2019-07-02 20:07:12 +00:00
|
|
|
type AppState struct {
|
2019-07-02 20:52:58 +00:00
|
|
|
Reducer *AppModel
|
|
|
|
Store *store.Store
|
|
|
|
setNodeInfo *rematch.Action
|
|
|
|
updatePeers *rematch.Action
|
|
|
|
setCurrentPeer *rematch.Action
|
2019-06-27 18:21:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 20:52:58 +00:00
|
|
|
func NewState() *AppState {
|
2019-06-27 19:50:10 +00:00
|
|
|
// Generate the reducer from our model.
|
2019-07-02 19:59:11 +00:00
|
|
|
Reducer := &AppModel{
|
2019-07-02 20:07:12 +00:00
|
|
|
State: AppData{
|
2019-06-27 18:21:51 +00:00
|
|
|
Peers: make([]Peer, 0),
|
2019-06-27 19:50:10 +00:00
|
|
|
Current: -1, // Should mean non selected.
|
2019-06-27 18:21:51 +00:00
|
|
|
},
|
|
|
|
}
|
2019-06-27 19:50:10 +00:00
|
|
|
// Instantiate the redux state from the reducer.
|
2019-07-02 20:07:12 +00:00
|
|
|
return &AppState{
|
2019-06-27 18:21:51 +00:00
|
|
|
Reducer: Reducer,
|
2019-06-27 19:50:10 +00:00
|
|
|
// Define the store.
|
2019-06-27 18:21:51 +00:00
|
|
|
Store: store.New(Reducer),
|
2019-06-27 19:50:10 +00:00
|
|
|
// Define available reducers for the store.
|
2019-07-02 20:52:58 +00:00
|
|
|
setNodeInfo: Reducer.Action(Reducer.SetInfo),
|
|
|
|
updatePeers: Reducer.Action(Reducer.Update),
|
|
|
|
setCurrentPeer: Reducer.Action(Reducer.Current),
|
2019-06-27 18:21:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 19:50:10 +00:00
|
|
|
// Helpers for shorter calls.
|
2019-07-02 20:52:58 +00:00
|
|
|
func (s *AppState) UpdateInfo(info NodeInfo) {
|
|
|
|
s.Store.Dispatch(s.setNodeInfo.With(info))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AppState) UpdatePeers(peers []Peer) {
|
2019-06-27 18:21:51 +00:00
|
|
|
s.Store.Dispatch(s.updatePeers.With(peers))
|
|
|
|
}
|
2019-07-02 20:52:58 +00:00
|
|
|
|
2019-07-02 20:07:12 +00:00
|
|
|
func (s *AppState) GetCurrent() *Peer {
|
2019-07-02 20:52:58 +00:00
|
|
|
state := s.GetData()
|
2019-06-27 19:09:02 +00:00
|
|
|
if state.Current == -1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &state.Peers[state.Current]
|
|
|
|
}
|
2019-06-27 19:50:10 +00:00
|
|
|
|
2019-07-02 20:52:58 +00:00
|
|
|
func (s *AppState) SetCurrentPeer(index int) {
|
|
|
|
s.Store.Dispatch(s.setCurrentPeer.With(index))
|
2019-06-27 18:21:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 20:52:58 +00:00
|
|
|
func (s *AppState) GetData() AppData {
|
|
|
|
return s.Store.StateOf(s.Reducer).(AppData)
|
2019-06-27 18:21:51 +00:00
|
|
|
}
|