1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03:00

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 { if self.pauseBackgroundRefreshes {
continue continue
} }
self.gui.c.OnWorker(func(gocui.Task) { self.gui.c.OnWorker(func(gocui.Task) error {
_ = function() _ = function()
done <- struct{}{} done <- struct{}{}
return nil
}) })
// waiting so that we don't bunch up refreshes if the refresh takes longer than the interval // waiting so that we don't bunch up refreshes if the refresh takes longer than the interval
<-done <-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 // 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) { func (self *AppStatusHelper) WithWaitingStatus(message string, f func(gocui.Task) error) {
self.c.OnWorker(func(task gocui.Task) { self.c.OnWorker(func(task gocui.Task) error {
err := self.statusMgr().WithWaitingStatus(message, self.renderAppStatus, func(waitingStatusHandle *status.WaitingStatusHandle) error { return self.statusMgr().WithWaitingStatus(message, self.renderAppStatus, func(waitingStatusHandle *status.WaitingStatusHandle) error {
return f(appStatusHelperTask{task, waitingStatusHandle}) 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() { 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)) ticker := time.NewTicker(time.Millisecond * time.Duration(self.c.UserConfig.Gui.Spinner.Rate))
defer ticker.Stop() defer ticker.Stop()
for range ticker.C { for range ticker.C {
@ -103,9 +98,10 @@ func (self *AppStatusHelper) renderAppStatus() {
}) })
if appStatus == "" { if appStatus == "" {
return break
} }
} }
return nil
}) })
} }

View File

@ -68,17 +68,11 @@ func (self *InlineStatusHelper) WithInlineStatus(opts InlineStatusOpts, f func(g
view := context.GetView() view := context.GetView()
visible := view.Visible && self.windowHelper.TopViewInWindow(context.GetWindowName(), false) == view visible := view.Visible && self.windowHelper.TopViewInWindow(context.GetWindowName(), false) == view
if visible && context.IsItemVisible(opts.Item) { if visible && context.IsItemVisible(opts.Item) {
self.c.OnWorker(func(task gocui.Task) { self.c.OnWorker(func(task gocui.Task) error {
self.start(opts) self.start(opts)
defer self.stop(opts)
err := f(inlineStatusHelperTask{task, self, opts}) return f(inlineStatusHelperTask{task, self, opts})
if err != nil {
self.c.OnUIThread(func() error {
return err
})
}
self.stop(opts)
}) })
} else { } else {
message := presentation.ItemOperationToString(opts.Operation, self.c.Tr) 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 // everything happens fast and it's better to have everything update
// in the one frame // in the one frame
if !self.c.InDemo() && options.Mode == types.ASYNC { if !self.c.InDemo() && options.Mode == types.ASYNC {
self.c.OnWorker(func(t gocui.Task) { self.c.OnWorker(func(t gocui.Task) error {
f() f()
return nil
}) })
} else { } else {
wg.Add(1) wg.Add(1)
@ -246,10 +247,11 @@ func getModeName(mode types.RefreshMode) string {
func (self *RefreshHelper) refreshReflogCommitsConsideringStartup() { func (self *RefreshHelper) refreshReflogCommitsConsideringStartup() {
switch self.c.State().GetRepoState().GetStartupStage() { switch self.c.State().GetRepoState().GetStartupStage() {
case types.INITIAL: case types.INITIAL:
self.c.OnWorker(func(_ gocui.Task) { self.c.OnWorker(func(_ gocui.Task) error {
_ = self.refreshReflogCommits() _ = self.refreshReflogCommits()
self.refreshBranches(false, true) self.refreshBranches(false, true)
self.c.State().GetRepoState().SetStartupStage(types.COMPLETE) self.c.State().GetRepoState().SetStartupStage(types.COMPLETE)
return nil
}) })
case types.COMPLETE: case types.COMPLETE:

View File

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

View File

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

View File

@ -180,7 +180,7 @@ func (self *FilesController) Explode(v *gocui.View, onDone func()) {
style.FgBlack.SetBold(), style.FgBlack.SetBold(),
} }
self.c.OnWorker(func(_ gocui.Task) { self.c.OnWorker(func(_ gocui.Task) error {
max := 25 max := 25
for i := 0; i < max; i++ { for i := 0; i < max; i++ {
image := getExplodeImage(width, height, i, max) image := getExplodeImage(width, height, i, max)
@ -198,6 +198,7 @@ func (self *FilesController) Explode(v *gocui.View, onDone func()) {
onDone() onDone()
return nil 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)) { func (gui *Gui) onWorker(f func(gocui.Task) error) {
gui.g.OnWorker(func(t gocui.Task) error { gui.g.OnWorker(f)
// 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) getWindowDimensions(informationStr string, appStatus string) map[string]boxlayout.Dimensions { 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) 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) self.gui.onWorker(f)
} }

View File

@ -85,7 +85,7 @@ type IGuiCommon interface {
OnUIThread(f func() error) 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 // 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 // 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 // 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 // For example, you may want a view's line to be focused only after that view is
// resized, if in accordion mode. // resized, if in accordion mode.

View File

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

View File

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