2019-06-27 18:21:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/dannypsnl/redux/rematch"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PeersState struct {
|
|
|
|
Peers []Peer
|
2019-06-27 19:09:02 +00:00
|
|
|
Current int
|
2019-06-27 18:21:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Model struct {
|
|
|
|
rematch.Reducer
|
|
|
|
State PeersState
|
|
|
|
}
|
|
|
|
|
2019-06-27 19:09:02 +00:00
|
|
|
func (m *Model) Current(state PeersState, peerIndex int) PeersState {
|
2019-06-27 19:34:34 +00:00
|
|
|
if peerIndex < 0 || peerIndex >= len(state.Peers) {
|
|
|
|
return state
|
|
|
|
}
|
2019-06-27 18:21:51 +00:00
|
|
|
return PeersState{
|
|
|
|
Peers: state.Peers,
|
2019-06-27 19:09:02 +00:00
|
|
|
Current: peerIndex,
|
2019-06-27 18:21:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 19:09:02 +00:00
|
|
|
func (m *Model) Update(state PeersState, peers []Peer) PeersState {
|
2019-06-27 18:21:51 +00:00
|
|
|
current := state.Current
|
2019-06-27 19:09:02 +00:00
|
|
|
// if not current peer is set use first one
|
|
|
|
if state.Current == -1 && len(peers) > 0 {
|
|
|
|
current = 0
|
|
|
|
}
|
|
|
|
// if set but doesn't exist in the list move up
|
|
|
|
if state.Current >= len(peers) {
|
|
|
|
current = len(peers) - 1
|
2019-06-27 18:21:51 +00:00
|
|
|
}
|
|
|
|
return PeersState{
|
|
|
|
Peers: peers,
|
|
|
|
Current: current,
|
|
|
|
}
|
|
|
|
}
|