diff --git a/pkg/gui/background.go b/pkg/gui/background.go index 9d034ffa1..73964838c 100644 --- a/pkg/gui/background.go +++ b/pkg/gui/background.go @@ -79,6 +79,16 @@ func (self *BackgroundRoutineMgr) startBackgroundFetch() { self.gui.waitForIntro.Wait() fetch := func() error { + // Do this on the UI thread so that we don't have to deal with synchronization around the + // access of the repo state. + self.gui.onUIThread(func() error { + // There's a race here, where we might be recording the time stamp for a different repo + // than where the fetch actually ran. It's not very likely though, and not harmful if it + // does happen; guarding against it would be more effort than it's worth. + self.gui.State.LastBackgroundFetchTime = time.Now() + return nil + }) + return self.gui.helpers.AppStatus.WithWaitingStatusImpl(self.gui.Tr.FetchingStatus, func(gocui.Task) error { return self.backgroundFetch() }, nil) diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index b4b295430..a7e17ef9f 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -12,6 +12,7 @@ import ( "sort" "strings" "sync" + "time" "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazycore/pkg/boxlayout" @@ -253,6 +254,8 @@ type GuiRepoState struct { ScreenMode types.ScreenMode CurrentPopupOpts *types.CreatePopupPanelOpts + + LastBackgroundFetchTime time.Time } var _ types.IRepoStateAccessor = new(GuiRepoState) @@ -308,7 +311,9 @@ func (self *GuiRepoState) GetSplitMainPanel() bool { func (gui *Gui) onSwitchToNewRepo(startArgs appTypes.StartArgs, contextKey types.ContextKey) error { err := gui.onNewRepo(startArgs, contextKey) if err == nil && gui.UserConfig().Git.AutoFetch && gui.UserConfig().Refresher.FetchInterval > 0 { - gui.BackgroundRoutineMgr.triggerImmediateFetch() + if time.Since(gui.State.LastBackgroundFetchTime) > gui.UserConfig().Refresher.FetchIntervalDuration() { + gui.BackgroundRoutineMgr.triggerImmediateFetch() + } } return err }