From ecf69febbc2f26b1a3e8fd5d3b6b7effd58dd1f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Tue, 2 Jul 2019 16:07:12 -0400 Subject: [PATCH] rename State to AppState MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- loop.go | 2 +- main.go | 1 + model.go | 10 +++++----- render.go | 2 +- state.go | 22 +++++++++++----------- view.go | 2 +- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/loop.go b/loop.go index 684a333..6154f1e 100644 --- a/loop.go +++ b/loop.go @@ -4,7 +4,7 @@ import ( "time" ) -func FetchLoop(state *State, interval int) { +func FetchLoop(state *AppState, interval int) { // Get the first peers fetch going sooner state.Fetch() // Then fetch every `interval` seconds diff --git a/main.go b/main.go index 16facbf..e27161c 100644 --- a/main.go +++ b/main.go @@ -49,6 +49,7 @@ func main() { // Create a state wrapper. state := NewState(client) + // Subscribe rendering method to state changes. state.Store.Subscribe(GenRenderFunc(g, state)) diff --git a/model.go b/model.go index d32bf09..2d15405 100644 --- a/model.go +++ b/model.go @@ -4,7 +4,7 @@ import ( "github.com/dannypsnl/redux/v2/rematch" ) -type AppState struct { +type AppData struct { Node NodeInfo // info about current node Peers []Peer // list of peers for the node Current int // currently selected peer @@ -12,15 +12,15 @@ type AppState struct { type AppModel struct { 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 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 if peerIndex >= 0 && peerIndex < len(s.Peers) { s.Current = peerIndex @@ -28,7 +28,7 @@ func (m *AppModel) Current(s AppState, peerIndex int) AppState { 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 s.Peers = peers // if not current peer is set use first one diff --git a/render.go b/render.go index f4a1495..f519782 100644 --- a/render.go +++ b/render.go @@ -8,7 +8,7 @@ import ( "github.com/jroimartin/gocui" ) -func GenRenderFunc(g *gocui.Gui, state *State) func() { +func GenRenderFunc(g *gocui.Gui, state *AppState) func() { return func() { ps := state.GetState() renderPeerList(g, ps.Peers) diff --git a/state.go b/state.go index 7c54fc2..362a989 100644 --- a/state.go +++ b/state.go @@ -9,7 +9,7 @@ import ( // This might need renaming, since it also contains the Client. // I need the client to make the RPC calls. -type State struct { +type AppState struct { Reducer *AppModel Store *store.Store Client *StatusGoClient @@ -17,16 +17,16 @@ type State struct { setCurrent *rematch.Action } -func NewState(client *StatusGoClient) *State { +func NewState(client *StatusGoClient) *AppState { // Generate the reducer from our model. Reducer := &AppModel{ - State: AppState{ + State: AppData{ Peers: make([]Peer, 0), Current: -1, // Should mean non selected. }, } // Instantiate the redux state from the reducer. - return &State{ + return &AppState{ Reducer: Reducer, // Define the store. Store: store.New(Reducer), @@ -39,25 +39,25 @@ func NewState(client *StatusGoClient) *State { } // Helpers for shorter calls. -func (s *State) Update(peers []Peer) { +func (s *AppState) Update(peers []Peer) { s.Store.Dispatch(s.updatePeers.With(peers)) } -func (s *State) GetCurrent() *Peer { +func (s *AppState) GetCurrent() *Peer { state := s.GetState() if state.Current == -1 { return nil } return &state.Peers[state.Current] } -func (s *State) SetCurrent(index int) { +func (s *AppState) SetCurrent(index int) { s.Store.Dispatch(s.setCurrent.With(index)) } -func (s *State) GetState() AppState { - return s.Store.StateOf(s.Reducer).(AppState) +func (s *AppState) GetState() AppData { + return s.Store.StateOf(s.Reducer).(AppData) } // For fetching current state of peers from status-go -func (s *State) Fetch() { +func (s *AppState) Fetch() { peers, err := s.Client.getPeers() if err != nil { log.Panicln(err) @@ -70,7 +70,7 @@ func (s *State) Fetch() { } // 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) if err != nil || success != true { log.Panicln(err) diff --git a/view.go b/view.go index a03564e..5a53027 100644 --- a/view.go +++ b/view.go @@ -32,7 +32,7 @@ type ViewController struct { SelFgColor gocui.Attribute Keybindings []Binding // Extra field for view state. Might need different name. - State *State + State *AppState } // To combine all existing views into one