summaryrefslogtreecommitdiffstats
path: root/pkg/tasks/tasks.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2023-07-09 11:32:27 +1000
committerJesse Duffield <jessedduffield@gmail.com>2023-07-09 21:30:19 +1000
commit14ecc15e71f648a95dab297ce9360d0badb6669a (patch)
tree8a60cc5a3e9836688e6e1943266541a76628b141 /pkg/tasks/tasks.go
parent9e79ee5fe36dc849479ce8f3b05e7cca56c7c216 (diff)
Use first class task objects instead of global counter
The global counter approach is easy to understand but it's brittle and depends on implicit behaviour that is not very discoverable. With a global counter, if any goroutine accidentally decrements the counter twice, we'll think lazygit is idle when it's actually busy. Likewise if a goroutine accidentally increments the counter twice we'll think lazygit is busy when it's actually idle. With the new approach we have a map of tasks where each task can either be busy or not. We create a new task and add it to the map when we spawn a worker goroutine (among other things) and we remove it once the task is done. The task can also be paused and continued for situations where we switch back and forth between running a program and asking for user input. In order for this to work with `git push` (and other commands that require credentials) we need to obtain the task from gocui when we create the worker goroutine, and then pass it along to the commands package to pause/continue the task as required. This is MUCH more discoverable than the old approach which just decremented and incremented the global counter from within the commands package, but it's at the cost of expanding some function signatures (arguably a good thing). Likewise, whenever you want to call WithWaitingStatus or WithLoaderPanel the callback will now have access to the task for pausing/ continuing. We only need to actually make use of this functionality in a couple of places so it's a high price to pay, but I don't know if I want to introduce a WithWaitingStatusTask and WithLoaderPanelTask function (open to suggestions).
Diffstat (limited to 'pkg/tasks/tasks.go')
-rw-r--r--pkg/tasks/tasks.go38
1 files changed, 18 insertions, 20 deletions
diff --git a/pkg/tasks/tasks.go b/pkg/tasks/tasks.go
index 8edcbfea2..498d8a1da 100644
--- a/pkg/tasks/tasks.go
+++ b/pkg/tasks/tasks.go
@@ -9,6 +9,7 @@ import (
"sync"
"time"
+ "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sasha-s/go-deadlock"
@@ -49,8 +50,7 @@ type ViewBufferManager struct {
onEndOfInput func()
// see docs/dev/Busy.md
- incrementBusyCount func()
- decrementBusyCount func()
+ newTask func() *gocui.Task
// if the user flicks through a heap of items, with each one
// spawning a process to render something to the main view,
@@ -80,19 +80,17 @@ func NewViewBufferManager(
refreshView func(),
onEndOfInput func(),
onNewKey func(),
- incrementBusyCount func(),
- decrementBusyCount func(),
+ newTask func() *gocui.Task,
) *ViewBufferManager {
return &ViewBufferManager{
- Log: log,
- writer: writer,
- beforeStart: beforeStart,
- refreshView: refreshView,
- onEndOfInput: onEndOfInput,
- readLines: make(chan LinesToRead, 1024),
- onNewKey: onNewKey,
- incrementBusyCount: incrementBusyCount,
- decrementBusyCount: decrementBusyCount,
+ Log: log,
+ writer: writer,
+ beforeStart: beforeStart,
+ refreshView: refreshView,
+ onEndOfInput: onEndOfInput,
+ readLines: make(chan LinesToRead, 1024),
+ onNewKey: onNewKey,
+ newTask: newTask,
}
}
@@ -298,18 +296,18 @@ type TaskOpts struct {
}
func (self *ViewBufferManager) NewTask(f func(TaskOpts) error, key string) error {
- self.incrementBusyCount()
+ task := self.newTask()
- var decrementCounterOnce sync.Once
+ var completeTaskOnce sync.Once
- decrementCounter := func() {
- decrementCounterOnce.Do(func() {
- self.decrementBusyCount()
+ completeTask := func() {
+ completeTaskOnce.Do(func() {
+ task.Done()
})
}
go utils.Safe(func() {
- defer decrementCounter()
+ defer completeTask()
self.taskIDMutex.Lock()
self.newTaskID++
@@ -349,7 +347,7 @@ func (self *ViewBufferManager) NewTask(f func(TaskOpts) error, key string) error
self.waitingMutex.Unlock()
- if err := f(TaskOpts{Stop: stop, InitialContentLoaded: decrementCounter}); err != nil {
+ if err := f(TaskOpts{Stop: stop, InitialContentLoaded: completeTask}); err != nil {
self.Log.Error(err) // might need an onError callback
}