2019-06-27 18:21:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
_ "github.com/dannypsnl/redux"
|
|
|
|
"github.com/dannypsnl/redux/rematch"
|
|
|
|
"github.com/dannypsnl/redux/store"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
Reducer *Model
|
|
|
|
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-06-27 18:21:51 +00:00
|
|
|
Reducer := &Model{
|
|
|
|
State: PeersState{
|
|
|
|
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
|
|
|
}
|
|
|
|
func (s *State) GetState() PeersState {
|
|
|
|
return s.Store.StateOf(s.Reducer).(PeersState)
|
|
|
|
}
|
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
|
|
|
|
}
|