Schedule refreshes more consistently.
All checks were successful
Build & Test / Main (push) Successful in 1m1s
All checks were successful
Build & Test / Main (push) Successful in 1m1s
Instead of returning the `scheduleRefresh` command only after receiving an `update` message, do it while handling the `refresh` message. For this not to cause weird behavior, the refresh deadline should be shorter than the refresh interval.
This commit is contained in:
parent
7b445a02a2
commit
2d991880ce
@ -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)},
|
||||
|
Loading…
Reference in New Issue
Block a user