mirror of
https://github.com/tulir/gomuks.git
synced 2025-04-20 10:33:41 -05:00
Synchronize access to roster view room list
This commit is contained in:
parent
b9529e39e1
commit
22acad8287
1 changed files with 22 additions and 0 deletions
|
@ -19,6 +19,8 @@ package ui
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
sync "github.com/sasha-s/go-deadlock"
|
||||||
|
|
||||||
"go.mau.fi/mauview"
|
"go.mau.fi/mauview"
|
||||||
"go.mau.fi/tcell"
|
"go.mau.fi/tcell"
|
||||||
|
|
||||||
|
@ -30,6 +32,7 @@ import (
|
||||||
|
|
||||||
type RosterView struct {
|
type RosterView struct {
|
||||||
mauview.Component
|
mauview.Component
|
||||||
|
sync.RWMutex
|
||||||
|
|
||||||
selected *rooms.Room
|
selected *rooms.Room
|
||||||
rooms []*rooms.Room
|
rooms []*rooms.Room
|
||||||
|
@ -53,6 +56,10 @@ func (rstr *RosterView) Add(room *rooms.Room) {
|
||||||
if room.IsReplaced() {
|
if room.IsReplaced() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rstr.Lock()
|
||||||
|
defer rstr.Unlock()
|
||||||
|
|
||||||
insertAt := len(rstr.rooms)
|
insertAt := len(rstr.rooms)
|
||||||
for i := 0; i < len(rstr.rooms); i++ {
|
for i := 0; i < len(rstr.rooms); i++ {
|
||||||
if rstr.rooms[i] == room {
|
if rstr.rooms[i] == room {
|
||||||
|
@ -72,6 +79,10 @@ func (rstr *RosterView) Remove(room *rooms.Room) {
|
||||||
if index < 0 || index > len(rstr.rooms) {
|
if index < 0 || index > len(rstr.rooms) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rstr.Lock()
|
||||||
|
defer rstr.Unlock()
|
||||||
|
|
||||||
last := len(rstr.rooms) - 1
|
last := len(rstr.rooms) - 1
|
||||||
if index < last {
|
if index < last {
|
||||||
copy(rstr.rooms[index:], rstr.rooms[index+1:])
|
copy(rstr.rooms[index:], rstr.rooms[index+1:])
|
||||||
|
@ -81,6 +92,9 @@ func (rstr *RosterView) Remove(room *rooms.Room) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rstr *RosterView) index(room *rooms.Room) int {
|
func (rstr *RosterView) index(room *rooms.Room) int {
|
||||||
|
rstr.Lock()
|
||||||
|
defer rstr.Unlock()
|
||||||
|
|
||||||
for index, entry := range rstr.rooms {
|
for index, entry := range rstr.rooms {
|
||||||
if entry == room {
|
if entry == room {
|
||||||
return index
|
return index
|
||||||
|
@ -109,10 +123,14 @@ func (rstr *RosterView) getMostRecentMessage(room *rooms.Room) (string, bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rstr *RosterView) First() *rooms.Room {
|
func (rstr *RosterView) First() *rooms.Room {
|
||||||
|
rstr.Lock()
|
||||||
|
defer rstr.Unlock()
|
||||||
return rstr.rooms[0]
|
return rstr.rooms[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rstr *RosterView) Last() *rooms.Room {
|
func (rstr *RosterView) Last() *rooms.Room {
|
||||||
|
rstr.Lock()
|
||||||
|
defer rstr.Unlock()
|
||||||
return rstr.rooms[len(rstr.rooms)-1]
|
return rstr.rooms[len(rstr.rooms)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,6 +138,8 @@ func (rstr *RosterView) ScrollNext() {
|
||||||
if index := rstr.index(rstr.selected); index == -1 || index == len(rstr.rooms)-1 {
|
if index := rstr.index(rstr.selected); index == -1 || index == len(rstr.rooms)-1 {
|
||||||
rstr.selected = rstr.First()
|
rstr.selected = rstr.First()
|
||||||
} else {
|
} else {
|
||||||
|
rstr.Lock()
|
||||||
|
defer rstr.Unlock()
|
||||||
rstr.selected = rstr.rooms[index+1]
|
rstr.selected = rstr.rooms[index+1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -128,6 +148,8 @@ func (rstr *RosterView) ScrollPrev() {
|
||||||
if index := rstr.index(rstr.selected); index < 1 {
|
if index := rstr.index(rstr.selected); index < 1 {
|
||||||
rstr.selected = rstr.Last()
|
rstr.selected = rstr.Last()
|
||||||
} else {
|
} else {
|
||||||
|
rstr.Lock()
|
||||||
|
defer rstr.Unlock()
|
||||||
rstr.selected = rstr.rooms[index-1]
|
rstr.selected = rstr.rooms[index-1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue