Skip to content

Commit

Permalink
feat(branches): config for refresh interval of prs+branches (#436)
Browse files Browse the repository at this point in the history
* fix(branches): refresh interval

* feat(branches): config for refresh interval of prs+branches
  • Loading branch information
dlvhdr authored Sep 4, 2024
1 parent 4d4f61e commit 04fff7f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
10 changes: 10 additions & 0 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ type Defaults struct {
DateFormat string `yaml:"dateFormat,omitempty"`
}

type RepoConfig struct {
BranchesRefetchIntervalSeconds int `yaml:"branchesRefetchIntervalSeconds,omitempty"`
PrsRefetchIntervalSeconds int `yaml:"prsRefetchIntervalSeconds,omitempty"`
}

type Keybinding struct {
Key string `yaml:"key"`
Command string `yaml:"command"`
Expand Down Expand Up @@ -183,6 +188,7 @@ type ThemeConfig struct {
type Config struct {
PRSections []PrsSectionConfig `yaml:"prSections"`
IssuesSections []IssuesSectionConfig `yaml:"issuesSections"`
Repo RepoConfig `yaml:"repo"`
Defaults Defaults `yaml:"defaults"`
Keybindings Keybindings `yaml:"keybindings"`
RepoPaths map[string]string `yaml:"repoPaths"`
Expand Down Expand Up @@ -250,6 +256,10 @@ func (parser ConfigParser) getDefaultConfig() Config {
},
},
},
Repo: RepoConfig{
BranchesRefetchIntervalSeconds: 30,
PrsRefetchIntervalSeconds: 60,
},
PRSections: []PrsSectionConfig{
{
Title: "My Pull Requests",
Expand Down
38 changes: 27 additions & 11 deletions ui/components/reposection/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package reposection

import (
"fmt"
"sync"
"time"

gitm "github.com/aymanbagabas/git-module"
Expand Down Expand Up @@ -247,23 +248,38 @@ func (m *Model) fetchPRsCmd() tea.Cmd {
})
}

type RefreshBranchesMsg time.Time
type RefreshBranchesMsg struct {
id int
time time.Time
}

type FetchMsg time.Time
type RefreshPrsMsg struct {
id int
time time.Time
}

const refreshIntervalSec = 30
var (
lastID int
idMtx sync.Mutex
)

const fetchIntervalSec = 60
// Return the next ID we should use on the Model.
func nextID() int {
idMtx.Lock()
defer idMtx.Unlock()
lastID++
return lastID
}

func (m *Model) tickRefreshBranchesCmd() tea.Cmd {
return tea.Tick(time.Second*refreshIntervalSec, func(t time.Time) tea.Msg {
return RefreshBranchesMsg(t)
return tea.Tick(time.Second*time.Duration(m.Ctx.Config.Repo.BranchesRefetchIntervalSeconds), func(t time.Time) tea.Msg {
return RefreshBranchesMsg{id: m.refreshId, time: t}
})
}

func (m *Model) tickFetchCmd() tea.Cmd {
return tea.Tick(time.Second*fetchIntervalSec, func(t time.Time) tea.Msg {
return FetchMsg(t)
func (m *Model) tickFetchPrsCmd() tea.Cmd {
return tea.Tick(time.Second*time.Duration(m.Ctx.Config.Repo.PrsRefetchIntervalSeconds), func(t time.Time) tea.Msg {
return RefreshPrsMsg{id: m.refreshId, time: t}
})
}

Expand All @@ -274,10 +290,10 @@ func (m *Model) onRefreshBranchesMsg() []tea.Cmd {
return cmds
}

func (m *Model) onFetchMsg() []tea.Cmd {
func (m *Model) onRefreshPrsMsg() []tea.Cmd {
cmds := make([]tea.Cmd, 0)
cmds = append(cmds, m.fetchRepoCmd()...)
cmds = append(cmds, m.tickRefreshBranchesCmd())
cmds = append(cmds, m.tickFetchPrsCmd())
return cmds
}

Expand Down
14 changes: 10 additions & 4 deletions ui/components/reposection/reposection.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Model struct {
Branches []branch.Branch
Prs []data.PullRequestData
isRefreshSetUp bool
refreshId int
}

func NewModel(
Expand Down Expand Up @@ -164,10 +165,14 @@ func (m *Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
m.Prs = msg.Prs

case RefreshBranchesMsg:
cmds = append(cmds, m.onRefreshBranchesMsg()...)
if msg.id == m.refreshId {
cmds = append(cmds, m.onRefreshBranchesMsg()...)
}

case FetchMsg:
cmds = append(cmds, m.onFetchMsg()...)
case RefreshPrsMsg:
if msg.id == m.refreshId {
cmds = append(cmds, m.onRefreshPrsMsg()...)
}

}

Expand Down Expand Up @@ -469,6 +474,7 @@ func FetchAllBranches(ctx context.ProgramContext) (Model, tea.Cmd) {
cfg,
time.Now(),
)
m.refreshId = nextID()

if ctx.RepoPath != nil {
cmds = append(cmds, m.readRepoCmd()...)
Expand All @@ -479,7 +485,7 @@ func FetchAllBranches(ctx context.ProgramContext) (Model, tea.Cmd) {
if !m.isRefreshSetUp {
m.isRefreshSetUp = true
cmds = append(cmds, m.tickRefreshBranchesCmd())
cmds = append(cmds, m.tickFetchCmd())
cmds = append(cmds, m.tickFetchPrsCmd())
}

return m, tea.Batch(cmds...)
Expand Down

0 comments on commit 04fff7f

Please sign in to comment.