Move history mutex to rooms.Room
This commit is contained in:
parent
516ea42317
commit
c32fffda15
@ -18,6 +18,7 @@ package rooms
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"maunium.net/go/gomatrix"
|
"maunium.net/go/gomatrix"
|
||||||
)
|
)
|
||||||
@ -39,6 +40,22 @@ type Room struct {
|
|||||||
nameCache string
|
nameCache string
|
||||||
// The topic of the room. Directly fetched from the m.room.topic state event.
|
// The topic of the room. Directly fetched from the m.room.topic state event.
|
||||||
topicCache string
|
topicCache string
|
||||||
|
|
||||||
|
// fetchHistoryLock is used to make sure multiple goroutines don't fetch history for this room at the same time.
|
||||||
|
fetchHistoryLock *sync.Mutex `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (room *Room) LockHistory() {
|
||||||
|
if room.fetchHistoryLock == nil {
|
||||||
|
room.fetchHistoryLock = &sync.Mutex{}
|
||||||
|
}
|
||||||
|
room.fetchHistoryLock.Lock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (room *Room) UnlockHistory() {
|
||||||
|
if room.fetchHistoryLock != nil {
|
||||||
|
room.fetchHistoryLock.Unlock()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateState updates the room's current state with the given Event. This will clobber events based
|
// UpdateState updates the room's current state with the given Event. This will clobber events based
|
||||||
@ -212,6 +229,7 @@ func (room *Room) GetMember(userID string) *Member {
|
|||||||
func NewRoom(roomID, owner string) *Room {
|
func NewRoom(roomID, owner string) *Room {
|
||||||
return &Room{
|
return &Room{
|
||||||
Room: gomatrix.NewRoom(roomID),
|
Room: gomatrix.NewRoom(roomID),
|
||||||
|
fetchHistoryLock: &sync.Mutex{},
|
||||||
SessionUserID: owner,
|
SessionUserID: owner,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -226,11 +226,7 @@ func (view *MainView) SwitchRoom(roomIndex int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
view.currentRoomIndex = roomIndex % len(view.roomIDs)
|
view.currentRoomIndex = roomIndex % len(view.roomIDs)
|
||||||
if !view.roomView.HasPage(view.CurrentRoomID()) {
|
|
||||||
debug.Print("Oh noes!", view.CurrentRoomID(), "has no page!")
|
|
||||||
} else {
|
|
||||||
view.roomView.SwitchToPage(view.CurrentRoomID())
|
view.roomView.SwitchToPage(view.CurrentRoomID())
|
||||||
}
|
|
||||||
view.roomList.SetCurrentItem(roomIndex)
|
view.roomList.SetCurrentItem(roomIndex)
|
||||||
view.gmx.App().SetFocus(view)
|
view.gmx.App().SetFocus(view)
|
||||||
view.parent.Render()
|
view.parent.Render()
|
||||||
@ -344,10 +340,10 @@ func (view *MainView) LoadHistory(room string, initial bool) {
|
|||||||
batch := roomView.Room.PrevBatch
|
batch := roomView.Room.PrevBatch
|
||||||
lockTime := time.Now().Unix() + 1
|
lockTime := time.Now().Unix() + 1
|
||||||
|
|
||||||
roomView.FetchHistoryLock.Lock()
|
roomView.Room.LockHistory()
|
||||||
roomView.MessageView().LoadingMessages = true
|
roomView.MessageView().LoadingMessages = true
|
||||||
defer func() {
|
defer func() {
|
||||||
roomView.FetchHistoryLock.Unlock()
|
roomView.Room.UnlockHistory()
|
||||||
roomView.MessageView().LoadingMessages = false
|
roomView.MessageView().LoadingMessages = false
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -19,7 +19,6 @@ package widget
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gdamore/tcell"
|
"github.com/gdamore/tcell"
|
||||||
@ -35,10 +34,9 @@ type RoomView struct {
|
|||||||
content *MessageView
|
content *MessageView
|
||||||
status *tview.TextView
|
status *tview.TextView
|
||||||
userList *tview.TextView
|
userList *tview.TextView
|
||||||
|
ulBorder *Border
|
||||||
input *AdvancedInputField
|
input *AdvancedInputField
|
||||||
Room *rooms.Room
|
Room *rooms.Room
|
||||||
|
|
||||||
FetchHistoryLock *sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRoomView(room *rooms.Room) *RoomView {
|
func NewRoomView(room *rooms.Room) *RoomView {
|
||||||
@ -48,8 +46,8 @@ func NewRoomView(room *rooms.Room) *RoomView {
|
|||||||
content: NewMessageView(),
|
content: NewMessageView(),
|
||||||
status: tview.NewTextView(),
|
status: tview.NewTextView(),
|
||||||
userList: tview.NewTextView(),
|
userList: tview.NewTextView(),
|
||||||
|
ulBorder: NewBorder(),
|
||||||
input: NewAdvancedInputField(),
|
input: NewAdvancedInputField(),
|
||||||
FetchHistoryLock: &sync.Mutex{},
|
|
||||||
Room: room,
|
Room: room,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,18 +122,14 @@ func (view *RoomView) Draw(screen tcell.Screen) {
|
|||||||
view.content.SetRect(x, y+1, width-30, height-3)
|
view.content.SetRect(x, y+1, width-30, height-3)
|
||||||
view.status.SetRect(x, y+height-2, width, 1)
|
view.status.SetRect(x, y+height-2, width, 1)
|
||||||
view.userList.SetRect(x+width-29, y+1, 29, height-3)
|
view.userList.SetRect(x+width-29, y+1, 29, height-3)
|
||||||
|
view.ulBorder.SetRect(x+width-30, y+1, 1, height-3)
|
||||||
view.input.SetRect(x, y+height-1, width, 1)
|
view.input.SetRect(x, y+height-1, width, 1)
|
||||||
|
|
||||||
view.topic.Draw(screen)
|
view.topic.Draw(screen)
|
||||||
view.content.Draw(screen)
|
view.content.Draw(screen)
|
||||||
view.status.Draw(screen)
|
view.status.Draw(screen)
|
||||||
view.input.Draw(screen)
|
view.input.Draw(screen)
|
||||||
|
view.ulBorder.Draw(screen)
|
||||||
borderX := x + width - 30
|
|
||||||
background := tcell.StyleDefault.Background(view.GetBackgroundColor()).Foreground(view.GetBorderColor())
|
|
||||||
for borderY := y + 1; borderY < y+height-2; borderY++ {
|
|
||||||
screen.SetContent(borderX, borderY, tview.GraphicsVertBar, nil, background)
|
|
||||||
}
|
|
||||||
view.userList.Draw(screen)
|
view.userList.Draw(screen)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user