rename State to AppState

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2019-07-02 16:07:12 -04:00 committed by Jakub
parent e678ee4d70
commit ecf69febbc
6 changed files with 20 additions and 19 deletions

View File

@ -4,7 +4,7 @@ import (
"time" "time"
) )
func FetchLoop(state *State, interval int) { func FetchLoop(state *AppState, interval int) {
// Get the first peers fetch going sooner // Get the first peers fetch going sooner
state.Fetch() state.Fetch()
// Then fetch every `interval` seconds // Then fetch every `interval` seconds

View File

@ -49,6 +49,7 @@ func main() {
// Create a state wrapper. // Create a state wrapper.
state := NewState(client) state := NewState(client)
// Subscribe rendering method to state changes. // Subscribe rendering method to state changes.
state.Store.Subscribe(GenRenderFunc(g, state)) state.Store.Subscribe(GenRenderFunc(g, state))

View File

@ -4,7 +4,7 @@ import (
"github.com/dannypsnl/redux/v2/rematch" "github.com/dannypsnl/redux/v2/rematch"
) )
type AppState struct { type AppData struct {
Node NodeInfo // info about current node Node NodeInfo // info about current node
Peers []Peer // list of peers for the node Peers []Peer // list of peers for the node
Current int // currently selected peer Current int // currently selected peer
@ -12,15 +12,15 @@ type AppState struct {
type AppModel struct { type AppModel struct {
rematch.Reducer rematch.Reducer
State AppState State AppData
} }
func (m *AppModel) SetInfo(s AppState, node NodeInfo) AppState { func (m *AppModel) SetInfo(s AppData, node NodeInfo) AppData {
s.Node = node s.Node = node
return s return s
} }
func (m *AppModel) Current(s AppState, peerIndex int) AppState { func (m *AppModel) Current(s AppData, peerIndex int) AppData {
// NOTE Not sure if I should just ignore invalid values or panic // NOTE Not sure if I should just ignore invalid values or panic
if peerIndex >= 0 && peerIndex < len(s.Peers) { if peerIndex >= 0 && peerIndex < len(s.Peers) {
s.Current = peerIndex s.Current = peerIndex
@ -28,7 +28,7 @@ func (m *AppModel) Current(s AppState, peerIndex int) AppState {
return s return s
} }
func (m *AppModel) Update(s AppState, peers []Peer) AppState { func (m *AppModel) Update(s AppData, peers []Peer) AppData {
// The argument is a copy so we can modify it and return it // The argument is a copy so we can modify it and return it
s.Peers = peers s.Peers = peers
// if not current peer is set use first one // if not current peer is set use first one

View File

@ -8,7 +8,7 @@ import (
"github.com/jroimartin/gocui" "github.com/jroimartin/gocui"
) )
func GenRenderFunc(g *gocui.Gui, state *State) func() { func GenRenderFunc(g *gocui.Gui, state *AppState) func() {
return func() { return func() {
ps := state.GetState() ps := state.GetState()
renderPeerList(g, ps.Peers) renderPeerList(g, ps.Peers)

View File

@ -9,7 +9,7 @@ import (
// This might need renaming, since it also contains the Client. // This might need renaming, since it also contains the Client.
// I need the client to make the RPC calls. // I need the client to make the RPC calls.
type State struct { type AppState struct {
Reducer *AppModel Reducer *AppModel
Store *store.Store Store *store.Store
Client *StatusGoClient Client *StatusGoClient
@ -17,16 +17,16 @@ type State struct {
setCurrent *rematch.Action setCurrent *rematch.Action
} }
func NewState(client *StatusGoClient) *State { func NewState(client *StatusGoClient) *AppState {
// Generate the reducer from our model. // Generate the reducer from our model.
Reducer := &AppModel{ Reducer := &AppModel{
State: AppState{ State: AppData{
Peers: make([]Peer, 0), Peers: make([]Peer, 0),
Current: -1, // Should mean non selected. Current: -1, // Should mean non selected.
}, },
} }
// Instantiate the redux state from the reducer. // Instantiate the redux state from the reducer.
return &State{ return &AppState{
Reducer: Reducer, Reducer: Reducer,
// Define the store. // Define the store.
Store: store.New(Reducer), Store: store.New(Reducer),
@ -39,25 +39,25 @@ func NewState(client *StatusGoClient) *State {
} }
// Helpers for shorter calls. // Helpers for shorter calls.
func (s *State) Update(peers []Peer) { func (s *AppState) Update(peers []Peer) {
s.Store.Dispatch(s.updatePeers.With(peers)) s.Store.Dispatch(s.updatePeers.With(peers))
} }
func (s *State) GetCurrent() *Peer { func (s *AppState) GetCurrent() *Peer {
state := s.GetState() state := s.GetState()
if state.Current == -1 { if state.Current == -1 {
return nil return nil
} }
return &state.Peers[state.Current] return &state.Peers[state.Current]
} }
func (s *State) SetCurrent(index int) { func (s *AppState) SetCurrent(index int) {
s.Store.Dispatch(s.setCurrent.With(index)) s.Store.Dispatch(s.setCurrent.With(index))
} }
func (s *State) GetState() AppState { func (s *AppState) GetState() AppData {
return s.Store.StateOf(s.Reducer).(AppState) return s.Store.StateOf(s.Reducer).(AppData)
} }
// For fetching current state of peers from status-go // For fetching current state of peers from status-go
func (s *State) Fetch() { func (s *AppState) Fetch() {
peers, err := s.Client.getPeers() peers, err := s.Client.getPeers()
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
@ -70,7 +70,7 @@ func (s *State) Fetch() {
} }
// For removing a selected peer from connected to status-go // For removing a selected peer from connected to status-go
func (s *State) Remove(peer *Peer) error { func (s *AppState) Remove(peer *Peer) error {
success, err := s.Client.removePeer(peer.Enode) success, err := s.Client.removePeer(peer.Enode)
if err != nil || success != true { if err != nil || success != true {
log.Panicln(err) log.Panicln(err)

View File

@ -32,7 +32,7 @@ type ViewController struct {
SelFgColor gocui.Attribute SelFgColor gocui.Attribute
Keybindings []Binding Keybindings []Binding
// Extra field for view state. Might need different name. // Extra field for view state. Might need different name.
State *State State *AppState
} }
// To combine all existing views into one // To combine all existing views into one