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"
)
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

View File

@ -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))

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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