don't create new state, method argument is already a copy

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2019-07-01 16:16:08 -04:00 committed by Jakub
parent d99f664ede
commit 2bc143aa2b
1 changed files with 8 additions and 13 deletions

View File

@ -16,27 +16,22 @@ type Model struct {
func (m *Model) Current(state PeersState, peerIndex int) PeersState { func (m *Model) Current(state PeersState, peerIndex int) PeersState {
// 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(state.Peers) { if peerIndex >= 0 && peerIndex < len(state.Peers) {
return state state.Current = peerIndex
}
return PeersState{
Peers: state.Peers,
Current: peerIndex,
} }
return state
} }
func (m *Model) Update(state PeersState, peers []Peer) PeersState { func (m *Model) Update(state PeersState, peers []Peer) PeersState {
current := state.Current // The argument is a copy so we can modify it and return it
state.Peers = peers
// if not current peer is set use first one // if not current peer is set use first one
if state.Current == -1 && len(peers) > 0 { if state.Current == -1 && len(peers) > 0 {
current = 0 state.Current = 0
} }
// if set but doesn't exist in the list move up // if set but doesn't exist in the list move up
if state.Current >= len(peers) { if state.Current >= len(peers) {
current = len(peers) - 1 state.Current = len(peers) - 1
}
return PeersState{
Peers: peers,
Current: current,
} }
return state
} }