Skip to content

Commit

Permalink
Clean up error handling of WithWaitingStatus and WithWaitingStatusSync
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhaller committed Apr 18, 2024
1 parent 82a3d33 commit 5396a70
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
21 changes: 10 additions & 11 deletions pkg/gui/controllers/helpers/app_status_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,24 @@ 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) {
self.statusMgr().WithWaitingStatus(message, self.renderAppStatus, func(waitingStatusHandle *status.WaitingStatusHandle) {
if err := f(appStatusHelperTask{task, waitingStatusHandle}); err != nil {
self.c.OnUIThread(func() error {
return err
})
}
err := 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
})
}
})
}

func (self *AppStatusHelper) WithWaitingStatusSync(message string, f func() error) {
self.statusMgr().WithWaitingStatus(message, func() {}, func(*status.WaitingStatusHandle) {
func (self *AppStatusHelper) WithWaitingStatusSync(message string, f func() error) error {
return self.statusMgr().WithWaitingStatus(message, func() {}, func(*status.WaitingStatusHandle) error {
stop := make(chan struct{})
defer func() { close(stop) }()
self.renderAppStatusSync(stop)

if err := f(); err != nil {
_ = self.c.Error(err)
}
return f()
})
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ func NewGui(
func() types.Context { return gui.State.ContextMgr.Current() },
gui.createMenu,
func(message string, f func(gocui.Task) error) { gui.helpers.AppStatus.WithWaitingStatus(message, f) },
func(message string, f func() error) {
gui.helpers.AppStatus.WithWaitingStatusSync(message, f)
func(message string, f func() error) error {
return gui.helpers.AppStatus.WithWaitingStatusSync(message, f)
},
func(message string, kind types.ToastKind) { gui.helpers.AppStatus.Toast(message, kind) },
func() string { return gui.Views.Confirmation.TextArea.GetContent() },
Expand Down
7 changes: 3 additions & 4 deletions pkg/gui/popup/popup_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type PopupHandler struct {
currentContextFn func() types.Context
createMenuFn func(types.CreateMenuOptions) error
withWaitingStatusFn func(message string, f func(gocui.Task) error)
withWaitingStatusSyncFn func(message string, f func() error)
withWaitingStatusSyncFn func(message string, f func() error) error
toastFn func(message string, kind types.ToastKind)
getPromptInputFn func() string
inDemo func() bool
Expand All @@ -34,7 +34,7 @@ func NewPopupHandler(
currentContextFn func() types.Context,
createMenuFn func(types.CreateMenuOptions) error,
withWaitingStatusFn func(message string, f func(gocui.Task) error),
withWaitingStatusSyncFn func(message string, f func() error),
withWaitingStatusSyncFn func(message string, f func() error) error,
toastFn func(message string, kind types.ToastKind),
getPromptInputFn func() string,
inDemo func() bool,
Expand Down Expand Up @@ -76,8 +76,7 @@ func (self *PopupHandler) WithWaitingStatus(message string, f func(gocui.Task) e
}

func (self *PopupHandler) WithWaitingStatusSync(message string, f func() error) error {
self.withWaitingStatusSyncFn(message, f)
return nil
return self.withWaitingStatusSyncFn(message, f)
}

func (self *PopupHandler) Error(err error) error {
Expand Down
7 changes: 3 additions & 4 deletions pkg/gui/status/status_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ func NewStatusManager() *StatusManager {
return &StatusManager{}
}

func (self *StatusManager) WithWaitingStatus(message string, renderFunc func(), f func(*WaitingStatusHandle)) {
func (self *StatusManager) WithWaitingStatus(message string, renderFunc func(), f func(*WaitingStatusHandle) error) error {
handle := &WaitingStatusHandle{statusManager: self, message: message, renderFunc: renderFunc, id: -1}
handle.Show()
defer handle.Hide()

f(handle)

handle.Hide()
return f(handle)
}

func (self *StatusManager) AddToastStatus(message string, kind types.ToastKind) int {
Expand Down

0 comments on commit 5396a70

Please sign in to comment.