Make OnWorker callback return an error

This lets us get rid of a few more calls to Error(), and it simplifies things
for clients of OnWorker: they can simply return an error from their callback
like we do everywhere else.
This commit is contained in:
Stefan Haller 2024-04-14 20:06:25 +02:00
parent 5396a70661
commit 1869fda800
12 changed files with 30 additions and 44 deletions

View file

@ -87,9 +87,10 @@ func (self *BackgroundRoutineMgr) goEvery(interval time.Duration, stop chan stru
if self.pauseBackgroundRefreshes {
continue
}
self.gui.c.OnWorker(func(gocui.Task) {
self.gui.c.OnWorker(func(gocui.Task) error {
_ = function()
done <- struct{}{}
return nil
})
// waiting so that we don't bunch up refreshes if the refresh takes longer than the interval
<-done

View file

@ -59,15 +59,10 @@ func (self appStatusHelperTask) Continue() {
// withWaitingStatus wraps a function and shows a waiting status while the function is still executing
func (self *AppStatusHelper) WithWaitingStatus(message string, f func(gocui.Task) error) {
self.c.OnWorker(func(task gocui.Task) {
err := self.statusMgr().WithWaitingStatus(message, self.renderAppStatus, func(waitingStatusHandle *status.WaitingStatusHandle) error {
self.c.OnWorker(func(task gocui.Task) error {
return self.statusMgr().WithWaitingStatus(message, self.renderAppStatus, func(waitingStatusHandle *status.WaitingStatusHandle) error {
return f(appStatusHelperTask{task, waitingStatusHandle})
})
if err != nil {
self.c.OnUIThread(func() error {
return err
})
}
})
}
@ -91,7 +86,7 @@ func (self *AppStatusHelper) GetStatusString() string {
}
func (self *AppStatusHelper) renderAppStatus() {
self.c.OnWorker(func(_ gocui.Task) {
self.c.OnWorker(func(_ gocui.Task) error {
ticker := time.NewTicker(time.Millisecond * time.Duration(self.c.UserConfig.Gui.Spinner.Rate))
defer ticker.Stop()
for range ticker.C {
@ -103,9 +98,10 @@ func (self *AppStatusHelper) renderAppStatus() {
})
if appStatus == "" {
return
break
}
}
return nil
})
}

View file

@ -68,17 +68,11 @@ func (self *InlineStatusHelper) WithInlineStatus(opts InlineStatusOpts, f func(g
view := context.GetView()
visible := view.Visible && self.windowHelper.TopViewInWindow(context.GetWindowName(), false) == view
if visible && context.IsItemVisible(opts.Item) {
self.c.OnWorker(func(task gocui.Task) {
self.c.OnWorker(func(task gocui.Task) error {
self.start(opts)
defer self.stop(opts)
err := f(inlineStatusHelperTask{task, self, opts})
if err != nil {
self.c.OnUIThread(func() error {
return err
})
}
self.stop(opts)
return f(inlineStatusHelperTask{task, self, opts})
})
} else {
message := presentation.ItemOperationToString(opts.Operation, self.c.Tr)

View file

@ -104,8 +104,9 @@ func (self *RefreshHelper) Refresh(options types.RefreshOptions) error {
// everything happens fast and it's better to have everything update
// in the one frame
if !self.c.InDemo() && options.Mode == types.ASYNC {
self.c.OnWorker(func(t gocui.Task) {
self.c.OnWorker(func(t gocui.Task) error {
f()
return nil
})
} else {
wg.Add(1)
@ -246,10 +247,11 @@ func getModeName(mode types.RefreshMode) string {
func (self *RefreshHelper) refreshReflogCommitsConsideringStartup() {
switch self.c.State().GetRepoState().GetStartupStage() {
case types.INITIAL:
self.c.OnWorker(func(_ gocui.Task) {
self.c.OnWorker(func(_ gocui.Task) error {
_ = self.refreshReflogCommits()
self.refreshBranches(false, true)
self.c.State().GetRepoState().SetStartupStage(types.COMPLETE)
return nil
})
case types.COMPLETE:

View file

@ -1153,10 +1153,8 @@ func (self *LocalCommitsController) GetOnFocus() func(types.OnFocusOpts) error {
context := self.context()
if context.GetSelectedLineIdx() > COMMIT_THRESHOLD && context.GetLimitCommits() {
context.SetLimitCommits(false)
self.c.OnWorker(func(_ gocui.Task) {
if err := self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.COMMITS}}); err != nil {
_ = self.c.Error(err)
}
self.c.OnWorker(func(_ gocui.Task) error {
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.COMMITS}})
})
}

View file

@ -68,10 +68,8 @@ func (self *SubCommitsController) GetOnFocus() func(types.OnFocusOpts) error {
context := self.context()
if context.GetSelectedLineIdx() > COMMIT_THRESHOLD && context.GetLimitCommits() {
context.SetLimitCommits(false)
self.c.OnWorker(func(_ gocui.Task) {
if err := self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUB_COMMITS}}); err != nil {
_ = self.c.Error(err)
}
self.c.OnWorker(func(_ gocui.Task) error {
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.SUB_COMMITS}})
})
}

View file

@ -180,7 +180,7 @@ func (self *FilesController) Explode(v *gocui.View, onDone func()) {
style.FgBlack.SetBold(),
}
self.c.OnWorker(func(_ gocui.Task) {
self.c.OnWorker(func(_ gocui.Task) error {
max := 25
for i := 0; i < max; i++ {
image := getExplodeImage(width, height, i, max)
@ -198,6 +198,7 @@ func (self *FilesController) Explode(v *gocui.View, onDone func()) {
onDone()
return nil
})
return nil
})
}

View file

@ -958,13 +958,8 @@ func (gui *Gui) onUIThread(f func() error) {
})
}
func (gui *Gui) onWorker(f func(t gocui.Task)) {
gui.g.OnWorker(func(t gocui.Task) error {
// Hack: adapt to the changed signature in the simplest possible way.
// We'll make this cleaner in subsequent commits in this branch.
f(t)
return nil
})
func (gui *Gui) onWorker(f func(gocui.Task) error) {
gui.g.OnWorker(f)
}
func (gui *Gui) getWindowDimensions(informationStr string, appStatus string) map[string]boxlayout.Dimensions {

View file

@ -151,7 +151,7 @@ func (self *guiCommon) OnUIThread(f func() error) {
self.gui.onUIThread(f)
}
func (self *guiCommon) OnWorker(f func(gocui.Task)) {
func (self *guiCommon) OnWorker(f func(gocui.Task) error) {
self.gui.onWorker(f)
}

View file

@ -85,7 +85,7 @@ type IGuiCommon interface {
OnUIThread(f func() error)
// Runs a function in a goroutine. Use this whenever you want to run a goroutine and keep track of the fact
// that lazygit is still busy. See docs/dev/Busy.md
OnWorker(f func(gocui.Task))
OnWorker(f func(gocui.Task) error)
// Function to call at the end of our 'layout' function which renders views
// For example, you may want a view's line to be focused only after that view is
// resized, if in accordion mode.

View file

@ -18,10 +18,10 @@ type AsyncHandler struct {
lastId int
mutex deadlock.Mutex
onReject func()
onWorker func(func(gocui.Task))
onWorker func(func(gocui.Task) error)
}
func NewAsyncHandler(onWorker func(func(gocui.Task))) *AsyncHandler {
func NewAsyncHandler(onWorker func(func(gocui.Task) error)) *AsyncHandler {
return &AsyncHandler{
mutex: deadlock.Mutex{},
onWorker: onWorker,
@ -34,9 +34,10 @@ func (self *AsyncHandler) Do(f func() func()) {
id := self.currentId
self.mutex.Unlock()
self.onWorker(func(gocui.Task) {
self.onWorker(func(gocui.Task) error {
after := f()
self.handle(after, id)
return nil
})
}

View file

@ -13,8 +13,8 @@ func TestAsyncHandler(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(2)
onWorker := func(f func(gocui.Task)) {
go f(gocui.NewFakeTask())
onWorker := func(f func(gocui.Task) error) {
go func() { _ = f(gocui.NewFakeTask()) }()
}
handler := NewAsyncHandler(onWorker)
handler.onReject = func() {