Fix deadlocks when swapping the channel and receiving a message at the same time
MODIFY
ui/window.go
ui/window.go
@@ -140,10 +140,16 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
channelTree.SetOnChannelSelect(func(channelID string) {
channel, cacheError := window.session.State.Channel(channelID)
if cacheError == nil {
+ go func() {
+ window.chatView.Lock()
+ defer window.chatView.Unlock()
+ window.QueueUpdateDrawSynchronized(func() {
loadError := window.LoadChannel(channel)
if loadError == nil {
channelTree.MarkChannelAsLoaded(channelID)
}
+ })
+ }()
}
})
window.registerGuildChannelHandler()
@@ -251,7 +257,12 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
return
}
+ go func() {
+ window.chatView.Lock()
+ defer window.chatView.Unlock()
+ window.QueueUpdateDrawSynchronized(func() {
window.LoadChannel(channel)
+
if channel.Type == discordgo.ChannelTypeGroupDM {
loadError := window.userList.LoadGroup(channel.ID)
if loadError != nil {
@@ -261,14 +272,20 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
window.RefreshLayout()
})
+ }()
+ })
window.privateList.SetOnFriendSelect(func(userID string) {
+ go func() {
+ window.chatView.Lock()
+ defer window.chatView.Unlock()
userChannels, _ := window.session.UserChannels()
for _, userChannel := range userChannels {
if userChannel.Type == discordgo.ChannelTypeDM &&
(userChannel.Recipients[0].ID == userID) {
- window.LoadChannel(userChannel)
- window.RefreshLayout()
+ window.QueueUpdateDrawSynchronized(func() {
+ window.loadPrivateChannel(userChannel)
+ })
return
}
}
@@ -281,9 +298,11 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
window.session.State.MessageAdd(message)
}
}
- window.LoadChannel(newChannel)
- window.RefreshLayout()
+ window.QueueUpdateDrawSynchronized(func() {
+ window.loadPrivateChannel(newChannel)
+ })
}
+ }()
})
window.chatArea = tview.NewFlex().
@@ -897,6 +916,11 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
return window, nil
}
+func (window *Window) loadPrivateChannel(channel *discordgo.Channel) {
+ window.LoadChannel(channel)
+ window.RefreshLayout()
+}
+
func (window *Window) insertNewLineAtCursor() {
left := window.messageInput.internalTextView.GetRegionText("left")
right := window.messageInput.internalTextView.GetRegionText("right")
@@ -2226,11 +2250,9 @@ func (window *Window) LoadChannel(channel *discordgo.Channel) error {
discordutil.SortMessagesByTimestamp(messages)
- window.chatView.Lock()
window.chatView.SetMessages(messages)
window.chatView.ClearSelection()
window.chatView.internalTextView.ScrollToEnd()
- window.chatView.Unlock()
window.UpdateChatHeader(channel)
@@ -2248,8 +2270,6 @@ func (window *Window) LoadChannel(channel *discordgo.Channel) error {
}
}
- window.channelTree.Lock()
-
//If there is a currently loaded guild channel and it isn't the same as
//the new one we assume it must be read and mark it white.
if window.selectedChannelNode != nil && channel.ID != window.selectedChannel.ID {
@@ -2274,7 +2294,6 @@ func (window *Window) LoadChannel(channel *discordgo.Channel) error {
if channel.Type == discordgo.ChannelTypeDM || channel.Type == discordgo.ChannelTypeGroupDM {
window.privateList.MarkChannelAsLoaded(channel)
}
- window.channelTree.Unlock()
window.exitMessageEditModeAndKeepText()

Fix deadlocks when swapping the channel and receiving a message at the same time