Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Fix deadlocks when swapping the channel and receiving a message at th…
Browse files Browse the repository at this point in the history
…e same time
  • Loading branch information
Bios-Marcel committed Oct 12, 2019
1 parent 1e4ae10 commit 40e493c
Showing 1 changed file with 52 additions and 33 deletions.
85 changes: 52 additions & 33 deletions ui/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
loadError := window.LoadChannel(channel)
if loadError == nil {
channelTree.MarkChannelAsLoaded(channelID)
}
go func() {
window.chatView.Lock()
defer window.chatView.Unlock()
window.QueueUpdateDrawSynchronized(func() {
loadError := window.LoadChannel(channel)
if loadError == nil {
channelTree.MarkChannelAsLoaded(channelID)
}
})
}()
}
})
window.registerGuildChannelHandler()
Expand Down Expand Up @@ -251,39 +257,52 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
return
}

window.LoadChannel(channel)
if channel.Type == discordgo.ChannelTypeGroupDM {
loadError := window.userList.LoadGroup(channel.ID)
if loadError != nil {
fmt.Fprintln(window.commandView.commandOutput, "Error loading users for channel.")
}
}
go func() {
window.chatView.Lock()
defer window.chatView.Unlock()
window.QueueUpdateDrawSynchronized(func() {
window.LoadChannel(channel)

window.RefreshLayout()
if channel.Type == discordgo.ChannelTypeGroupDM {
loadError := window.userList.LoadGroup(channel.ID)
if loadError != nil {
fmt.Fprintln(window.commandView.commandOutput, "Error loading users for channel.")
}
}

window.RefreshLayout()
})
}()
})

window.privateList.SetOnFriendSelect(func(userID string) {
userChannels, _ := window.session.UserChannels()
for _, userChannel := range userChannels {
if userChannel.Type == discordgo.ChannelTypeDM &&
(userChannel.Recipients[0].ID == userID) {
window.LoadChannel(userChannel)
window.RefreshLayout()
return
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.QueueUpdateDrawSynchronized(func() {
window.loadPrivateChannel(userChannel)
})
return
}
}
}

newChannel, discordError := window.session.UserChannelCreate(userID)
if discordError == nil {
messages, discordError := window.session.ChannelMessages(newChannel.ID, 100, "", "", "")
newChannel, discordError := window.session.UserChannelCreate(userID)
if discordError == nil {
for _, message := range messages {
window.session.State.MessageAdd(message)
messages, discordError := window.session.ChannelMessages(newChannel.ID, 100, "", "", "")
if discordError == nil {
for _, message := range messages {
window.session.State.MessageAdd(message)
}
}
window.QueueUpdateDrawSynchronized(func() {
window.loadPrivateChannel(newChannel)
})
}
window.LoadChannel(newChannel)
window.RefreshLayout()
}
}()
})

window.chatArea = tview.NewFlex().
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)

Expand All @@ -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 {
Expand All @@ -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()

Expand Down

0 comments on commit 40e493c

Please sign in to comment.