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:
parent
d99f664ede
commit
2bc143aa2b
21
model.go
21
model.go
|
@ -16,27 +16,22 @@ type Model struct {
|
|||
|
||||
func (m *Model) Current(state PeersState, peerIndex int) PeersState {
|
||||
// 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) {
|
||||
state.Current = peerIndex
|
||||
}
|
||||
return state
|
||||
}
|
||||
return PeersState{
|
||||
Peers: state.Peers,
|
||||
Current: peerIndex,
|
||||
}
|
||||
}
|
||||
|
||||
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 state.Current == -1 && len(peers) > 0 {
|
||||
current = 0
|
||||
state.Current = 0
|
||||
}
|
||||
// if set but doesn't exist in the list move up
|
||||
if state.Current >= len(peers) {
|
||||
current = len(peers) - 1
|
||||
}
|
||||
return PeersState{
|
||||
Peers: peers,
|
||||
Current: current,
|
||||
state.Current = len(peers) - 1
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue