2019-06-27 18:21:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
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-06-27 18:21:51 +00:00
|
|
|
type State struct {
|
2019-07-02 19:59:11 +00:00
|
|
|
Reducer *AppModel
|
2019-06-27 18:21:51 +00:00
|
|
|
Store *store.Store
|
2019-06-27 19:09:02 +00:00
|
|
|
Client *StatusGoClient
|
2019-06-27 18:21:51 +00:00
|
|
|
updatePeers *rematch.Action
|
|
|
|
setCurrent *rematch.Action
|
|
|
|
}
|
|
|
|
|
2019-06-27 19:09:02 +00:00
|
|
|
func NewState(client *StatusGoClient) *State {
|
2019-06-27 19:50:10 +00:00
|
|
|
// Generate the reducer from our model.
|
2019-07-02 19:59:11 +00:00
|
|
|
Reducer := &AppModel{
|
|
|
|
State: AppState{
|
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-06-27 18:21:51 +00:00
|
|
|
return &State{
|
|
|
|
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
|
|
|
// Client for RPC calls.
|
2019-06-27 19:09:02 +00:00
|
|
|
Client: client,
|
2019-06-27 19:50:10 +00:00
|
|
|
// Define available reducers for the store.
|
2019-06-27 18:21:51 +00:00
|
|
|
updatePeers: Reducer.Action(Reducer.Update),
|
|
|
|
setCurrent: Reducer.Action(Reducer.Current),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 19:50:10 +00:00
|
|
|
// Helpers for shorter calls.
|
2019-06-27 18:21:51 +00:00
|
|
|
func (s *State) Update(peers []Peer) {
|
|
|
|
s.Store.Dispatch(s.updatePeers.With(peers))
|
|
|
|
}
|
2019-06-27 19:09:02 +00:00
|
|
|
func (s *State) GetCurrent() *Peer {
|
|
|
|
state := s.GetState()
|
|
|
|
if state.Current == -1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &state.Peers[state.Current]
|
|
|
|
}
|
|
|
|
func (s *State) SetCurrent(index int) {
|
|
|
|
s.Store.Dispatch(s.setCurrent.With(index))
|
2019-06-27 18:21:51 +00:00
|
|
|
}
|
2019-07-02 19:59:11 +00:00
|
|
|
func (s *State) GetState() AppState {
|
|
|
|
return s.Store.StateOf(s.Reducer).(AppState)
|
2019-06-27 18:21:51 +00:00
|
|
|
}
|
2019-06-27 19:50:10 +00:00
|
|
|
|
|
|
|
// For fetching current state of peers from status-go
|
2019-06-27 19:09:02 +00:00
|
|
|
func (s *State) Fetch() {
|
|
|
|
peers, err := s.Client.getPeers()
|
2019-06-27 18:21:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
ps := s.GetState()
|
|
|
|
s.Update(peers)
|
2019-06-27 19:09:02 +00:00
|
|
|
if ps.Current == -1 {
|
|
|
|
s.SetCurrent(0)
|
2019-06-27 18:21:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 19:50:10 +00:00
|
|
|
// For removing a selected peer from connected to status-go
|
2019-06-27 19:09:02 +00:00
|
|
|
func (s *State) Remove(peer *Peer) error {
|
|
|
|
success, err := s.Client.removePeer(peer.Enode)
|
2019-06-27 18:21:51 +00:00
|
|
|
if err != nil || success != true {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
2019-06-27 19:09:02 +00:00
|
|
|
s.Fetch()
|
2019-06-27 18:21:51 +00:00
|
|
|
return nil
|
|
|
|
}
|