Skip to content

Commit

Permalink
Some tight locking, reduced startup drawing time
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole Andre Birkedal committed Jan 27, 2020
1 parent 74c34eb commit 15dd67f
Showing 1 changed file with 39 additions and 28 deletions.
67 changes: 39 additions & 28 deletions ui/activity_bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/rivo/tview"
"sort"
"strings"
"sync"
"time"
)

Expand All @@ -22,6 +23,7 @@ type activityBar struct {
buffers map[string]activityBuffer
sorted []activityBuffer
triggerWords []string
buffersLock sync.Mutex
}

func NewActivityBar(triggers []string) *activityBar {
Expand All @@ -35,7 +37,10 @@ func NewActivityBar(triggers []string) *activityBar {
}
}

func (b *activityBar) updateActivityBar() {
func (b *activityBar) updateActivityBar(view *View) {
b.buffersLock.Lock()
defer b.buffersLock.Unlock()

list := []activityBuffer{}
for _, b := range b.buffers {
list = append(list, b)
Expand All @@ -47,7 +52,9 @@ func (b *activityBar) updateActivityBar() {

b.sorted = list

b.bar.SetText(generateBar(list))
view.app.QueueUpdate(func() {
b.bar.SetText(generateBar(list))
})
}

func bufferToBarElement(buffer activityBuffer) string {
Expand Down Expand Up @@ -86,22 +93,26 @@ func (b *activityBar) hasTriggerWord(line string) bool {
}

func (b *activityBar) MarkAsVisited(buffer string, view *View) {
view.app.QueueUpdateDraw(func() {
elem, ok := b.buffers[buffer]

if ok {
elem.visited = true

// If you have buffers open with activity from 34 years ago
// then this is going to look weird! Not sure of a better way
// to keep the order than a translation down memory lane
elem.lastActivity = elem.lastActivity.AddDate(-34, 0, 0)

// Assign the value back to the map
b.buffers[buffer] = elem
}
b.buffersLock.Lock()
elem, ok := b.buffers[buffer]
b.buffersLock.Unlock()

if ok {
elem.visited = true

// If you have buffers open with activity from 34 years ago
// then this is going to look weird! Not sure of a better way
// to keep the order than a translation down memory lane
elem.lastActivity = elem.lastActivity.AddDate(-34, 0, 0)

// Assign the value back to the map
b.buffersLock.Lock()
b.buffers[buffer] = elem
b.buffersLock.Unlock()
}

b.updateActivityBar()
view.app.QueueUpdateDraw(func() {
b.updateActivityBar(view)
})
}

Expand All @@ -114,16 +125,16 @@ func (b *activityBar) GetLatestActiveChannel() (string, error) {
}

func (b *activityBar) RegisterActivity(buffer, msg string, view *View) {
view.app.QueueUpdate(func() {
trigger := b.hasTriggerWord(msg)

b.buffers[buffer] = activityBuffer{
displayName: buffer,
lastActivity: time.Now(),
visited: false,
triggerWord: trigger,
}
trigger := b.hasTriggerWord(msg)

b.buffersLock.Lock()
b.buffers[buffer] = activityBuffer{
displayName: buffer,
lastActivity: time.Now(),
visited: false,
triggerWord: trigger,
}
b.buffersLock.Unlock()

b.updateActivityBar()
})
b.updateActivityBar(view)
}

0 comments on commit 15dd67f

Please sign in to comment.