diff --git a/tui/asset/asset.go b/tui/asset/asset.go index a4169b0..b1611fd 100644 --- a/tui/asset/asset.go +++ b/tui/asset/asset.go @@ -112,18 +112,19 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { return m, tea.Batch( m.resumeIndicator, m.refresh, + m.scheduleRefresh(), ) case update: - commands := []tea.Cmd{m.scheduleRefresh()} + var cmd tea.Cmd if msg.err == nil { m.math = msg.math refillProperties(&m) refillProjections(&m) - commands = append(commands, m.stopIndicator()) + cmd = m.stopIndicator() } else { m.indicator.Style = indicatorErrorStyle } - return m, tea.Batch(commands...) + return m, cmd case stopIndicator: m.refreshing = false return m, nil @@ -149,7 +150,7 @@ type stopIndicator struct{} func (m Model) refresh() tea.Msg { ctx, cancel := context.WithDeadline( context.Background(), - time.Now().Add(time.Second*15)) + time.Now().Add(refreshDeadline)) defer cancel() err := m.math.Refresh(ctx) if err != nil { @@ -166,7 +167,7 @@ func (m Model) resumeIndicator() tea.Msg { } func (m Model) scheduleRefresh() tea.Cmd { - return tea.Tick(time.Second*30, + return tea.Tick(refreshInterval, func(t time.Time) tea.Msg { return Msg{m.math.Asset, refresh{}} }) @@ -175,12 +176,16 @@ func (m Model) scheduleRefresh() tea.Cmd { func (m Model) stopIndicator() tea.Cmd { // wait a bit to stop the indicator, so that it's more obvious // even when the refresh completes quickly. - return tea.Tick(time.Millisecond*500, + return tea.Tick(stopIndicatorDelay, func(t time.Time) tea.Msg { return Msg{m.math.Asset, stopIndicator{}} }) } +var refreshInterval = time.Second * 30 +var refreshDeadline = time.Second * 15 +var stopIndicatorDelay = time.Millisecond * 500 + func refillProperties(m *Model) { rows := []table.Row{ {"Asset", string(m.math.Asset)},