Skip to content

Commit

Permalink
fix: add refresh queue to synchronize refreshes (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaP106 authored Sep 26, 2024
1 parent 7e3da3c commit ab4394b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/GianlucaP106/gotmux v0.2.0 h1:6oVhs+r6kyIQsPjrgxGYH1/Ki5epb9AW9zeamkStKnA=
github.com/GianlucaP106/gotmux v0.2.0/go.mod h1:qOsZ+exnCbgv3KJ84VaBo4Q7mXs/W23CW4fyoXAgKe4=
github.com/GianlucaP106/gotmux v0.2.1-0.20240906010315-bb38e85334e8 h1:VOR/nn5FQLFGU6g5p9kXi9UmW3GGsZFvdXBWFggD6BI=
github.com/GianlucaP106/gotmux v0.2.1-0.20240906010315-bb38e85334e8/go.mod h1:qOsZ+exnCbgv3KJ84VaBo4Q7mXs/W23CW4fyoXAgKe4=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
Expand Down
18 changes: 15 additions & 3 deletions pkg/ui/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Start(api *core.Api) {
if getApi().LocalConfiguration.IsConfigInitialized {
_ui.InitUI()
} else if getApi().GlobalConfiguration.Standalone {
_ui.initStandaloneUI()
_ui.InitStandaloneUI()
} else {
_ui.askConfig()
}
Expand Down Expand Up @@ -71,10 +71,10 @@ func focusView(viewName string) {
}

func refreshAsync(v viewable) {
go func() {
queueRefresh(func() {
v.refresh()
renderView(v)
}()
})
}

func getMainTabGroup() *tui.TabGroup {
Expand All @@ -84,3 +84,15 @@ func getMainTabGroup() *tui.TabGroup {
func getApi() *core.Api {
return _ui.api
}

func runAction(f func()) {
tui.Suspend()
f()
tui.Resume()
refreshMainViews()
refreshTmuxViews()
}

func queueRefresh(f func()) {
_ui.queueRefresh(f)
}
4 changes: 2 additions & 2 deletions pkg/ui/tmux_session_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ func (ts *tmuxSessionView) refresh() {
}

func refreshTmuxViews() {
go func() {
queueRefresh(func() {
ts := getTmuxSessionView()
ts.refresh()
renderView(ts)
ts.refreshDown()
}()
})
}

func (ts *tmuxSessionView) refreshDown() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/ui/topic_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,15 @@ func (tv *topicsView) refresh() {

func refreshMainViews() {
if !getApi().GlobalConfiguration.Standalone {
go func() {
queueRefresh(func() {
t := getTopicsView()
t.refresh()
renderView(t)

wv := getWorkspacesView()
wv.refresh()
renderView(wv)
}()
})
}
}

Expand Down
29 changes: 19 additions & 10 deletions pkg/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type UI struct {
mainTabGroup *tui.TabGroup
api *core.Api
refreshQueue chan func()
views []viewable
}

Expand Down Expand Up @@ -58,10 +59,11 @@ func (ui *UI) InitUI() *UI {
}

ui.initGlobalKeybindings()
ui.initRefreshExecutor()
return ui
}

func (ui *UI) initStandaloneUI() {
func (ui *UI) InitStandaloneUI() {
ui.views = []viewable{
newHeaderView(),
newTmuxSessionView(),
Expand All @@ -82,13 +84,28 @@ func (ui *UI) initStandaloneUI() {

systemUpdate()
ui.initGlobalKeybindings()
ui.initRefreshExecutor()
}

func (ui *UI) initRefreshExecutor() {
ui.refreshQueue = make(chan func(), 10)
go func() {
for {
task := <-ui.refreshQueue
task()
}
}()
}

func (ui *UI) queueRefresh(f func()) {
ui.refreshQueue <- f
}

func (ui *UI) askConfig() {
openConfirmationDialog(func(b bool) {
if !b {
getApi().GlobalConfiguration.SetStandalone(true)
ui.initStandaloneUI()
ui.InitStandaloneUI()
return
}

Expand Down Expand Up @@ -166,11 +183,3 @@ func (ui *UI) buildGithubTab() *tui.Tab {
tab.GenerateNavigationKeyBindings()
return tab
}

func runAction(f func()) {
tui.Suspend()
f()
tui.Resume()
refreshMainViews()
refreshTmuxViews()
}

0 comments on commit ab4394b

Please sign in to comment.