minor message buffer improvements

This commit is contained in:
diamondburned 2020-11-05 19:23:06 -08:00
parent f5ba082b86
commit 2f64bb509a
3 changed files with 16 additions and 5 deletions

View File

@ -19,7 +19,10 @@ func NewContainer(ctrl container.Controller) *Container {
} }
func (c *Container) CreateMessage(msg cchat.MessageCreate) { func (c *Container) CreateMessage(msg cchat.MessageCreate) {
gts.ExecAsync(func() { c.GridContainer.CreateMessageUnsafe(msg) }) gts.ExecAsync(func() {
c.GridContainer.CreateMessageUnsafe(msg)
c.GridContainer.CleanMessages()
})
} }
func (c *Container) UpdateMessage(msg cchat.MessageUpdate) { func (c *Container) UpdateMessage(msg cchat.MessageUpdate) {

View File

@ -101,15 +101,23 @@ func NewGridContainer(constr Constructor, ctrl Controller) *GridContainer {
} }
} }
// CreateMessageUnsafe inserts a message as well as cleaning up the backlog if // CreateMessageUnsafe inserts a message. It does not clean up old messages.
// the user is scrolled to the bottom.
func (c *GridContainer) CreateMessageUnsafe(msg cchat.MessageCreate) { func (c *GridContainer) CreateMessageUnsafe(msg cchat.MessageCreate) {
// Insert the message first. // Insert the message first.
c.GridStore.CreateMessageUnsafe(msg) c.GridStore.CreateMessageUnsafe(msg)
}
// CleanMessages cleans up the oldest messages if the user is scrolled to the
// bottom. True is returned if there were changes.
func (c *GridContainer) CleanMessages() bool {
// Determine if the user is scrolled to the bottom for cleaning up. // Determine if the user is scrolled to the bottom for cleaning up.
if c.Bottomed() { if c.Bottomed() {
// Clean up the backlog. // Clean up the backlog.
c.DeleteEarliest(c.MessagesLen() - BacklogLimit) if delta := c.MessagesLen() - BacklogLimit; delta > 0 {
c.DeleteEarliest(delta)
return true
} }
}
return false
} }

View File

@ -134,7 +134,7 @@ func (c *Container) CreateMessage(msg cchat.MessageCreate) {
// Did the handler wipe old messages? It will only do so if the user is // Did the handler wipe old messages? It will only do so if the user is
// scrolled to the bottom. // scrolled to the bottom.
if c.Bottomed() { if c.GridContainer.CleanMessages() {
// We need to uncollapse the first (top) message. No length check is // We need to uncollapse the first (top) message. No length check is
// needed here, as we just inserted a message. // needed here, as we just inserted a message.
c.uncompact(c.FirstMessage()) c.uncompact(c.FirstMessage())