Skip to content

Commit 2af56de

Browse files
committed
Trigger background fetch on repo switch only if enough time has passed since the last one
1 parent d45f27b commit 2af56de

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

pkg/gui/background.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ func (self *BackgroundRoutineMgr) startBackgroundFetch() {
7979
self.gui.waitForIntro.Wait()
8080

8181
fetch := func() error {
82+
// Do this on the UI thread so that we don't have to deal with synchronization around the
83+
// access of the repo state.
84+
self.gui.onUIThread(func() error {
85+
// There's a race here, where we might be recording the time stamp for a different repo
86+
// than where the fetch actually ran. It's not very likely though, and not harmful if it
87+
// does happen; guarding against it would be more effort than it's worth.
88+
self.gui.State.LastBackgroundFetchTime = time.Now()
89+
return nil
90+
})
91+
8292
return self.gui.helpers.AppStatus.WithWaitingStatusImpl(self.gui.Tr.FetchingStatus, func(gocui.Task) error {
8393
return self.backgroundFetch()
8494
}, nil)

pkg/gui/gui.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"sort"
1313
"strings"
1414
"sync"
15+
"time"
1516

1617
"github.com/jesseduffield/gocui"
1718
"github.com/jesseduffield/lazycore/pkg/boxlayout"
@@ -253,6 +254,8 @@ type GuiRepoState struct {
253254
ScreenMode types.ScreenMode
254255

255256
CurrentPopupOpts *types.CreatePopupPanelOpts
257+
258+
LastBackgroundFetchTime time.Time
256259
}
257260

258261
var _ types.IRepoStateAccessor = new(GuiRepoState)
@@ -308,7 +311,9 @@ func (self *GuiRepoState) GetSplitMainPanel() bool {
308311
func (gui *Gui) onSwitchToNewRepo(startArgs appTypes.StartArgs, contextKey types.ContextKey) error {
309312
err := gui.onNewRepo(startArgs, contextKey)
310313
if err == nil && gui.UserConfig().Git.AutoFetch && gui.UserConfig().Refresher.FetchInterval > 0 {
311-
gui.BackgroundRoutineMgr.triggerImmediateFetch()
314+
if time.Since(gui.State.LastBackgroundFetchTime) > gui.UserConfig().Refresher.FetchIntervalDuration() {
315+
gui.BackgroundRoutineMgr.triggerImmediateFetch()
316+
}
312317
}
313318
return err
314319
}

0 commit comments

Comments
 (0)